Clojure – Variadic Functions

Clojure variadic functions

In this guide, we will discuss Clojure Variadic Functions. Variadic functions are functions that take varying number of arguments (some arguments are optional). Function can also specify the ‘&’ ampersand symbol to take in an arbitrary number of arguments.

Following example shows how this can be achieved.

(defn demo 
   [message & others]
   (str message (clojure.string/join " " others)))

The above function declaration has the ‘&’ symbol next to the argument others, which means that it can take an arbitrary number of arguments.

If you invoke the above function as

Example

(demo "Hello" "This" "is" "the" "message")

Output

Following will be the output.

“HelloThis is the message”

The ‘clojure.string/join’ is used to combine each individual string argument, which is passed to the function.

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply