Embed Notice
HTML Code
Corresponding Notice
- Embed this notice@p @j @Junes @SilverDeth @lanodan @ElDeadKennedy @ceo_of_monoeye_dating
>We have just added an invariant. We can proceed down the line, gradually narrowing the number of cases we need to deal with instead of branching. You can sort of do this in Elixir but it requires you to name all of the intermediate steps.
You do this with a cond clause, no need to name anything. Same thing, slightly different syntax.
cond do
is_nil(var) -> do_nil(var)
is_binary(var) -> do_binary(var)
var == 5 -> do_five(var)
true -> do_default(var)
end
You don't have to call a function after the match either, you can do the processing right after the match. Put this at the bottom of a function and you have the exact same thing.