Is there a nicer way to write this case statement

case, date2) do
      :lt-> doA()
      :gt -> doB()
      :eq -> doA(); doB()

Thinking is there a way to write this more succinctly? Something like below but we know we cannot call a function for guard so the below does not compile

case, get_changeover_date()) do
      x when Enum.member?(x, [:lt, :eq]) -> doA()
      x when Enum.member?(x, [:gt, :eq])  -> doB()

You can write:

case, get_changeover_date()) do
  x when x in [:lt, :eq] -> doA()
  x when x in [:gt, :eq]  -> doB()

but that is not the same as your example above, since only the first matching case will be executed.

1 Like

Given daA/0 and doB/0 seem to do sideeffects (return nothing of interest) you can do this:

result =, date2)
if result in [:lt, :eq], do: doA()
if result in [:gt, :eq], do: doB()
1 Like

Yeah I would like doA and doB to be executed for :eq. So this would be the solution. The case solution would only doA for :eq

I would need to extrapolate this further as doA and doB returns stuff but thanks. This is a good start :smile: