Created
April 15, 2013 17:26
-
-
Save bendlas/5389764 to your computer and use it in GitHub Desktop.
A macro that allows to define a function with one arity, that gets curried.
You can call the curried function with any number of arguments at a time.
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
(ns util.curry) | |
(defn feed-curry [f arg args] | |
(if (pos? (count args)) | |
(recur (f arg) (first args) (rest args)) | |
(f arg))) | |
(defn- curried-fn-body [name args body] | |
{:pre [(pos? (count args))]} | |
(let [arg (first args) | |
fname (gensym (str name "_curry" (count args) \_))] | |
`(fn ~fname | |
([arg# & rst#] (feed-curry ~fname arg# rst#)) | |
([~arg] ~@(if (= 1 (count args)) | |
body | |
[(curried-fn-body name (rest args) body)]))))) | |
(defmacro defc | |
"Define curried function" | |
[name args & body] | |
`(def ~name ~(curried-fn-body name args body))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment