We had a meetup reading 2nd chapter of Reactive Programming with Scala and Akka last Friday.

The first half of this chapter explains what is functional programming in Scala. And the latter, through one of the most popular Scala library called Scalaz, the important concepts in functional programming; Functors, Applicative Functors, and Monads.

In this blog post, I will explain about my shallow understanding of those three handsome guys.

*Disclaimer: the below is not thorough nor correct definitions of those concepts at all. It simply is a starting point for understanding them.*

## Functors, Applicative Functors, and Monads, for the first step

If you don’t know very much about functors, applicative functional, and monads (like me as a Scala newbie), maybe a table below could help understand them.

Who are you? | does what, mainly | in which method |
---|---|---|

`Functor` |
maps a value `A` in some container `F` |
`map[B](f: A => B): F[B]` |

`Applicative` |
applies a method `A => B` in some container `F` to a value `A` in another container F |
`ap[A,B](f: => F[A => B]): F[B]` |

`Monad` |
flatMaps a value `A` in some container `F` |
`bind[A, B](f: A => F[B]): F[B]` |

Here, the word *container* means kind of an abstraction of a way of holding value. `Option`

, `List`

and `Future`

are good examples of them in Scala language.

type | how it holds value |
---|---|

`Option[T]` |
holds a value of type `T` (`Some(value)` ), or nothing (`None` ) |

`List[T]` |
holds multiple values of type `T` .(that is, homogeneous collection of type `T` ) |

`Future[T]` |
holds some concurrent block that will eventually return the value of type `T` , or `Throwable` as a failure result. |

*NOTE: Strictly speaking, container should actually be called a type class. See Wikipaedia for further explanations.*

And *Monad*s `flatMap`

(or `bind`

), this *flatten*s the nested container as a result of mapping.

Anyway for better understandings let’s check out their implemtation in Scalaz’s source code with some examples.

### Implementations & Examples

- Functor

1 | // From Scalaz, details omitted |

- Applicative (Applicative extends Apply)

1 | // From Scalaz, details omitted |

- Monad

1 | // From Scalaz, details omitted |

But what are these `XxxOps`

and `XxxSyntax`

??? This is kind of what we call **pimp my library** pattern in Scala, to add methods on target class or trait without modifying it, using the power of *implicit conversion*.

Its basic structure in Scalaz can be simplified like this:

1 | class Original |

## By the way,

Is there any easy good way to write some fancy operators like `"η"`

, `"⊛"`

, or `"∘"`

, without simply copy-and-pasting them? Once in a while I feel like to indulge in using them with a bit of fear about Gestaltzerfall…

## Other helpful resources

- http://eed3si9n.com/learning-scalaz/scalaz-cheatsheet.html
- https://github.com/arosien/scalaz-cheatsheets

## For those who want to dive more into functional programming…

Oneteam Inc. is hiring a Scala engineer for server-side development. Please visit HERE