This lesson covers:
Let’s make two helpful functions:
scala> def addUmm(x: String) = x + " umm" addUmm: (x: String)String scala> def addAhem(x: String) = x + " ahem" addAhem: (x: String)String
compose calls the second function and then the first function.
compose is f(g(x))
scala> val ummThenAhem = addAhem(_).compose(addUmm(_))
ummThenAhem: (String) => String = <function1>
scala> ummThenAhem("well")
res0: String = well umm ahem
andThen calls the first function and then the second.
andThen is g(f(x))
scala> val ahemThenUmm = addAhem(_).andThen(addUmm (_))
ahemThenUmm: (String) => String = <function1>
scala> ahemThenUmm("well")
res1: String = well ahem umm
It’s a subclass of function called a PartialFunction.
They are multiple PartialFunctions composed together.
A function works for every argument of the defined type. In other words, a function defined as (Int) => String takes any Int and returns a String.
A Partial Function is only defined for certain values of the defined type. A Partial Function (Int) => String might not accept every Int.
isDefinedAt is a method on PartialFunction that can be used to determine if the PartialFunction will accept a given argument.
Note PartialFunction is unrelated to a partially applied function that we talked about earlier.
scala> val one: PartialFunction[Int, String] = { case 1 => "one" }
one: PartialFunction[Int,String] = <function1>
scala> one.isDefinedAt(1)
res0: Boolean = true
scala> one.isDefinedAt(2)
res1: Boolean = false
You can apply a partial function.
scala> one(1) res2: String = one
PartialFunctions can be composed with something new, called orElse, that reflects whether the PartialFunction is defined over the supplied argument.
scala> val two: PartialFunction[Int, String] = { case 2 => "two" }
two: PartialFunction[Int,String] = <function1>
scala> val three: PartialFunction[Int, String] = { case 3 => "three" }
three: PartialFunction[Int,String] = <function1>
scala> val wildcard: PartialFunction[Int, String] = { case _ => "something else" }
wildcard: PartialFunction[Int,String] = <function1>
scala> one orElse two orElse three orElse wildcard
res23: PartialFunction[Int,String] = <function1>
scala> res23(5)
res24: String = something else
scala> res23(3)
res25: String = three
scala> res23(2)
res26: String = two
scala> res23(1)
res27: String = one
scala> res23(0)
res28: String = something else
Last week we saw something curious. We saw a case statement used where a function is normally used.
scala> case class PhoneExt(name: String, ext: Int)
defined class PhoneExt
scala> val extensions = List(PhoneExt("steve", 100), PhoneExt("robey", 200))
extensions: List[PhoneExt] = List(PhoneExt(steve,100), PhoneExt(robey,200))
scala> extensions.filter { case PhoneExt(name, extension) => extension < 200 }
res0: List[PhoneExt] = List(PhoneExt(steve,100))
Why does this work?
filter takes a function. In this case a predicate function of (PhoneExt) => Boolean.
A PartialFunction is a subtype of Function so filter can also take a PartialFunction!