August 2018
Intermediate to advanced
380 pages
10h 2m
English
Special constructs in Scala are partial functions and pattern matching. For example, you can write expressions as follows:
scala> val str = "Foo"str: String = Fooscala> str match { | case "Bar" => println("It is a bar") | case "Foo" => println("It is a foo") | }It is a foo
More complex pattern matching is also possible. For example, given a list, we can match on its head and tail, or its head and its second argument and its tail:
scala> val list = List(1, 2, 3, 4, 5)list: List[Int] = List(1, 2, 3, 4, 5)scala> list match { | case e1 :: e2 :: rest => e1 + e2 | }<console>:13: warning: match may not be exhaustive.It would fail on the following inputs: List(_), Nil list match { ^res10: Int = 3
In fact, we can perform pattern matching ...
Read now
Unlock full access