Skip to content

Instantly share code, notes, and snippets.

@wrobstory
Created September 19, 2014 22:25
Show Gist options
  • Save wrobstory/a352ff715d7c94042574 to your computer and use it in GitHub Desktop.
Save wrobstory/a352ff715d7c94042574 to your computer and use it in GitHub Desktop.
Scala pattern match
val vec1 = Vector(1, 2, 3)
val vec2 = Vector(3, 4, 5, 6, 7, 8)
val vec3 = Vector("foo", "bar", "baz")
val vec4 = Vector("one", Map("one" -> 1, "two" -> 2))
def match_vec(vec: Vector[Any]) {
vec match {
case Vector(_, _, 3) => println("Vector ends in 3!")
case Vector(3, _*) => println("Vector starts with three, has any number of elements!")
case Vector(_, "bar", _) => println("String Vector!")
case _ => println("Fallthrough case...")
}
}
scala> match_vec(vec1)
Vector ends in 3!
scala> match_vec(vec2)
Vector starts with three, has any number of elements!
scala> match_vec(vec3)
String Vector!
scala> match_vec(vec4)
Fallthrough case...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment