Allow anonymous functions in threading macro without extra parens?

124 views
Skip to first unread message

Rob Nikander

unread,
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)))
      x)))

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

Rob

Christophe Grand

unread,
Apr 27, 2018, 3:28:23 AM4/27/18
to cloju...@googlegroups.com
Or you can just use as-> (granted there’s no as->>)

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

Alan Thompson

unread,
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 clojure-dev+unsubscribe@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at https://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages