Skip to content

Instantly share code, notes, and snippets.

@javipacheco
Last active August 29, 2015 14:25
Show Gist options
  • Save javipacheco/08270f5b402f32044987 to your computer and use it in GitHub Desktop.
Save javipacheco/08270f5b402f32044987 to your computer and use it in GitHub Desktop.
Paranoia
List<String> list = Arrays.asList("a", "b", "c", "d");
match(list)
.when(nil()).then(() -> System.out.println("Empty List"))
.when(headNil(eq("b"))).then(() -> System.out.println("Singleton List of 'b'"))
.when(headNil(any())).then(head -> System.out.println("Singleton List of " + head))
.when(headTail(any(), any())).then(
(head, tail) -> System.out.println("head: " + head + " Remaining: " + tail))
.doMatch();
val list = List("a", "b", "c", "d")
list match {
case Nil => println("Empty List")
case List("b") => println("Singleton List of 'b'")
case List(head) => println("Singleton List of " + head)
case head :: tail => println("head: " + head + " Remaining: " + tail)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment