Reputation: 453
How can I do, if I can, pattern matching with "or" condition? I need this because I have some different conditions for which action is the same?
case something123 do
:a -> func1()
:b -> func1()
:c -> func1()
:d -> func2()
end
Upvotes: 7
Views: 8348
Reputation: 2307
You can also use cond to do it.
cond do
something123 == :a or something123 == :b something123 == :c ->
func1()
something123 == :d ->
func2()
end
Upvotes: 7
Reputation: 10041
You could use multiple function heads and then multiple working function. Something along the lines of
def foo(%{"key" => "value"}), do: do_something()
def foo(%{"other_key" => "other_value"}), do: do_something()
def foo(map), do: do_something_else(map)
defp do_something() do
...
end
defp do_something_else(map) do
...
end
This would allow you to match on the data that you need, and act accordingly.
Upvotes: 4
Reputation: 222060
You can use in
and lists:
case something123 do
x when x in [:a, :b, :c] -> func1()
:d -> func2()
end
x in [:a, :b, :c]
expands to x == :a or x == :b or x == :c
when the in
macro detects it's being called from a guard statement and the RHS is a literal list (because you can't call remote functions from guards).
Upvotes: 20