Hanfei Sun
Hanfei Sun

Reputation: 47051

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?

Upvotes: 10

Views: 3487

Answers (3)

slk500
slk500

Reputation: 781

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

There is no real difference between exprs* and & body. The former some sort of informal description inspired by regular expression and the latter more how it is often written in the code.

Upvotes: 1

Thumbnail
Thumbnail

Reputation: 13473

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.

Upvotes: 14

Diego Basch
Diego Basch

Reputation: 13059

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).

Upvotes: 16

Related Questions