Created
January 27, 2014 23:07
-
-
Save mkremins/8659188 to your computer and use it in GitHub Desktop.
clojure.core/partial with explicit argument ordering
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
(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