Pattern Match Keyword List in Function Def
TIL that you can pattern match a keyword list in a function definition.
Sometimes you'll receive the last argument as an empty keyword list; in this case, we're calling it opts
. You can pattern match key-values by defining the shape in a function.
Note: As a TIL reader pointed out, pattern matching Keywords will make your function args order dependent. The following would not work:
If you need them to be order independent, use a map
or just match on single argument, then check for each option appropriately with the Keyword module.
https://elixir-lang.org/getting-started/keywords-and-maps.html
Tweet