Parallelization of computation
Another nice benefit of having pure functions is that you can divide a computation into multiple small parts, distribute the workload, and assemble the result. It is possible to do so for any mapping, filtering, and folding operations. The function used for folding needs to be monoidal as we will see. Functions for mapping and filtering have no particular constraint besides purity.
Mapping does not have any particular constraint beside a pure function. Say you have four cores, or computers; you will only need to follow these steps:
- Split the array into four parts.
- Send a part to each core to do the mapping.
- Merge the results.
In this particular case, it might be slower than doing it on a single core as the merging operation ...