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/

要查看或添加评论,请登录

Brad Cypert的更多文章

  • Modern times call for modern languages

    Modern times call for modern languages

    Computing has only been around for roughly 70 years. While that may seem like a long time, it's actually an extremely…

  • Communicating in Code

    Communicating in Code

    Writing elegant code is an extremely difficult task. In some sense, you can think of it as writing a paper in a mix of…

    1 条评论
  • Dart all the way down

    Dart all the way down

    If you're using an app and it's built with a cross-platform framework, chances are high that it's either built with…

    1 条评论
  • Type Systems

    Type Systems

    This issue is going to deviate a tiny bit from the more theoretical format. Instead, we're going to dig into why I…

  • The one on burnout

    The one on burnout

    I really, really enjoy writing code. It's both my hobby and my career, and in that sense I am extremely fortunate to be…

  • How to write less code without losing your mind

    How to write less code without losing your mind

    Today's issue is near and dear to my heart. As I've moved up in my career (in the "away from code" sense, not the…

  • Shortening the SDLC

    Shortening the SDLC

    One thing that has been on my mind a lot, recently, is how the SDLC is effected by modern development practices like…

    4 条评论
  • Read Only Memories: Optimizing Software

    Read Only Memories: Optimizing Software

    Hi! I wanted to give you some insight into what exactly Read Only Memories is. The goal with Read Only Memories is to…

    4 条评论
  • Boxing things in Rust

    Boxing things in Rust

    Rust's memory management can feel a bit intimidating for most developers. Rust gives you some control over whether…

  • Introduction to Asynchronous Processes in Clojure

    Introduction to Asynchronous Processes in Clojure

    I've been writing about Clojure for a while now, and after some requests, decided to write about Core.Async.

社区洞察

其他会员也浏览了