read me

Understanding the Clojure -> macro

Sep 4, 2009

On page 109 of Paul Graham’s ANSI Common Lisp he describes a function compose taken from the Dylan programming language. The function described is very similar to the -> macro that often confuses many a new Clojurian. In order to better understand this little nasty, I’ve found it useful to think of it as an arrow indicating the flow of data from one function to another. That is, the follow:

(-> (Math/sqrt 25) int list)

Can literally be read:

  1. Take the result of (Math/sqrt 25)
  2. Feed it into the function int
  3. Feed that result into the function list

Graphically, this can be viewed as:

(Math/sqrt 25) --5.0--> (int 5.0) --5--> (list 5) 
=> (5)

Which expands into the following s-expression:

(list (int (Math/sqrt 25)))

When viewed this way, the -> macro can be said to “thread” 1 a sequence of forms into each in turn. This threading can be done within any form and is always stitched in as the second argument to the outermost s-expression. Clojurians will sometimes use the comma 2 as a visual marker for the stitch point:

(-> (/ 144 12) (/ ,,, 2 3) str keyword list)
=> (:2)

(-> (/ 144 12) (* ,,, 4 (/ 2 3)) str keyword (list ,,, :33))
=> (:32 :33)

Unfortunately, the documentation for -> does not do justice to its power, but hopefully things are a little more clear now.

-m


  1. Not the java.lang.Thread kind of thread. 

  2. Since commas are considered whitespace. The number of comma’s used as a marker is a matter of taste. 

Related posts:

  1. Clojure Golf episode 1 This is an attempt to have some fun with Clojure....
  2. De-chunkifying Sequences in Clojure At the first CAP CLUG meetup I gave a presentation...
  3. Clojure Golf – Episode 2: Largest Prime Factor In the last episode of Clojure Golf we saw some...
  4. Clojure’s :pre and :post One of the more exciting features of the upcoming Clojure...
  5. Baysick: A Scala DSL Implementing BASIC This post was featured on the Scala website. It is...

Related posts brought to you by Yet Another Related Posts Plugin.

3 Comments, Comment or Ping

  1. The final example produces (:32 :33)

  2. (-> (/ 144 12) (* ,,, 4 (/ 2 3)) str keyword (list ,,, :33))

    evaluates to

    (:32 :33)

    for me. :)

    But thanks for the explanation. I’d be a bit more direct than you were (you very diplomatic, I thought) and state that the doc string for -> sucks. You should submit a patch to it.

  3. @Hugo @Andera

    D’oh! Serves me right for not re-REPLing the problem after changing it during the course of the modifications. :(

    Thanks for the comments. -m

Reply to “Understanding the Clojure -> macro”

Additional comments powered by BackType