Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "%&" mean in Clojure?

Tags:

clojure

I solved the 58th 4clojure problem using recursion but then I looked at another persons solution and found this:

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

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

like image 891
Johan Avatar asked Aug 23 '15 09:08

Johan


People also ask

What does the fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

What does a real fox say?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.

How can I identify a song by sound?

On your phone, touch and hold the Home button or say "Hey Google." Ask "What's this song?" Play a song or hum, whistle, or sing the melody of a song. Hum, whistle, or sing: Google Assistant will identify potential matches for the song.


1 Answers

It means the "rest arguments", as per this source.

Arguments in the body 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.

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

Some code to compare anonymous functions and their anonymous function shorthand equivalent :

;; a fixed number of arguments (three in this case)
(#(println %1 %2 %3) 1 2 3)
((fn [a b c] (println a b c)) 1 2 3)

;; the result will be :
;;=>1 2 3
;;=>nil

;; a variable number of arguments (three or more in this case) :
((fn [a b c & more] (println a b c 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 gives a list of the rest of the arguments.

like image 142
nha Avatar answered Oct 10 '22 10:10

nha