Created
September 9, 2011 05:28
-
-
Save alandipert/1205545 to your computer and use it in GitHub Desktop.
positional partial
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
;;; positional partial for giggles | |
;;; use #(foo %1 %2) instead | |
(defn- placeholder-indices | |
([template] | |
(placeholder-indices '_ template)) | |
([placeholder-sym template] | |
(->> template | |
(map vector (range)) | |
(filter (comp (partial = placeholder-sym) last)) | |
(map first)))) | |
(defn- merge-args [template values] | |
(let [slots-values (map vector (placeholder-indices template) values)] | |
(reduce (fn [args [slot val]] | |
(assoc args slot val)) | |
(vec template) | |
slots-values))) | |
(defn ppartial* [f template] | |
(fn [& args] | |
(apply f (merge-args template args)))) | |
(defmacro ppartial [f & args] | |
`(ppartial* ~f '~(vec args))) | |
(comment | |
((ppartial - _ 3) 10) ;=> 7 | |
((ppartial assoc _ :y _) {} 2) ;=> {:y 2} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment