Multi-arity function can respond to different sets of arguments. Single-arity takes one set of arguments, multy-arity takes more than one set of arguments
To reduce redundancy, the function can call itself when its called with fewer arguments
Function that takes infinite arguments, uses &, called varargs or variadic functions, they have single argument set
Variadic function has
Multimethod
A mechanism to implement dispatching of functions. Similar to RouteToLabel.
Our requirement is to take a message, determine its format, i.e., whether it’s xml, json, dfdl or none and call a function that implements the specific transformation logic to handle it.
First, decide what you want to call the common function. Here we’ll call it process-message.
We’ll define a special defmulti which maps this common method name to a special dispatcher which we call a router. The method name comes first and then the dispatcher function name next.
Next, define a regular function that acts as a dispatcher which takes an argument and returns a label. Here, the router function looks at a message and figures out the format of the message, whether it’s xml, json, dfdl and returns a label that identifies the format. It has a catch all label of default.
Then define a method for each of the labels and the default label.
Here is how you call it
Defining a new router function is not necessary. We just need a function that takes the same input and gives back a label.
Suppose the message is like this { :type :cobol :payload "old cobol"}.
Remember label is a function that returns its value in the collection.
That is, (:type message) gives back :cobol
So, the label can be used as the dispatch function.
Here’s a variation of the process-message called process-message-type.
The multifunction that takes the label :type instead of router as the dispatch function
A corresponding implementation for the type :cobol