Elixir provides several is_ functions that let you check whether an element is of a particular type:
is_atomis_binaryis_map- etc.
But how can I check if a value is a keyword list in Elixir? I understand that the keyword lists below are a list of two-element tuples with the first element as an atom, so my current workaround is this:
defmodule KList do def is_keyword?(list) when is_list(list) do Enum.all? list, fn item -> case item do {k, _} -> is_atom(k) _ -> false end end end def is_keyword?(_), do: false end
Is there a better (or built-in) way to do this? And more importantly, how can I do this in the when clause?
source share