zulooprofiles.blogg.se

Flatmap scala
Flatmap scala











flatmap scala

The problem with this is, if capital is None, I get a NullPointerException – and rightly so. Since capital is an Option, I have to treat it as if it were a list:Į // doesn't compile it returns an Option of whatever type is B, because map preserves the shape of the monad I applied it toįollowing the same logic, flatMap returns Option – and not Option] – because it flattens the list, so that:.it takes a Boolean (remember I’m mapping through Option).The signature for map is telling me that what map does here is: Map (f: (Boolean => B)) // returns OptionįlatMap (f: (Boolean => Option)) // returns Option Since london.hasRiver is a list, the signatures for map and flatMap are Val england = Country("England", Some(london)) To conclude, here’s some mock classes to play with mostly as a boring exercise:Ĭase class Country(name: String, capital: Option)Ĭase class City(name: String, hasRiver: Option)

Flatmap scala code#

On the other hand, it means that the code will always succeed, and it’s going to be harder to handle the case when the lookup fails. The latter method is especially neat because I can chain multiple calls to functional combinators which will only be executed if some value is present. UserRepository.findById(2).map(_.age) // Some(33) UserRepository.findById(2).foreach(user => println(user.name)) I want to log a warning message and exit the app. This is useful especially when I want to be explicit about what to do if the lookup fails, e.g. Using the curly brackets syntax for map/flatMap Nested flatMap (innerList => innerList map (value => value * 2)) Nested flatMap (inner => inner map (value => value * 2))Ĭhaining multiple map and flatMap calls can become hard to read, so I can spread it over multiple lines. To be fair, this last example is a bit weird because I could actually just flatten the nested list without “unrolling” it first if I did the unrolling is because a) it makes for a good example, forcing me to reason about the inner structure of the object I’m dealing with and b) it actually becomes necessary if I want to do something with the inner objects, e.g. Nested map ((x: List) => x map ((n: Int) => n)) flatten The same can also be expressed by manually “unrolling” the nested list, and flattening the result: This takes a list of int, returns a list of int, and flattens the result. Scala> List(1,2,3) flatMap (x => Some(x))Įxample: using flatMap to flatten a nested list fails: expected Int => GenTraversableOnce, In fact if I try to feed to flatMap a function that does not return a GenTraversableOnce it won’t compile, since you can’t possibly flatten a flat list! flattens the result into the original list.returns a sequence for each element in the list.The signature for flatMap tells us how it works, namely by applying a function that Numbers filter (p => p % 2 = 1) map (n => n * 2)ĭef flatMap(f: (A) => GenTraversableOnce): Traversable

flatmap scala

Map() evaluates a function over each element in the list, returning a list with the same number of elements.Ī for comprehension is a pretty way of saying the same: iterate through a list, and apply some function to every item.įor (n p % 2 = 1) map ((n : Int) => n * 2) A (not so) quick intro/recap to map, flatMap and Options in Scala.













Flatmap scala