You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2014-04-21-elixir-v0-13-0-released.markdown
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ Even with all those improvements, Elixir v0.13.0 is backwards compatible with El
42
42
43
43
Maps are key-value data structures:
44
44
45
-
```iex
45
+
```elixir
46
46
iex> map = %{"hello"=>:world}
47
47
%{"hello"=>:world}
48
48
iex> map["hello"]
@@ -55,7 +55,7 @@ Maps do not have a explicit ordering and keys and values can be any term.
55
55
56
56
Maps can be pattern matched on:
57
57
58
-
```iex
58
+
```elixir
59
59
iex> %{"hello"=> world} = map
60
60
%{"hello"=>:world}
61
61
iex> world
@@ -72,7 +72,7 @@ Developers can use the functions in the [`Map` module](https://hexdocs.pm/elixir
72
72
73
73
Maps also provide special syntax for creating, accessing and updating maps with atom keys:
74
74
75
-
```iex
75
+
```elixir
76
76
iex> user = %{name:"john", age:27}
77
77
%{name:"john", age:27}
78
78
iex> user.name
@@ -85,7 +85,7 @@ iex> user.name
85
85
86
86
Both access and update syntax above expect the given keys to exist. Trying to access or update a key that does not exist raises an error:
87
87
88
-
```iex
88
+
```elixir
89
89
iex> %{ user |address: [] }
90
90
** (ArgumentError) argument error
91
91
:maps.update(:address, [], %{})
@@ -110,7 +110,7 @@ Internally, this record is represented as the following tuple:
110
110
111
111
Records can also be created and pattern matched on:
112
112
113
-
```iex
113
+
```elixir
114
114
iex> user =User[name:"john"]
115
115
User[name:"john", age:0]
116
116
iex> user.name
@@ -156,21 +156,21 @@ end
156
156
157
157
Now a `User` struct can be created without a need to explicitly list all necessary fields:
158
158
159
-
```iex
159
+
```elixir
160
160
iex> user = %User{name:"john"}
161
161
%User{name:"john", age:0}
162
162
```
163
163
164
164
Trying to create a struct with an unknown key raises an error during compilation:
165
165
166
-
```iex
166
+
```elixir
167
167
iex> user = %User{address: []}
168
168
** (CompileError) unknown key :addressfor struct User
169
169
```
170
170
171
171
Furthermore, every struct has a `__struct__` field which contains the struct name:
172
172
173
-
```iex
173
+
```elixir
174
174
iex> user.__struct__
175
175
User
176
176
```
@@ -195,29 +195,29 @@ Erlang R17 also introduced recursion to anonymous functions. This feature, while
195
195
196
196
The most common use case of a comprehension are [list comprehensions](https://en.wikipedia.org/wiki/List_comprehension). For example, we can get all the square values of elements in a list as follows:
197
197
198
-
```iex
198
+
```elixir
199
199
iex>for n <- [1, 2, 3, 4], do: n * n
200
200
[1, 4, 9, 16]
201
201
```
202
202
203
203
We say the `n <- [1, 2, 3, 4]` part is a comprehension generator. In previous Elixir versions, Elixir supported only lists in generators. In Elixir v0.13.0, any Enumerable is supported (ranges, maps, etc):
204
204
205
-
```iex
205
+
```elixir
206
206
iex>for n <-1..4, do: n * n
207
207
[1, 4, 9, 16]
208
208
```
209
209
210
210
As in previous Elixir versions, there is also support for a bitstring generator. In the example below, we receive a stream of RGB pixels as a binary and break it down into triplets:
By default, a comprehension returns a list as a result. However the result of a comprehension can be inserted into different data structures by passing the `:into` option. For example, we can use bitstring generators with the `:into` option to easily remove all spaces in a string:
219
219
220
-
```iex
220
+
```elixir
221
221
iex>for<<c <-" hello world ">>, c !=?\s, into:"", do:<<c>>
222
222
"helloworld"
223
223
```
@@ -226,7 +226,7 @@ Sets, maps and other dictionaries can also be given with the `:into` option. In
226
226
227
227
For example, the `IO` module provides streams, that are both `Enumerable` and `Collectable`. You can implement an echo terminal that returns whatever is typed into the shell, but in upcase, using comprehensions:
Copy file name to clipboardExpand all lines: _posts/2016-06-21-elixir-v1-3-0-released.markdown
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,7 @@ Elixir v1.3 also introduces 3 new sigils related to the types above:
71
71
72
72
This release introduces new accessors to make it simpler for developers to traverse nested data structures, traversing and updating data in different ways. For instance, given a user with a list of languages, here is how to deeply traverse the map and convert all language names to uppercase:
Copy file name to clipboardExpand all lines: _posts/2017-01-05-elixir-v1-4-0-released.markdown
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Broadly speaking, the Registry is a local, decentralized and scalable key-value
22
22
23
23
A registry may have unique or duplicate keys. Every key-value pair is associated to the process registering the key. Keys are automatically removed once the owner process terminates. Starting, registering and looking up keys is quite straight-forward:
@@ -97,7 +97,7 @@ We use `import` whenever we want to access functions or macros from other module
97
97
98
98
For example, if we want to use the `duplicate/2` function from the `List` module several times, we can import it:
99
99
100
-
```iex
100
+
```elixir
101
101
iex>importList, only: [duplicate:2]
102
102
List
103
103
iex> duplicate :ok, 3
@@ -164,7 +164,7 @@ At this point, you may be wondering: what exactly is an Elixir alias and how is
164
164
165
165
An alias in Elixir is a capitalized identifier (like `String`, `Keyword`, etc) which is converted to an atom during compilation. For instance, the `String` alias translates by default to the atom `:"Elixir.String"`:
166
166
167
-
```iex
167
+
```elixir
168
168
iex>is_atom(String)
169
169
true
170
170
iex>to_string(String)
@@ -177,7 +177,7 @@ By using the `alias/2` directive, we are changing the atom the alias expands to.
177
177
178
178
Aliases expand to atoms because in the Erlang <abbrtitle="Virtual Machine">VM</abbr> (and consequently Elixir) modules are always represented by atoms. For example, that's the mechanism we use to call Erlang modules:
Copy file name to clipboardExpand all lines: getting-started/basic-operators.markdown
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ In the [previous chapter](/getting-started/basic-types.html), we saw Elixir prov
11
11
12
12
Elixir also provides `++` and `--` to manipulate lists:
13
13
14
-
```iex
14
+
```elixir
15
15
iex> [1, 2, 3] ++ [4, 5, 6]
16
16
[1, 2, 3, 4, 5, 6]
17
17
iex> [1, 2, 3] -- [2]
@@ -20,14 +20,14 @@ iex> [1, 2, 3] -- [2]
20
20
21
21
String concatenation is done with `<>`:
22
22
23
-
```iex
23
+
```elixir
24
24
iex>"foo"<>"bar"
25
25
"foobar"
26
26
```
27
27
28
28
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:
29
29
30
-
```iex
30
+
```elixir
31
31
iex>trueandtrue
32
32
true
33
33
iex>falseoris_atom(:example)
@@ -36,14 +36,14 @@ true
36
36
37
37
Providing a non-boolean will raise an exception:
38
38
39
-
```iex
39
+
```elixir
40
40
iex>1andtrue
41
41
** (BadBooleanError) expected a boolean on left-side of "and", got:1
42
42
```
43
43
44
44
`or` and `and` are short-circuit operators. They only execute the right side if the left side is not enough to determine the result:
45
45
46
-
```iex
46
+
```elixir
47
47
iex>falseandraise("This error will never be raised")
48
48
false
49
49
iex>trueorraise("This error will never be raised")
@@ -54,7 +54,7 @@ true
54
54
55
55
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:
56
56
57
-
```iex
57
+
```elixir
58
58
# or
59
59
iex>1||true
60
60
1
@@ -80,7 +80,7 @@ As a rule of thumb, use `and`, `or` and `not` when you are expecting booleans. I
80
80
81
81
Elixir also provides `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` and `>` as comparison operators:
82
82
83
-
```iex
83
+
```elixir
84
84
iex>1==1
85
85
true
86
86
iex>1!=2
@@ -91,7 +91,7 @@ true
91
91
92
92
The difference between `==` and `===` is that the latter is more strict when comparing integers and floats:
93
93
94
-
```iex
94
+
```elixir
95
95
iex>1==1.0
96
96
true
97
97
iex>1===1.0
@@ -100,7 +100,7 @@ false
100
100
101
101
In Elixir, we can compare two different data types:
0 commit comments