However, now and then I really miss the cons operator and deconstructor pattern (::) that is defined on List. An excerpt:
case Nil => Nil
case head :: tail => something(head) :: recurse(tail)
If what I'm matching is a sequence instead of list, I can't use :: in either context. Here is the equivalent for a sequence:
case Seq() => Seq()
case Seq(head, tail @ _*) => something(head) +: recurse(tail)
case Seq(head, tail @ _*) => something(head) +: recurse(tail)
- cons :: is replaced by prepend +:
- The sequence deconstruction pattern _* matches 0 or more sequence elements after the first one[2].
- Meanwhile, the @ operator links an identifier to a portion of the pattern, capturing the rest of the sequence in tail.
- The empty sequence is equivalent to Nil in List.
-----------------
[1] Technically we could be declaring the argument as GenIterable, but we're not that hard-core.
[2] google unapplySeq to learn more about _*
No comments:
Post a Comment