Last active
August 29, 2015 13:56
-
-
Save niwinz/9295439 to your computer and use it in GitHub Desktop.
Auto currying for clojure (a la haskell but more explicit)
This file contains 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 defc | |
[identifier bindings & body] | |
(let [n# (count bindings)] | |
`(def ~identifier | |
(fn [& args#] | |
(if (< (count args#) ~n#) | |
(apply partial ~identifier args#) | |
(let [myfn# (fn ~bindings ~@body)] | |
(apply myfn# args#))))))) | |
(defc sum-three-numbers | |
[num1 num2 num3] | |
(+ num1 num2 num3)) |
This file contains 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
(sum-three-numbers 1 2 3) | |
;; -> 6 | |
(sum-three-numbers 1) | |
;; -> #<core$partial$fn__4190 clojure.core$partial$fn__4190@8a20568> | |
(sum-three-numbers 1 2) | |
;; -> #<core$partial$fn__4192 clojure.core$partial$fn__4192@892c746> | |
((sum-three-numbers 1) 2) | |
;; -> #<core$partial$fn__4192 clojure.core$partial$fn__4192@4be0ae98> | |
(((sum-three-numbers 1) 2) 3) | |
;; -> 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment