Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Basic Operators

03-basic-operators.livemd

Basic Operators

Intro

In the previous chapter, we saw Elixir provides +, -, *, / as arithmetic operators, plus the functions div/2 and rem/2 for integer division and remainder.

Elixir also provides ++ and -- to manipulate lists:

[1, 2, 3] ++ [4, 5, 6]
[1, 2, 3] -- [2]

String concatenation is done with <>:

"foo" <> "bar"

Elixir also provides three boolean operators: or, and and not. These operators are strict in the sense that they expect something that evaluates to a boolean (true or false) as their first argument:

true and true
false or is_atom(:example)

Providing a non-boolean will raise an exception:

1 and true

or and and are short-circuit operators. They only execute the right side if the left side is not enough to determine the result:

false and raise("This error will never be raised")
true or raise("This error will never be raised")

Besides these boolean operators, Elixir also provides ||, && and ! which accept arguments of any type. For these operators, all values except false and nil will evaluate to true:

or

1 || true
false || 11

and

nil &amp;&amp; 13
true &amp;&amp; 17

not

!true
!1
!nil

As a rule of thumb, use and, or and not when you are expecting booleans. If any of the arguments are non-boolean, use &&, || and !.

Elixir also provides ==, !=, ===, !==, <=, >=, < and > as comparison operators:

1 == 1
1 != 2
1 < 2

The difference between == and === is that the latter is more strict when comparing integers and floats:

1 == 1.0
1 === 1.0

In Elixir, we can compare two different data types:

1 < :atom

The reason we can compare different data types is pragmatism. Sorting algorithms don’t need to worry about different data types in order to sort. The sorting order, from lower to higher, is:

number, atom, reference, function, port, pid, tuple, map, list, bitstring

You don’t actually need to memorize this; it’s enough to know that it exists. For more information, check the reference page on operators and ordering.

In the next chapter, we are going to discuss pattern matching through the use of =, the match operator.