Operators
August 22, 2023 2 minutes • 408 words
Table of contents
Operators are non-qualified calls.
`.’ is an operator.
Elixir operators always return a float.
Infix Expression 1 + 1
Prefix Expression +(1, 1)
Postfix Expression (1, 1)+
Operator | Name | What it does | Precedence |
---|---|---|---|
@ | module attribute | defines compile constants too | Unary |
. | dot | Left | |
^ | pin | prevents the variable from changing | |
= | match | ||
& | capture | ||
:: | type | ||
! ^ not ~~~ | strict and relaxed boolean “not” | Unary | |
. + - | positive/negative | Unary | |
/ * + - | division, multiplication, etc | arithmetic | Left |
<> ++ – +++ — .. | list concatenation and subtraction | Right | |
in not in | Left | ||
> «< »> «~ ~» <~ ~> <~> < | > | ||
< > <= >= | Left | ||
== != =~ === !== | Left | ||
&& &&& and | strict and relaxed boolean “and” | Left | |
= | Right | ||
& | Unary | ||
-> | arrow | used in anonymous functions and case statements | |
=> (valid only inside %{}) | fat arrow | Defines key value-pairs | Right |
Cons | Splits list to head and tail | Right | |
:: | Right | ||
when | Right | ||
<- \ | Left | ||
in and not in | membership | ||
.. | range creation | ||
<> | binary concatenation | ||
> | pipeline | ||
=~ | text-based match: the left side has the right side | ||
=> | %{} | ||
when | Guards | ||
<- | for with | ||
\ | Default arguments | used to enter something if there is null value |
Sorting Order
number < atom < reference < function < port < pid < tuple < map < list < bitstring
Possible operators:
|||
&&&
<<<
>>>
<<~
~>>
<~
~>
<~>
<|>
+++
---
~~~
Pipe Operator
This takes the result on the left, and passes it to the right.
"This is tough." |> String.upcase() |> String.split()
# ["THIS", "IS", "TOUGH."]
Attach ++
or Detach --
to a [list]
iex> [1, 2, 3] ++ [4, 5, 6]
[1, 2, 3, 4, 5, 6]
iex> [1, 2, 3] -- [2]
[1, 3]
Concatenate strings <>
iex> "inter" <> "nation" <> "alization"
"internationalization"
Boolean and or not
or
and
not
iex> true and true
true
iex> false or is_atom(:example)
true
Non-boolean and or not
||
gets the first one
&&
gets the last one
!
not
iex> 1 || true
1
iex> false || 11
11
iex> nil && 13
nil
iex> true && 17
17
not
iex> !true
false
iex> !1
false
iex> !nil
true
Comparison Operators
<=
>=
<
>
==
!=
iex> 1 == 1
true
iex> 1 != 2
true
iex> 1 < 2
true
Stricter:
===
!==
iex> 1 == 1.0
true
iex> 1 === 1.0
false