Skip to content

Instantly share code, notes, and snippets.

@gregspurrier
Last active December 29, 2015 23:49
Show Gist options
  • Save gregspurrier/7745663 to your computer and use it in GitHub Desktop.
Save gregspurrier/7745663 to your computer and use it in GitHub Desktop.
Sequence wildcards and sequence arguments in Scala
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