May 2018
Beginner to intermediate
290 pages
6h 43m
English
One of the surprising things about Clojure functions is that you can mix and match the variadic & into a multi-arity function if you are careful. Here’s us being careful:
| | (defn one-two-or-more |
| | ([a] (println "One arg:" a)) |
| | ([a b] (println "Two args:" a b)) |
| | ([a b & more] (println "More than two:" a b more))) |
We just need to keep in mind that Clojure is sharp enough not to let us define a multi-arity function with overlapping arguments. For example, if we had written that last function as
| | ;; Oh no! |
| | |
| | (defn one-two-or-more |
| | ([a] (println "One arg:" a)) |
| | ([a b] (println "Two args:" a b)) |
| | ([& more] (println "More than two:" more))) |
then we wouldn’t get past the Clojure compiler. The problem is ...
Read now
Unlock full access