What does "% &" mean in Clojure?

I solved the 58th 4clojure problem using recursion, but then I looked at the solution of others and found the following:

(fn [& fs] (reduce (fn [fg] #(f (apply g %&))) fs)) 

Which is more elegant than my decision. But I don't understand what %& means? (I understand what % means, but not when it is combined with & ). Can anyone shed some light on this?

+8
clojure
source share
1 answer

This means "arguments of peace," according to this source .

Arguments in the body are determined by the presence of the argument literals take the form%,% n, or% &. % is a synonym for% 1,% n denotes the nth arg (1-based) and% &.

Note that the & syntax resembles the & more arguments in function parameters ( see here ), but &% works inside the abbreviation of an anonymous function .

Some code to compare anonymous functions and their anonymous abbreviated equivalent of a function:

 ;; a fixed number of arguments (three in this case) (#(println %1 %2 %3) 1 2 3) ((fn [abc] (println abc)) 1 2 3) ;; the result will be : ;;=>1 2 3 ;;=>nil ;; a variable number of arguments (three or more in this case) : ((fn [abc & more] (println abc more)) 1 2 3 4 5) (#(println %1 %2 %3 %&) 1 2 3 4 5) ;; the result will be : ;;=>1 2 3 (4 5) ;;=>nil 

Note that the & more or %& syntax provides a list of the remaining arguments.

+10
source share

All Articles