Understanding Clojure's map and pmap
Let's be honest - Part of the reason you're using Clojure is the higher order functions like . They're great, composable, and describe what you're trying to do instead of defining what to do - this keeps code simple. Maybe you've been playing with map
for a while now and have even tried using . If you did, you probably noticed that pmap
is often slower than map
, and if you want to know why - read on.
What is map
?
Before we get into pmap
let's go ahead and clarify our understanding of map
. There's actually several neat use cases for map
, some that you may not be familiar with. From their source code, "Returns a lazy sequence consisting of the result of applying f to the set of first items of each coll, followed by applying f to the set of second items in each coll, until any one of the colls is exhausted. Any remaining items in other colls are ignored. Function f should accept number-of-colls arguments. Returns a transducer when no collection is provided."
If you've used map
before, you're probably aware of everything in the doc string with the possible exception of the bolded part - returning a transducer. We'll also talk about the multiple collections use case before moving onto pmap
as well.
The rest of this post contains code examples which can best be viewed on my blog at https://www.bradcypert.com/understanding-clojures-map-pmap/