3 ms·
One example is when you want to pass a function as param to another function. JavaScript example: Forced to use named function: var greaterThanFunction = func
by joubert 17y ago
One example is when you want to pass a function as param to another function.
JavaScript example:
Forced to use named function:
var greaterThanFunction = function(a, b) { return a > b; };
var filteredResults = filter(biglist, greatherThanFunction);
VS:
Anonymous:
var filteredResults = filter(biglist, function(a, b) { return a > b; });
The JavaScript example above is nice, but in Clojure anonymous lambdas are even nicer:
You can do things like:
(reduce #(+ %1 %2) (range 1000))
i.e., not only do you not have to name the function, you don't even have to name the variables.
----
Excerpt from http://clojure.org/reader http://clojure.org/reader
Anonymous function literal (#())
#(...) => (fn [args] (...))
where args are determined by the presence of argument literals taking the form %, %n or %&. % is a synonym for %1, %n designates the nth arg (1-based), and %& designates a rest arg. This is not a replacement for fn - idiomatic used would be for very short one-off mapping/filter fns and the like. #() forms cannot be nested.