May 2018
Beginner to intermediate
290 pages
6h 43m
English
Multi-arity functions are fine for functions like greet, functions that might take one or two or some other definite number of arguments. But what if you want to write a function that can deal with a completely arbitrary number of arguments? The good news is that you can get this done with the strategic placement of an & in your argument list.
Here, for example, is a function that will take any number of arguments and print them all out:
| | (defn print-any-args [& args] |
| | (println "My arguments are:" args)) |
When someone calls print-any-args, the arguments show up in the args parameter—the one after the ampersand—as a collection. So if you call print-any-args like this:
| | (print-any-args 7 true nil) |
you will see this: ...
Read now
Unlock full access