Multimethods
Multi-arity and variadic functions are great for those situations where you want to build functions that are less picky about the number of arguments they will accept. Sometimes what you want is to be able to vary your function’s behavior based on some other aspect of the values that get passed to it.
For example, imagine that our system was getting book data from various sources, in different formats. Some books still look like the maps we’ve been using:
| {:title "War and Peace" :author "Tolstoy"} |
While others come in maps with different keys:
| {:book "Emma" :by "Austen"} |
And still others are encoded in vectors:
| ["1984" "Orwell"] |
Clearly we could handle all of this by writing a function to convert the odd-ball formats ...
Get Getting Clojure now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.