You are on page 1of 4

3/8/2016

Basic operators - Elixir

HOME

INSTALL

GETTING STARTED

LEARNING

DOCS

BLOG

PACKAGES

Basic operators
In the previous chapter, we saw Elixir provides +, -, *, / as arithmetic

News: Elixir v1.2 released


Search...

operators, plus the functions div/2 and rem/2 for integer division and
remainder.

G ETTI N G ST A RTED

1. Introduction
Elixir also provides ++ and -- to manipulate lists:

2. Basic types
3. Basic operators

iex> [1,2,3] ++ [4,5,6]


[1,2,3,4,5,6]
iex> [1,2,3] -- [2]
[1,3]

4. Pattern matching
5. case, cond and if
6. Binaries, strings and char
lists

String concatenation is done with <>:

7. Keywords and maps


8. Modules

iex> "foo" <> "bar"


"foobar"

9. Recursion
10. Enumerables and streams
11. Processes

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

12. IO and the file system


13. alias, require and import
14. Module attributes

iex> true and true


true
iex> false or is_atom(:example)
true

15. Structs
16. Protocols
17. Comprehensions
18. Sigils

Providing a non-boolean will raise an exception:

19. try, catch and rescue


20. Typespecs and behaviours

iex> 1 and true


** (ArgumentError) argument error

or and and are short-circuit operators. They only execute the right side if the

left side is not enough to determine the result:

21. Erlang libraries


22. Where to go next

M IX A N D OTP

1. Introduction to Mix
2. Agent

http://elixir-lang.org/getting-started/basic-operators.html

1/4

3/8/2016

Basic operators - Elixir

iex> false and raise("This error will never be raised")


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

3. GenServer
4. Supervisor and Application
5. ETS
6. Dependencies and umbrella
apps

Note:IfyouareanErlangdeveloper, andand orinElixir


actuallymaptothe andalsoand orelseoperatorsinErlang.
Besides these boolean operators, Elixir also provides ||, && and ! which
accept arguments of any type. For these operators, all values except false and

7. Task and gen-tcp


8. Docs, tests and with
9. Distributed tasks and
configuration

nil will evaluate to true:


M ETA -PROG RA M MI N G I N
EL IX I R

# or
iex> 1 || true
1
iex> false || 11
11
# and

1. Quote and unquote


2. Macros
3. Domain Specific Languages

S PON SORS

iex> nil && 13


nil
iex> true && 17
17
#!
iex> !true

EL IX I R RA D AR

false
iex> !1

A weekly Elixir email newsletter

false

with content curated by

iex> !nil
true

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 !.

Plataformatec. Subscribe
below.

Elixir
Radar
weekly

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

Subscribe
now

newsletter

operators:

iex> 1 == 1
true
iex> 1 != 2
true
iex> 1 < 2
true

http://elixir-lang.org/getting-started/basic-operators.html

2/4

3/8/2016

Basic operators - Elixir

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

iex> 1 == 1.0
true
iex> 1 === 1.0
false

In Elixir, we can compare two different data types:

iex> 1 < :atom


true

The reason we can compare different data types is pragmatism. Sorting


algorithms dont need to worry about different data types in order to sort. The
overall sorting order is defined below:

number < atom < reference < functions < port < pid < tuple <
maps < list < bitstring

You dont actually need to memorize this ordering, but it is important just to
know an order exists.

Operator table
Although we have learned only a handful of operators so far, we present below
the complete operator table for Elixir ordered from higher to lower precedence
for reference:

OPERATOR

ASSOCIATIVITY

Unary

Left to right

+ - ! ^ not ~~~

Unary

* /

Left to right

+ -

Left to right

http://elixir-lang.org/getting-started/basic-operators.html

3/4

3/8/2016

Basic operators - Elixir

++ -- .. <>

Right to left

in

Left to right

|> <<< >>> ~>> <<~ ~> <~ <~>

Left to right

<|>

< > <= >=

Left to right

== != =~ === !==

Left to right

&& &&& and

Left to right

|| ||| or

Left to right

Right to left

=>

Right to left

Right to left

::

Right to left

when

Right to left

<-, \\

Left to right

&

Unary

We will learn the majority of those operators as we go through the getting


started guide. In the next chapter, we are going to discuss some basic functions,
data type conversions and a bit of control-flow.
Previous

Top

Next

2012-2016 Plataformatec. All rights reserved.

http://elixir-lang.org/getting-started/basic-operators.html

4/4

You might also like