raulraja
01/24/2018, 6:39 PMflatMap at some point and that was probably when introduced to Rx or Collections. Those `flatMap`s concretions have their foundation in Monad#flatMap which is essentially sequencing over different values of the same container. Works in Option, List, Observable etc... and generally for any data type where you can express: fun <B> flatMap(f: (A) -> F<B>): F<B> where F here means any type such as Option, List etc...
That is why FP makes so much emphasis in foundations first, and that is because it gives you the intuition to understand all implementations of flatMap for all types since what they share in common is the essence that you can get a new F<B> value once you have an A from the previous computation.
In Kotlin when we say:
listOf(1,2,3).flatMap { a ->
listOf(4, 5, 6).map { it + 1 }
}
//[5, 6, 7, 5, 6, 7, 5, 6, 7]
We are saying than in order to obtain the final list it depends on the result of computing the first one listOf(1, 2, 3). That is why Monad is used to express dependencies of either the value of the first container or the order in which operations are performed. In contrast Applicative is used when we don't have dependencies between the values in the containers.