Skip to content

Instantly share code, notes, and snippets.

@oliyh
Last active December 15, 2015 10:18
Show Gist options
  • Save oliyh/5244280 to your computer and use it in GitHub Desktop.
Save oliyh/5244280 to your computer and use it in GitHub Desktop.
A threading macro for Clojure for use when threading functions which have varying signatures.
(deftest customisable-threading
(is (= "hello" (->>> :a (% {:a :h}) (name %) (cons % "ello") (clojure.string/join %)))))
(defmacro ->>>
"Threads the expr through the forms. Inserts x in the place of % 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 in place of % in the second form, and so on."
([x form] (if (seq? form)
(with-meta `((fn [~(symbol "%")] (~@form)) ~x) (meta form))
(list form x)))
([x form & more] `(->>> (->>> ~x ~form) ~@more)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment