Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Clojure, what does `&` mean in function/macro signature?

I saw the usage of & in Clojure function signature like this (http://clojure.github.io/core.async/#clojure.core.async/thread):

(thread & body)

And this:

(doseq seq-exprs & body)

Does that means the function/macro can accept a list as variable? I also find * is often used to mean multiple parameters can be accepted, like this:

(do exprs*)

Does anyone have ideas about the difference between & and * in function/macro signature? Is there any documentation to explain this syntax?

like image 276
Hanfei Sun Avatar asked Feb 15 '16 15:02

Hanfei Sun


People also ask

What are symbols in Clojure?

In Common Lisp, a "symbol" is a location in memory, a place where data can be stored. The "value" of a symbol is the data stored at that location in memory. In Clojure, a "symbol" is just a name. It has no value.

What #: means in Clojure?

#: and #:: - Namespace Map Syntax Namespace map syntax was added in Clojure 1.9 and is used to specify a default namespace context when keys or symbols in a map where they share a common namespace.

How do you comment on Clojure?

#_ is the comment reader macro that instructs the Clojure reader to completely ignore the next form, as if it had never been written. No value is returned, so this comment is safe to use within an expression.


2 Answers

In clojure binding forms (let, fn, loop, and their progeny), you can bind the rest of a binding vector to a sequence with a trailing &. For instance,

(let [[a b & xs] (range 5)] xs) ;(2 3 4)

Uses of * and other uses of & are conventions for documenting the structure of argument lists.

like image 68
Thumbnail Avatar answered Oct 12 '22 23:10

Thumbnail


It means that there can be multiple parameters after the ampersand, and they will be seen as a seq by the function. Example:

(defn g [a & b]
  (println a b))

Then if you call:

 (g 1 2 3 4)

it will print out 1 (2 3 4) (a is 1, b is a sequence containing 2, 3 and 4).

like image 32
Diego Basch Avatar answered Oct 12 '22 23:10

Diego Basch