Skip to content

Instantly share code, notes, and snippets.

@bendlas
Created April 15, 2013 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendlas/5389764 to your computer and use it in GitHub Desktop.
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.
(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