A Functional Toolkit

Since so much of Clojure programming revolves around creating, combining, and using functions, it’s unsurprising that the language provides a fair number of functions aimed at easing the job.

Take, for example, the apply function. It tackles the surprisingly common situation where you have a function and the arguments that you want to call that function with in a collection. In other words, instead of having this:

 (+ 1 2 3 4) ​; Gives you 10.

what if you had the function (+ in this case) and the arguments, like this:

 (​def​ the-function +)
 (​def​ args [1 2 3 4])

Enter apply. You supply a function and a collection of arguments, and apply will call that function with the arguments, returning the result. Armed with apply ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.