Scala for Multicore

UPMARC Multicore Computing Summer School 2011

Philipp Haller, EPFL and Stanford University

Code Example: Partial Functions

Experimenting with partial functions in Scala's interpreter (REPL). Below is both the code entered into the REPL as well as the REPL's output.

Note: This example relies on the previous Pattern Matching example.

val pf: PartialFunction[Tree[Int], Boolean] = {
 case Empty => false
 case Binary(el, l, r) => el % 2 == 0
}

pf(Empty)
res2: Boolean = false

pf.apply(Empty)
res3: Boolean = false

pf(Binary(4, Empty, Empty))
res4: Boolean = true

pf(Binary(5, Empty, Empty))
res5: Boolean = false