I want to simultaneously match on a specific key in a map, and capture the rest of that map. I was hoping something like this would work:
iex(10)> %{"nodeType" => type | rest} = %{"nodeType" => "conditional", "foo" => "bar"}
** (CompileError) iex:10: cannot invoke remote function IEx.Helpers.|/2 inside match
The goal is to write a set of functions which take a map, pattern match on one of the map's fields, and perform some transformations on the rest of the map.
def handle_condition(%{"nodeType" => "condition" | rest}) do
# do something with rest
done
def handle_expression(%{"nodeType" => "expression" | rest}) do
# do something with rest
done
But it looks like I'll need the callee to pass the nodeType separately unless I'm missing something.
Pattern matching allows developers to easily destructure data types such as tuples and lists. As we will see in the following chapters, it is one of the foundations of recursion in Elixir and applies to other types as well, like maps and binaries.
Pattern matching is the act of checking one or more inputs against a pre-defined pattern and seeing if they match. In Elm, there's only a fixed set of patterns we can match against, so pattern matching has limited application.
12.3) Maps are the "go to" key-value data structure in Elixir. Maps can be created with the %{} syntax, and key-value pairs can be expressed as key => value : iex> %{} %{} iex> %{"one" => :two, 3 => "four"} %{3 => "four", "one" => :two}
You can easily capture the whole map - maybe this will be enough?
def handle_condition(all = %{"nodeType" => "condition"}) do
# do something with all
end
Or:
def handle_condition(all = %{"nodeType" => "condition"}) do
all = Map.delete(all, "nodeType")
# do something with all
end
Another nice way of achieving this is by using Map.pop/2:
def handle(%{} = map), do: handle(Map.pop(map, "nodeType"))
def handle({"condition", rest}) do
# ... handle conditions here
end
def handle({"expression", rest}) do
# ... handle expressions here
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