(fn [x] (+ x 5)) and #(+ % 5) - The two are completely equivalent, the latter just uses the dispatch macro to make the code a bit more concise. For short functions, the #() syntax is usually preferable, and the (fn [x]) syntax is better for functions that are slightly longer. Also, if you have nested anonymous functions, you cannot use #() for both because of the ambiguity that this might cause.
(fn add-five [x] (+ x 5)) is the same as the previous two, except that it has the name: add-five. This can sometimes be useful, for example, if you need to make a recursive call to your function. *
(partial + 5) - In clojure, + is a variational function. This means that it can take any number of arguments. (+ 1 2) and (+ 1 2 3 4 5 6) are perfectly valid forms. partial creates a new function that is identical to + , except that the first argument is always 5. Because of this ((partial + 5) 3 3 3) valid. In this case, you could not use other forms.
* When making a recursive call from tail position, you must use recur , however this is not always possible.
dbyrne
source share