score:58
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.
score:10
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.
score:12
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.
score:104
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.)
Source: stackoverflow.com
Related Query
- Where does the "flatmap that s***" idiomatic expression in Scala come from?
- Where does one place a scala file in the Play Framework 2.0 so that it is compiled as part of the default package?
- What is the efficient way to create Spark DataFrame in Scala with array type columns from another DataFrame that does not have an array column?
- How does the “scala.sys.process” from Scala 2.9 work?
- Does the order of alternatives in a Scala match expression matter in terms of performance?
- How does the Scala type checker know to prevent calling flatten on a List that is already flat
- Is it possible to implement `??` (a null coalescing operator from C#) in Scala that does not use reflection?
- Where does Scala store information that cannot be represented in Java?
- Why does the Scala compiler say that copy is not a member of my case class?
- Why does Spark ML NaiveBayes output labels that are different from the training data?
- How does the Scala type system know that cons + Nil is exhaustive?
- Where does the T, U, V convention for generic type params come from?
- groupByKey vs. aggregateByKey - where exactly does the difference come from?
- Does it make a difference to the debugger that it is Scala code I'm debugging?
- Why does Scala support multi-name definitions with an expression that is evaluated for each?
- What's the best way using Scala and SBT to create samples for a library that can be built from the SBT CLI?
- I'm thrilled that this scala snippet uses all of my processors to find the (correct) answer faster but... why does it do that?
- Scala: Is there a way where type aliases can be treated as distinct from the type that they alias?
- Scala List: why the IDEA prompts that Unused expression without side effects
- Scala case matching with cons. Where does this variable h come from?
- Idiomatic to find the first element in a collection that matches a given sub-type in Scala
- Why does the `is not a member of` error come while creating a list in scala using the :: operator
- Why does a scala try block allow variables from the enclosing scope to be redefined?
- When true, does Scala exists stop at the first element that satisfies the predicate?
- How the Scala script that reads 5G log file from network drive should be modified in order to read last x lines (like 'tail' in Unix)?
- Does the Scala 3 library have a Show typeclass that works with `derives`?
- How to resolve warnings field name does not match the regular expression in scala
- Is it possible in Scala to write an expression that placed in a function parameter, defaults the parameter value?
- Installing scala from coursier: where is the `scala` command?
- how can I write a function in scala called Order that takes one arguement: a list of Ints. And returns the same List of Ints from least to greatest
More Query from same tag
- Adding uncore package to a Chisel project
- How to consume Kafka to MySQL using Beam in Scala
- SbtOsgi AutoPlugin can't be loaded due to type mismatch error
- First Approach with Scala
- Flattening nested lists of the same type
- splitting contents of a dataframe column using Spark 1.4 for nested json data
- scala generic function return type
- Override a class variable from parent classes function
- IntelliJ IDEA complains about 'broken source path' for sbt-driven project
- Scala and JMM: number types performance
- Scala functional programming - transform java like code to functional code
- Play Framework how to sort collection in repeat() form helper?
- Compare complex objects with scala
- Testing values returned by 2 Futures are equal to each other
- Replace null in a column of a dataframe with other value
- Is it possible to run both tests based on specs2 and scalatest in the same project with SBT
- Launching scala uber-jar from scala console
- lsof always report offset equal to file size in OSX
- Scala: recursive match case?
- update date for a Dataframe and join with Kafka stream data live in spark
- Spark sessionization using data frames
- Get exact milliseconds from time stamp - Spark Scala
- val or object for immutable, final singleton object
- How to create schema in Spark with Scala if more than 100 columns in the input?
- Can sample weight be used in Spark MLlib Random Forest training?
- Output contents of DStream in Scala Apache Spark
- What is the concept of "weak conformance" in Scala?
- Creating some random case class dynamically using List[Strings] or Array[Strings]
- Define a common trait for types with different numbers of type parameters
- Compose "Insert...Select...Where" query