Skip to content

Instantly share code, notes, and snippets.

@niwinz
Last active August 29, 2015 13:56
Show Gist options
  • Save niwinz/9295439 to your computer and use it in GitHub Desktop.
Save niwinz/9295439 to your computer and use it in GitHub Desktop.
Auto currying for clojure (a la haskell but more explicit)
(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))
(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