Erlang: parallel-map and parallel-foreach
In a post to erlang-questions last summer, Erlang on the niagara, Erlang super-hero Joe Armstrong posts a simple "parallel-map" implementation that he uses in place of some of his simple "iterative-foreach" calls to quickly achieve dramatic speed improvements by taking advantage of multiple processors. The post got me thinking, particularly in light of recent discussions on side-effects and purity, about the real differences and guarantees of such simple things as map
and foreach
.
Principally, map
is (1) supposed to be called with a function with no side effects, (2) guarantees order of return to match the input list, and (3) makes no guarantee on order of function calls, while foreach
is (1) used explicitly (and solely) for its side effects as (2) it has no return value (other than the atom ok
for success) and (3) guarantees the order of function calls to match the order of the input list. Now, Joe's code was for a parallel-map
implementation -- returning a list of results from the application of the given function, no guaranteed order of execution -- when it was actually being used to replace a foreach
statement. It happened to work out nicely that his code did not rely on any order of execution promises.
[More]