This lesson covers:

Function Composition

Let's make two aptly-named functions:

In []:
def f(s: String) = "f(" + s + ")"
In []:
def g(s: String) = "g(" + s + ")"

compose

compose makes a new function that composes other functions f(g(x))

In []:
val fComposeG = f _ compose g _
Out[]:
<function>
In []:
fComposeG("yay")
Out[]:
f(g(yay))

andThen

andThen is like compose, but calls the first function and then the second, g(f(x))

In []:
val fAndThenG = f _ andThen g _
Out[]:
<function>
In []:
fAndThenG("yay")
Out[]:
g(f(yay))

Currying vs Partial Application

case statements

So just what are case statements?

It's a subclass of function called a PartialFunction.

What is a collection of multiple case statements?

They are multiple PartialFunctions composed together.

Understanding PartialFunction

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.

See Also Effective Scala has opinions about PartialFunction.

In []:
val one: PartialFunction[Int, String] = { case 1 => "one" }
Out[]:
<function1>
In []:
one.isDefinedAt(1)
Out[]:
true
In []:
one.isDefinedAt(2)
Out[]:
false

You can apply a partial function.

In []:
one(1)
Out[]:
one

PartialFunctions can be composed with something new, called orElse, that reflects whether the PartialFunction is defined over the supplied argument.

In []:
val two: PartialFunction[Int, String] = { case 2 => "two" }
Out[]:
<function1>
In []:
val three: PartialFunction[Int, String] = { case 3 => "three" }
Out[]:
<function1>
In []:
val wildcard: PartialFunction[Int, String] = { case _ => "something else" }
Out[]:
<function1>
In []:
val partial = one orElse two orElse three orElse wildcard
Out[]:
<function1>
In []:
partial(5)
Out[]:
something else
In []:
partial(3)
Out[]:
three
In []:
partial(2)
Out[]:
two
In []:
partial(1)
Out[]:
one
In []:
partial(0)
Out[]:
something else

The mystery of case.

Last week we saw something curious. We saw a case statement used where a function is normally used.

In []:
case class PhoneExt(name: String, ext: Int)
In []:
val extensions = List(PhoneExt("steve", 100), PhoneExt("robey", 200))
Out[]:
List(PhoneExt(steve,100), PhoneExt(robey,200))
In []:
extensions.filter { case PhoneExt(name, extension) => extension < 200 }
Out[]:
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!