This is a very common sample in Elixir. The last argument must be a list (a specific list of keywords) and have a default value of [] . In Elixir, if the last argument is a list of keywords, you do not need to add [] to the list. For instance:
def do_lots(arg1, arg2, opts \\ []) do one = opts[:one] || :default two = opts[:two] # default to nil if it not passed # do something with arg1, arg2, one, and two end def my_func do arg1 |> do_lots(2) |> do_lots(49, one: :question) |> do_lots(99, two: :agent) |> do_lots(-1, one: 3, two: 4) end
Another option for handling variable-sized arguments is to pass them all in a list. This makes the arity 1 function, and you can process them as needed.
Finally, you can pass some or all of the arguments as a map. This gives you the opportunity to match the template on the map and create several functional sentences based on the keys transmitted on the map.
Keep in mind that you cannot easily stroke a match in a list of keywords, because they are order dependent.
source share