Where does the "flatmap that s***" idiomatic expression in Scala come from?

Scala

Scala Problem Overview


What is so powerful about flatmap that it deserves such a place in the Scala folklore?

Scala Solutions


Solution 1 - Scala

The story I heard was that two preeminent Scala programmers were pairing when one of them started writing some code like this:

option match {
    case Some ...

At which point the other said "What is this? Amateur hour? Flat map that shit!"

As to what's so powerful about flatMap, well... First, it's the fundamental monadic operator. That means it is a common operation shared by, for example, containers (such as Option, collections, etc), continuations, state, etc. Second, while you can de-construct an Option, that, as opposed to flatMap, is not a monadic operation, so it cannot be as widely applied. Also, it requires too much knowledge about the data you are manipulating.

Note: previously I said matching was slower than flatMap -- the opposite is true as a matter of fact, up to the most recent version of Scala at the time of this writing, 2.10.1.)

Solution 2 - Scala

The reasoning behind this phrase is that you can replace a lot of tedious if/then/else code you would write with calls to flatMap (and other higher order functions).

This is especially true for Options (see http://tonymorris.github.io/blog/posts/scalaoption-cheat-sheet/)

But it applies to other monads as well (although I have to admit, I don't exactly understand the details yet myself)

Imagine the situation where you have a collection for which you want to apply a function (or a series of functions) where each function might return null. When you actually use null you code will be riddled with null checks. But if you use Options instead of values, you can just flatmap the values with the desired functions, chaining the functions in the case of multiple functions and get a collection with just the results that aren't null, which in many cases is exactly what you want.

Since that description is rather convoluted the shorter advice "just flatmap that shit" established itself.

Solution 3 - Scala

The crucial thing about flatMap is that it's Scala's representation of the monadic bind operation. There are numerous tutorials on the web explaining the purpose of monads and why exactly they're so useful; James Iry has one which goes into some detail.

Solution 4 - Scala

Runar Bjarnason is the person you're looking for for the origin.

Realising why it's so powerful is something that can only come with time to be honest. The Option class is the best place to start for seeing how you would repeatedly flatMap a series of lookups (for example) into a final result.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuillaume BelroseView Question on Stackoverflow
Solution 1 - ScalaDaniel C. SobralView Answer on Stackoverflow
Solution 2 - ScalaJens SchauderView Answer on Stackoverflow
Solution 3 - ScalaSubmonoidView Answer on Stackoverflow
Solution 4 - ScalaSean ParsonsView Answer on Stackoverflow