Cross-language comparison: function map and similar

Steve D'Aprano steve+python at pearwood.info
Wed Aug 16 10:53:42 EDT 2017


Over in another thread, we've been talking about comprehensions and their
similarities and differences from the functional map() operation.

Reminder:

map(chr, [65, 66, 67, 68])

will return ['A', 'B', 'C'].

My questions for those who know languages apart from Python:

Are there language implementations which evaluate the result of map() (or its
equivalent) in some order other than the obvious left-to-right first-to-last
sequential order? Is that order guaranteed by the language, or is it an
implementation detail?

Standard library functions implementing an explicitly "parallel map"
or "threaded map" are also relevant. (Less interested in third-party libraries,
unless they're practically a standard for the language in question.)

E.g. given the map above, Blub language might promise to evaluate chr(65),
chr(66), chr(67) and chr(68) in parallel if your computer has four cores.

Or some Blub language implementation may unroll the above map to the equivalent
of this pseudo-code:

array = [None, None, None, None]
array[3] = chr(68)
array[2] = chr(67)
array[1] = chr(66)
array[0] = chr(65)


I'm not terribly interested in hypothetical "Blub language doesn't specify
evaluation order, so some implementation could do this". I'm more interested in
actual concrete examples of mapping implementations which don't operate in the
obvious first-to-last order. For example, Fortran has "do concurrent":

do concurrent(i = 1:n)
  a(i) = a(i+m)+b(i)
end do

which tells the compiler that it can run the iterations in any order, or
parallelize them and run them in parallel across threads, etc:

https://www.hpcwire.com/2015/04/06/compilers-and-more-the-past-present-and-future-of-parallel-loops/

Wikipedia mentions a few specialised parallel processing languages which have
equivalent forms of parallel map:

https://en.wikipedia.org/wiki/Map_%28parallel_pattern%29

Wolfram Language (Mathematica?) has a parallel map:

http://reference.wolfram.com/language/ref/ParallelMap.html

as does Clojure:

https://clojuredocs.org/clojure.core/pmap


Any others?

I'm especially interested in cases of languages where their regular map()
function is performed in parallel, or out of order, *without* the performance
hit that the Clojure docs warn about:

"Only useful for computationally intensive functions where the time of f
dominates the coordination overhead."






-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list