Skip to content

Commit bd37919

Browse files
eksperimentaljosevalim
authored andcommitted
Improvements to Operators guide (#8512)
1 parent 60b16a5 commit bd37919

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

lib/elixir/pages/Operators.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ Operator
1414
`*` `/` | Left to right
1515
`+` `-` | Left to right
1616
`++` `--` `..` `<>` | Right to left
17+
`^^^` | Left to right
1718
`in` `not in` | Left to right
18-
`\|>` `<<<` `>>>` `~>>` `<<~` `~>` `<~` `<~>` `<\|>` | Left to right
19+
`\|>` `<<<` `>>>` `<<~` `~>>` `<~` `~>` `<~>` `<\|>` | Left to right
1920
`<` `>` `<=` `>=` | Left to right
2021
`==` `!=` `=~` `===` `!==` | Left to right
2122
`&&` `&&&` `and` | Left to right
2223
`\|\|` `\|\|\|` `or` | Left to right
23-
`&` | Unary
2424
`=` | Right to left
25+
`&` | Unary
2526
`=>` (valid syntax only inside `%{}`) | Right to left
2627
`\|` | Right to left
2728
`::` | Right to left
@@ -36,12 +37,12 @@ Elixir provides the following built-in comparison operators:
3637
* [`===`](`Kernel.===/2`) - strict equality
3738
* [`!=`](`Kernel.!=/2`) - inequality
3839
* [`!==`](`Kernel.!==/2`) - strict inequality
39-
* [`>`](`Kernel.>/2`) - greater than
4040
* [`<`](`Kernel.</2`) - less than
41-
* [`>=`](`Kernel.>=/2`) - greater than or equal
41+
* [`>`](`Kernel.>/2`) - greater than
4242
* [`<=`](`Kernel.<=/2`) - less than or equal
43+
* [`>=`](`Kernel.>=/2`) - greater than or equal
4344

44-
The only difference between [`==`](`Kernel.==/2`) and [`===`](`Kernel.===/2`) is that [`===`](`Kernel.===/2`) is stricter when it comes to comparing integers and floats:
45+
The only difference between [`==`](`Kernel.==/2`) and [`===`](`Kernel.===/2`) is that [`===`](`Kernel.===/2`) is strict when it comes to comparing integers and floats:
4546

4647
```elixir
4748
iex> 1 == 1.0
@@ -67,12 +68,12 @@ The reason we can compare different data types is pragmatism. Sorting algorithms
6768
number < atom < reference < function < port < pid < tuple < map < list < bitstring
6869
```
6970

70-
When comparing two numbers of different types (a number is either an integer or a float), a conversion to the type with greater precision will always occur, unless the comparison operator used is either `===` or `!==`. A float will be considered more precise than an integer, unless the float is greater/less than +/-9007199254740992.0, at which point all the significant figures of the float are to the left of the decimal point. This behavior exists so that the comparison of large numbers remains transitive.
71+
When comparing two numbers of different types (a number being either an integer or a float), a conversion to the type with greater precision will always occur, unless the comparison operator used is either [`===`](`===/2`) or [`!==`](`!==/2`). A float will be considered more precise than an integer, unless the float is greater/less than +/-9007199254740992.0 respectively, at which point all the significant figures of the float are to the left of the decimal point. This behavior exists so that the comparison of large numbers remains transitive.
7172

7273
The collection types are compared using the following rules:
7374

74-
* Tuples are compared by size then element by element.
75-
* Maps are compared by size then by keys in ascending term order then by values in key order. In the specific case of maps' key ordering, integers are always considered to be less than floats.
75+
* Tuples are compared by size, then element by element.
76+
* Maps are compared by size, then by keys in ascending term order, then by values in key order. In the specific case of maps' key ordering, integers are always considered to be less than floats.
7677
* Lists are compared element by element.
7778
* Bitstrings are compared byte by byte, incomplete bytes are compared bit by bit.
7879

@@ -111,10 +112,10 @@ The following is a table of all the operators that Elixir is capable of parsing,
111112
* `&&&`
112113
* `<<<`
113114
* `>>>`
114-
* `~>>`
115115
* `<<~`
116-
* `~>`
116+
* `~>>`
117117
* `<~`
118+
* `~>`
118119
* `<~>`
119120
* `<|>`
120121
* `^^^`

lib/elixir/src/elixir_parser.yrl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Rootsymbol grammar.
4949
Expect 3.
5050

5151
%% Changes in ops and precedence should be reflected on lib/elixir/lib/code/identifier.ex
52+
%% and lib/elixir/pages/Operators.md
5253
%% Note though the operator => in practice has lower precedence than all others,
5354
%% its entry in the table is only to support the %{user | foo => bar} syntax.
5455
Left 5 do.
@@ -65,7 +66,7 @@ Left 130 or_op_eol. %% ||, |||, or
6566
Left 140 and_op_eol. %% &&, &&&, and
6667
Left 150 comp_op_eol. %% ==, !=, =~, ===, !==
6768
Left 160 rel_op_eol. %% <, >, <=, >=
68-
Left 170 arrow_op_eol. %% |>, <<<, >>>, ~>>, <<~, ~>, <~, <~>, <|>
69+
Left 170 arrow_op_eol. %% |>, <<<, >>>, <<~, ~>>, <~, ~>, <~>, <|>
6970
Left 180 in_op_eol. %% in, not in
7071
Left 190 three_op_eol. %% ^^^
7172
Right 200 two_op_eol. %% ++, --, .., <>

0 commit comments

Comments
 (0)