Allow anonymous functions in threading macro without extra parens?

Skip to first unread message

Rob Nikander

Apr 26, 2018, 10:26:05 AM4/26/18
to Clojure Dev
Why not? Sometimes it makes sense, and the extra parens seem ugly to me.  You loose the ability the thread the form `(fn <here> [arg ...] body ...)` but that's hardly useful.

For example, this macro adds a simple `if` branch to the `->` macro in clojure.core:

(defmacro -->
  "Variation of -> macro that allows anonymous functions without extra parentheses."
  [x & forms]
  (loop [x x, forms forms]
    (if forms
      (let [form (first forms)
            threaded (if (seq? form)
                       (if (or (= 'fn* (first form))
                               (= 'fn (first form)))
                         (with-meta `(~form ~x) (meta form)) ;; new branch for 'fn 'fn*
                         (with-meta `(~(first form) ~x ~@(next form)) (meta form)))
                       (list form x))]
        (recur threaded (next forms)))

(defn test []
  (--> 5
       #(* % %)
       (fn [x] (- x 1))
       (vector :b :c)))


Christophe Grand

Apr 27, 2018, 3:28:23 AM4/27/18
Or you can just use as-> (granted there’s no as->>)

(-> 5
  (as-> % (* % %))
  (as-> x (- x 1))
  (vector :b :c))

Alan Thompson

Apr 29, 2018, 8:34:31 PM4/29/18
to clojure-dev
I prefer the it-> macro for these situations:

    (it-> 5
      (inc it)
      (* it it)
      (- it 1)
      (vector :a it :c))     ;=> [:a 35 :c]

You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

Reply all
Reply to author
0 new messages