I want to achieve something like the below (which obviously does not work)
def f1(%{key => _}) when is_integer(key) do :error end
def f1(%{}) do :ok end
I could match a map in the function head and check for an integer key inside the function body. Just wondering if there is a better approach. Any suggestions?
Nope, this can't be done with pattern matching. Here's how I'd implement it using the method you described:
def f1(%{} = map) do
if map |> Map.keys() |> Enum.any?(&is_integer/1), do: :error, else: :ok
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With