Currying, partially applying arguments to functions, in Clojure.
partial accepts a function and some, but not all, of the arguments for that function. It returns a new function which accepts the remaining arguments. This is known as currying.
Using the function
<, which accepts two arguments, as an example we can use
partial to apply to the first argument and return a new function which accepts the second.
< accepts two arguments:
(< 25 10) ; false
We can create a new function with just
(partial < 25) ; fn
If we define a var and store the function returned by
partial we can use it as follows:
(def greater-than-25 (partial < 25)) (greater-than-25 10) ; false (greater-than-25 100) ; true
partial returns a function it can be used with high-order functions such as
(map (partial * 10) [10 20 30 40]) ; 100 200 300 400
map accepts a function (which accepts one argument) and a collection, the function is applied to each item in the collection.