October 2014
Intermediate to advanced
280 pages
7h 20m
English
In Elixir, if and its evil twin, unless, take two parameters: a condition and a keyword list, which can contain the keys do: and else:. If the condition is truthy, the if expression evaluates the code associated with the do: key; otherwise it evaluates the else: code. Either branch may be absent.
| | iex> if 1 == 1, do: "true part", else: "false part" |
| | "true part" |
| | iex> if 1 == 2, do: "true part", else: "false part" |
| | "false part" |
Just as it does with functions, Elixir provides some syntactic sugar. You can write the first of the previous examples as follows:
| | iex> if 1 == 1 do |
| | ...> "true part" |
| | ...> else |
| | ...> "false part" |
| | ...> end |
| | true part |
unless is similar:
| | iex> |
Read now
Unlock full access