The flatMap
operation completes the functional trio, and gives us the power to sequence computations. Where map
allowed us to transform a single computation, and zip
allowed us to transform many independent computations at once, flatMap
instead allows us to run one computation after another so that each subsequent computation can depend on the result of the previous one.
When exploring ways to cook up complex form of randomness, such as randomly sized arrays of random values, we were naturally led to the concept of flatMap
. It was exactly what we needed to allow new random values to depend on previous random values.
Now that you have the functional trio toolkit under your belt, it’s time to level up your understanding of how functional operations can be discovered. In this episode we introduce the idea of contravariance, and show that it can be viewed through a functional lens by taking the signature of the map
operation and giving it a little flip!