Tutorial by Examples

Partial functions are often used to define a total function in parts: sealed trait SuperType case object A extends SuperType case object B extends SuperType case object C extends SuperType val pfA: PartialFunction[SuperType, Int] = { case A => 5 } val pfB: PartialFunction[SuperType,...
While partial function are often used as convenient syntax for total functions, by including a final wildcard match (case _), in some methods, their partiality is key. One very common example in idiomatic Scala is the collect method, defined in the Scala collections library. Here, partial functions ...
Scala has a special type of function called a partial function, which extends normal functions -- meaning that a PartialFunction instance can be used wherever Function1 is expected. Partial functions can be defined anonymously using case syntax also used in pattern matching: val pf: PartialFunction...
Partial functions are very common in idiomatic Scala. They are often used for their convenient case-based syntax to define total functions over traits: sealed trait SuperType // `sealed` modifier allows inheritance within current build-unit only case object A extends SuperType case object B exten...
These three map functions are equivalent, so use the variation that your team finds most readable. val numberNames = Map(1 -> "One", 2 -> "Two", 3 -> "Three") // 1. No extraction numberNames.map(it => s"${it._1} is written ${it._2}" ) // 2....

Page 1 of 1