Skip to content

Instantly share code, notes, and snippets.

@francoisdevlin
Created November 5, 2009 22:01
Show Gist options
  • Save francoisdevlin/227454 to your computer and use it in GitHub Desktop.
Save francoisdevlin/227454 to your computer and use it in GitHub Desktop.
(def & comp)
(def p partial)
(defn- sym->key [x] (keyword (str x)))
(defn- sym-rep [x s-map]
(let [k (sym->key x)]
(if (contains? s-map k)
(s-map k)
x)))
(defn s-partial* [f p-map]
(let [m (eval `(meta (var ~f)))
args (:arglists m)
docs (:doc m)
applied-args (map
(&
(p concat (vector f))
(p map #(sym-rep % p-map))) args)
free-args (map
(&
vec
(p remove (&
(p contains? p-map)
sym->key)))
args)
captured-args (map
(&
vec
(p filter (&
(p contains? p-map)
sym->key))) args)
reduced-args ((&
(p sort-by (& count first))
(p apply hash-map))
(interleave free-args applied-args))
all-args ((&
(p concat '(fn))
(p map seq))
reduced-args)]
all-args))
(defn s-partial [f p-map]
(eval (s-partial* f p-map)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment