You're viewing version 1.2.0 of ->>. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16
  • (->> x form)
  • (->> x form & more)
Threads the expr through the forms. Inserts x as the
last item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
last item in second form, etc.

2 Examples top

  • ;; An example of using the "thread-last" macro to get
    ;; the sum of the first 10 even squares.
    user=> (->> (range)
                (map #(* % %))
                (filter even?)
                (take 10)
                (reduce +))
    1140
    
    ;; This expands to:
    user=> (reduce +
                   (take 10
                         (filter even?
                                 (map #(* % %)
                                      (range)))))
    1140
    
  • user=> (def c 5)
    user=> (->> c (+ 3) (/ 2) (- 1))                          
    3/4
    
    ;; and if you are curious why
    user=> (use 'clojure.walk)
    user=> (macroexpand-all '(->> c (+ 3) (/ 2) (- 1)))
    (- 1 (/ 2 (+ 3 c)))
    
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:1296 top

(defmacro ->>
  "Threads the expr through the forms. Inserts x as the
  last item in the first form, making a list of it if it is not a
  list already. If there are more forms, inserts the first form as the
  last item in second form, etc."
  {:added "1.1"} 
  ([x form] (if (seq? form)
              (with-meta `(~(first form) ~@(next form)  ~x) (meta form))
              (list form x)))
  ([x form & more] `(->> (->> ~x ~form) ~@more)))
Vars in clojure.core/->>: defmacro first list meta next seq? with-meta concat seq

Comments top

3 comment(s) for ->>.

I'm getting: Exception in thread "main" java.lang.Exception: Unable to resolve symbol: ->> in this context (11.clj:25)

It's also extremely hard to Google this method. It'd be nice if there was a non-symbol name for this that one could search for.

Update

The name of this operator is called a thrush.

See also -> which is similar but threads the first expr as the second argument of the forms.

My error was due to using an old version of Clojure. I was using 1.0.0.

Log in to add a comment.