Skip to content

Instantly share code, notes, and snippets.

@madvas
Created June 13, 2015 12:40
Show Gist options
  • Save madvas/8caab2e03e4702a8a31c to your computer and use it in GitHub Desktop.
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)
(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"})
@madis
Copy link

madis commented Jun 1, 2023

How funny, I was just looking for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment