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:

  1. Split the array into four parts.
  2. Send a part to each core to do the mapping.
  3. Merge the results.

In this particular case, it might be slower than doing it on a single core as the merging operation ...

Get Functional PHP now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.