##### λ Tony's Blog λ

## scala.Option Cheat Sheet

Posted on January 16, 2008Many people who are coming in to Scala first encounter the Option type, which may be thought of (among other things) as a type-safe `null`

. They also encounter pattern matching as both a new and (relatively) powerful concept, but also one that is easy to understand. This leads to quite a lot of use of pattern matching and often excessively so in what I have observed.

Particularly with a type as trivial as `Option`

, it is almost always possible to do away with pattern matching by using a higher-order function. Use of this function is typically preferred over pattern matching as tighter code. In fact, it is important to observe that it is possible to encapsulate all forms of pattern matching over `Option`

with one simple higher-order function:

Then, all functions can be written in terms of this one and needn’t pattern match at all. For example, consider `Option.map`

:

In this post, I am going to give some common uses of pattern matching, which many developers might find themselves performing, followed by the use of a function that already exists on `Option`

that encapsulates that given form of pattern matching. If you find yourself using pattern matching in a form not listed below, but feel it could be abstracted, then chances are that such a function exists in the Scalaz extension to scala.Option.

I will use the identifier `foo`

below to denote any particular function, including many functions composed, for example, `foo(x)`

may represent the composition of two functions f and g: `f(g(x))`

. I also use the identifier `option`

to denote any value of the type `scala.Option`

.

I hope this helps :)

`flatMap`

This code is equivalent to:

`flatten`

This code is equivalent to:

`map`

This code is equivalent to:

`foreach`

This code is equivalent to:

`isDefined`

This code is equivalent to:

`isEmpty`

This code is equivalent to:

`forall`

This code is equivalent to:

`exists`

This code is equivalent to:

`orElse`

This code is equivalent to:

`getOrElse`

This code is equivalent to:

`toList`

This code is equivalent to:

`coflatMap`

^{1}

This code is equivalent to:

`duplicate`

^{2}

This code is equivalent to: