Last active
December 15, 2015 10:18
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(deftest customisable-threading | |
(is (= "hello" (->>> :a (% {:a :h}) (name %) (cons % "ello") (clojure.string/join %))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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