Elixir: How to test multiple values in case condition?

There’s no direct syntax for or in the middle of patterns but you can use a guard:

case testvalue do
  n when n in [200, 400] ->
    true
  _ ->
    false
end

You can also use or in guards. This will work too but is more verbose:

case testvalue do
  n when n == 200 or n == 400 ->
    true
  _ ->
    false
end

Both will run equally fast as in inside guards is converted into comparisons + or, as mentioned in the docs.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)