Skip to content

Instantly share code, notes, and snippets.

@mkremins
Created January 27, 2014 23:07
Show Gist options
  • Save mkremins/8659188 to your computer and use it in GitHub Desktop.
Save mkremins/8659188 to your computer and use it in GitHub Desktop.
clojure.core/partial with explicit argument ordering
(defn partial* [f & static-args]
(fn [& dynamic-args]
(loop [args []
static-args static-args
dynamic-args dynamic-args]
(if-let [static (first static-args)]
(if (= static :%)
(if-let [dynamic (first dynamic-args)]
(recur (conj args dynamic) (rest static-args) (rest dynamic-args))
(throw (Exception. "too few arguments")))
(recur (conj args static) (rest static-args) dynamic-args))
(apply f args)))))
;; example
(let [greet (partial* str "Hello, " :% "!")]
(greet "Max")) ;; -> "Hello, Max!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment