A library of keyword-list handling functions to complement Keyword
Elixir keyword lists as a common representation of optional arguments to functions feels a bit clunky. You may find yourself writing something like:
def update_user_details(opts \\ []) do
opts = Keyword.validate!(opts, [:name, :email, role: :guest, gender: :unspecified])
name = Keyword.fetch!(opts, :name)
email = Keyword.fetch!(opts, :email)
...
end
The intent of Kword is to enable list matching (there's an order of parameters in the second argument) and to write instead:
def update_user_details(opts \\ []) do
[name, email | _rest] = Kword.extract!(opts, [:name, :email, role: :guest, gender: :unspecified])
...
end
If you want to ensure required parameters are in fact supplied, use extract
or extract!
.
If you don't want to allow parameters that aren't specified, use extract_exhaustive
or extract_exhaustive!
.
And if you just want to pluck values out of a keyword list in the order specified, use extract_permissive
which will default parameters to nil that have no default specified.
The API may change slightly, most likely change would be the function names if people come up with better ones. If so the old names will be supported for some time.
If available in Hex, the package can be installed
by adding kword
to your list of dependencies in mix.exs
:
def deps do
[
{:kword, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/kword.