Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created September 9, 2011 05:28
Show Gist options
  • Save alandipert/1205545 to your computer and use it in GitHub Desktop.
Save alandipert/1205545 to your computer and use it in GitHub Desktop.
positional partial
;;; 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