Elixir Basic Operators
Operators in Elixir are mostly what you would expect, with a few twists: there are two kinds of boolean operators, two kinds of equality, and dedicated operators for joining strings and lists.
Arithmetic
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - * |
add, subtract, multiply | 7 * 6 |
42 |
/ |
division (always a float) | 7 / 2 |
3.5 |
** |
power | 2 ** 10 |
1024 |
div/2, rem/2 |
integer division and remainder (functions, not operators) | div(7, 2) |
3 |
IO.inspect(7 + 3 * 2)
IO.inspect((7 + 3) * 2)
IO.inspect(7 / 2)
IO.inspect(div(-7, 2))
IO.inspect(rem(-7, 2))
IO.inspect(Integer.mod(-7, 2))13
20
3.5
-3
-1
1
div truncates toward zero and rem keeps the sign of the dividend, so rem(-7, 2) is -1. When you need a result that is always non-negative (for clock arithmetic, for example) use Integer.mod/2.
Joining strings and lists
| Operator | Works on | Example | Result |
|---|---|---|---|
<> |
strings (binaries) | "foo" <> "bar" |
"foobar" |
++ |
lists, concatenation | [1, 2] ++ [3] |
[1, 2, 3] |
-- |
lists, subtraction | [1, 2, 3, 2] -- [2] |
[1, 3, 2] |
in |
membership in an enumerable | 2 in [1, 2] |
true |
IO.puts("data" <> "base")
IO.inspect([:a, :b] ++ [:c])
IO.inspect([1, 2, 3, 2, 1] -- [2, 1])
IO.inspect(:b in [:a, :b, :c])
IO.inspect(5 in 1..10)database
[:a, :b, :c]
[3, 2, 1]
true
true
-- removes the first occurrence of each element on the right, which is why one 2 and one 1 are left.
Boolean operators: and/or/not vs &&/||/!
Elixir has two families:
and,or,notare strict: their first argument must be a boolean (trueorfalse), otherwise you get aBadBooleanError.&&,||,!are relaxed: they accept any value. Onlyfalseandnilcount as falsy; everything else (including0,""and[]) is truthy.
Both families short-circuit: the right side is only evaluated when needed.
IO.inspect(true and false)
IO.inspect(not true)
IO.inspect(nil || "default")
IO.inspect(0 || "default")
IO.inspect("" && "both truthy")
IO.inspect(!nil)
try do
IO.inspect(1 and true)
rescue
e -> IO.puts("error: " <> Exception.message(e))
endfalse
false
"default"
0
"both truthy"
true
error: expected a boolean on left-side of "and", got:
1
A very common idiom is value || default: if value is nil you get the default.
Comparison and equality
| Operator | Meaning |
|---|---|
== != |
equal / not equal (1 == 1.0 is true) |
=== !== |
strictly equal / not equal (1 === 1.0 is false) |
< > <= >= |
ordering |
IO.inspect(1 == 1.0)
IO.inspect(1 === 1.0)
IO.inspect("apple" < "banana")
IO.inspect(:zebra < "aardvark")
IO.inspect(1 < :atom)true
false
true
true
true
Any two values can be compared, even of different types. The global order is:
number < atom < reference < function < port < pid < tuple < map < list < bitstringThis is what makes sorting a mixed list possible, but it can also hide bugs: comparing a number to a string never raises, it is simply true.
Note:
<,>on structs such as dates compare their internal fields, which gives wrong answers. UseDate.compare/2and friends for dates, andDecimal.compare/2for decimals.
Operator precedence
From high to low (most common ones): unary ! not -, then **, then * /, then + -, then ++ -- <> (right-associative), in, then |>, comparisons, && and, || or, =. When in doubt, add parentheses.
Test yourself
What is nil || 0 || 5?
- nil
- 0
- 5
- true
What happens with 1 and true?
- It returns
true - It returns
1 - It raises
BadBooleanError - It returns
false
Which expression is false?
1 == 1.01 === 1.0[1] ++ [2] == [1, 2]"a" <> "b" == "ab"