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 args + additional args.
user=> (def to-english (partial clojure.pprint/cl-format nil "~@(~@[~R~]~^ ~A.~)")) #'user/to-english user=> (to-english 1234567890) "One billion, two hundred thirty-four million, five hundred sixty-seven thousand, eight hundred ninety"
user=> (def hundred-times (partial * 100)) #'user/hundred-times user=> (hundred-times 5) 500 user=> (hundred-times 4 5 6) 12000 user=> (def add-hundred (partial + 100)) #'user/add-hundred user=> (add-hundred 5) 105
(def subtract-from-hundred (partial - 100)) user=> (subtract-from-hundred 10) ; same as (- 100 10) 90 user=> (subtract-from-hundred 10 20) ; same as (- 100 10 20) 70
Like clojure.core/partial for functions that take their primary arg
(defn partial
"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 args + additional args."
{:added "1.0"}
([f arg1]
(fn [& args] (apply f arg1 args)))
([f arg1 arg2]
(fn [& args] (apply f arg1 arg2 args)))
([f arg1 arg2 arg3]
(fn [& args] (apply f arg1 arg2 arg3 args)))
([f arg1 arg2 arg3 & more]
(fn [& args] (apply f arg1 arg2 arg3 (concat more args)))))
Comments top
No comments for partial. Log in to add a comment.