Created
September 19, 2014 22:25
-
-
Save wrobstory/a352ff715d7c94042574 to your computer and use it in GitHub Desktop.
Scala pattern match
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
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