1.3.0 permalink 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:1540 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/->>:
Used in 0 other vars

Comments top

No comments for ->>. Log in to add a comment.