🎉 Black Friday Sale! Save 30% when you subscribe today.
The zip
operation is the next powerful operation in the functional trio. It is a generalization of the map
operation that allows you to perform map
-like transformations on multiple generic containers at once. The map
operation alone is incapable of doing that, and exploring this generalization leads us to some exciting new topics, such as multiple zip
implementations and context-independent computation.
The zip
operation came to our rescue in this episode where we need to be able to run multiple parsers on an input string, independent of each other. This was not possible using map
and flatMap
alone.
In the episode before this one we discovered that we could leverage Swift’s Decodable
protocol to instantly unlock randomness for any decodable type, including your own custom types. However, the random values produced weren’t the easiset to work with and was difficult to customize. So, we turned to the zip
operation to fix those problems, and came up with something surprising.
You can accomplish quite a bit with the map
and zip
operations alone, such as transforming the underlying value of a computation, or combining multiple computations into a single one. But there are still some things they cannot accomplish, such as combining multiple computations together in such a way that later computations depend on earlier computations. This property alone is what motivates us to introduce the flatMap
operation.