Created
June 13, 2015 12:40
-
-
Save madvas/8caab2e03e4702a8a31c to your computer and use it in GitHub Desktop.
Clojure partial-right (Like a partial, but arguments are added to the end)
This file contains 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-right | |
"Takes a function f and fewer than the normal arguments to f, and | |
returns a fn that takes a variable number of additional args. When | |
called, the returned function calls f with additional args + args." | |
([f] f) | |
([f arg1] | |
(fn [& args] (apply f (concat args [arg1])))) | |
([f arg1 arg2] | |
(fn [& args] (apply f (concat args [arg1 arg2])))) | |
([f arg1 arg2 arg3] | |
(fn [& args] (apply f (concat args [arg1 arg2 arg3])))) | |
([f arg1 arg2 arg3 & more] | |
(fn [& args] (apply f (concat args (concat [arg1 arg2 arg3] more)))))) | |
(def example-data [{:name "John" :weight 80} {:name "Jeff" :weight 62}]) | |
(map (partial-right select-keys [:name]) example-data) | |
=> ({:name "John"} {:name "Jeff"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How funny, I was just looking for this!