Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clojure partial-right (Like a partial, but arguments are added to the end)
(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
You can’t perform that action at this time.