Last active
December 29, 2015 23:49
-
-
Save gregspurrier/7745663 to your computer and use it in GitHub Desktop.
Sequence wildcards and sequence arguments in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.immutable.Queue | |
val q = Queue(1, 2, 3) | |
// When pattern matching against a sequence, the sequence wildcard _* | |
// can be used as the last pattern and will match an arbitrary number of | |
// elements. To bind the matched sequence to a variable, use the form | |
// `x @ _*`: | |
val restSeq = q match { case Queue(x, xs @ _*) => xs } | |
// --> rest: Seq[Int] = Queue(2, 3) | |
// Scala also supports sequence arguments to functions that take | |
// repeated parameters. To indicate that a sequence should be | |
// expanded, follow it with `: _*`: | |
val restQ = Queue(restSeq: _*) | |
// --> restQ: scala.collection.immutable.Queue[Int] = Queue(2, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment