TExcelTAD
Home › Elixir › Elixir Basic Operators

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
elixir · try it yourself
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
elixir · try it yourself
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, not are strict: their first argument must be a boolean (true or false), otherwise you get a BadBooleanError.
  • &&, ||, ! are relaxed: they accept any value. Only false and nil count as falsy; everything else (including 0, "" and []) is truthy.

Both families short-circuit: the right side is only evaluated when needed.

elixir · try it yourself
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))
end
false
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
elixir · try it yourself
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:

text
number < atom < reference < function < port < pid < tuple < map < list < bitstring

This 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. Use Date.compare/2 and friends for dates, and Decimal.compare/2 for 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?

  1. nil
  2. 0
  3. 5
  4. true

What happens with 1 and true?

  1. It returns true
  2. It returns 1
  3. It raises BadBooleanError
  4. It returns false

Which expression is false?

  1. 1 == 1.0
  2. 1 === 1.0
  3. [1] ++ [2] == [1, 2]
  4. "a" <> "b" == "ab"