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
In order to facilitate software reuse, Elixir provides three directives. As we are going to see below, they are called directives because they have **lexical scope**.
13
12
14
-
## 13.1 alias
13
+
## `alias`
15
14
16
15
`alias` allows you to set up aliases for any given module name. Imagine our `Math` module uses a special list implementation for doing math specific operations:
17
16
@@ -60,7 +59,7 @@ end
60
59
61
60
In the example above, since we are invoking `alias` inside the function `plus/2`, the alias will just be valid inside the function `plus/2`. `minus/2` won't be affected at all.
62
61
63
-
## 13.2 require
62
+
## `require`
64
63
65
64
Elixir provides macros as a mechanism for meta-programming (writing code that generates code).
66
65
@@ -79,7 +78,7 @@ In Elixir, `Integer.is_odd/1` is defined as a macro so that it can be used as a
79
78
80
79
In general a module does not need to be required before usage, except if we want to use the macros available in that module. An attempt to call a macro that was not loaded will raise an error. Note that like the `alias` directive, `require` is also lexically scoped. We will talk more about macros in a later chapter.
81
80
82
-
## 13.3 import
81
+
## `import`
83
82
84
83
We use `import` whenever we want to easily access functions or macros from other modules without using the fully-qualified name. For instance, if we want to use the `duplicate/2` function from the `List` module several times, we can simply import it:
85
84
@@ -119,7 +118,7 @@ In the example above, the imported `List.duplicate/2` is only visible within tha
119
118
120
119
Note that `import`ing a module automatically `require`s it.
121
120
122
-
## 13.4 Aliases
121
+
## Aliases
123
122
124
123
At this point you may be wondering: what exactly an Elixir alias is and how is it represented?
125
124
@@ -154,7 +153,7 @@ iex> mod.flatten([1, [2], 3])
154
153
155
154
We are simply calling the function `flatten` on the atom `:lists`.
156
155
157
-
## 13.5 Nesting
156
+
## Nesting
158
157
159
158
Now that we have talked about aliases, we can talk about nesting and how it works in Elixir. Consider the following example:
Copy file name to clipboardExpand all lines: getting-started/basic-types.markdown
+10-11
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,7 @@
1
1
---
2
2
layout: getting-started
3
-
title: 2 Basic types
4
-
guide: 2
5
-
redirect_from: "/getting_started/2.html"
3
+
title: Basic types
4
+
redirect_from: /getting_started/2.html
6
5
---
7
6
8
7
# {{ page.title }}
@@ -22,7 +21,7 @@ iex> [1, 2, 3] # list
22
21
iex> {1, 2, 3} # tuple
23
22
```
24
23
25
-
## 2.1 Basic arithmetic
24
+
## Basic arithmetic
26
25
27
26
Open up `iex` and type the following expressions:
28
27
@@ -79,7 +78,7 @@ iex> trunc 3.58
79
78
3
80
79
```
81
80
82
-
## 2.2 Booleans
81
+
## Booleans
83
82
84
83
Elixir supports `true` and `false` as booleans:
85
84
@@ -105,7 +104,7 @@ You can also use `is_integer/1`, `is_float/1` or `is_number/1` to check, respect
105
104
106
105
> Note: At any moment you can type `h` in the shell to print information on how to use the shell. The `h` helper can also be used to access documentation for any function. For example, typing `h is_integer/1` is going to print the documentation for the `is_integer/1` function. It also works with operators and other constructs (try `h ==/2`).
107
106
108
-
## 2.3 Atoms
107
+
## Atoms
109
108
110
109
Atoms are constants where their name is their own value. Some other languages call these symbols:
111
110
@@ -127,7 +126,7 @@ iex> is_boolean(:false)
127
126
true
128
127
```
129
128
130
-
## 2.4 Strings
129
+
## Strings
131
130
132
131
Strings in Elixir are inserted between double quotes, and they are encoded in UTF-8:
133
132
@@ -194,7 +193,7 @@ iex> String.upcase("hellö")
194
193
"HELLÖ"
195
194
```
196
195
197
-
## 2.5 Anonymous functions
196
+
## Anonymous functions
198
197
199
198
Functions are delimited by the keywords `fn` and `end`:
200
199
@@ -235,7 +234,7 @@ iex> x
235
234
42
236
235
```
237
236
238
-
## 2.6 (Linked) Lists
237
+
## (Linked) Lists
239
238
240
239
Elixir uses square brackets to specify a list of values. Values can be of any type:
241
240
@@ -292,7 +291,7 @@ false
292
291
293
292
Single-quotes are char lists, double-quotes are strings. We will talk more about them in the "Binaries, strings and char lists" chapter.
294
293
295
-
## 2.7 Tuples
294
+
## Tuples
296
295
297
296
Elixir uses curly brackets to define tuples. Like lists, tuples can hold any value:
298
297
@@ -329,7 +328,7 @@ Notice that `put_elem/3` returned a new tuple. The original tuple stored in the
329
328
330
329
By being immutable, Elixir also helps eliminate common cases where concurrent code has race conditions because two different entities are trying to change a data structure at the same time.
Copy file name to clipboardExpand all lines: getting-started/binaries-strings-and-char-lists.markdown
+5-6
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,7 @@
1
1
---
2
2
layout: getting-started
3
-
title: 6 Binaries, strings and char lists
4
-
guide: 6
5
-
redirect_from: "/getting_started/6.html"
3
+
title: Binaries, strings and char lists
4
+
redirect_from: /getting_started/6.html
6
5
---
7
6
8
7
# {{ page.title }}
@@ -20,7 +19,7 @@ true
20
19
21
20
In this chapter, we will understand what binaries are, how they associate with strings, and what a single-quoted value, `'like this'`, means in Elixir.
22
21
23
-
## 6.1 UTF-8 and Unicode
22
+
## UTF-8 and Unicode
24
23
25
24
A string is a UTF-8 encoded binary. In order to understand exactly what we mean by that, we need to understand the difference between bytes and code points.
26
25
@@ -59,7 +58,7 @@ You will see that Elixir has excellent support for working with strings. It also
59
58
60
59
However, strings are just part of the story. If a string is a binary, and we have used the `is_binary/1` function, Elixir must have an underlying type empowering strings. And it does. Let's talk about binaries!
61
60
62
-
## 6.2 Binaries (and bitstrings)
61
+
## Binaries (and bitstrings)
63
62
64
63
In Elixir, you can define a binary using `<<>>`:
65
64
@@ -154,7 +153,7 @@ iex> rest
154
153
155
154
This finishes our tour of bitstrings, binaries and strings. A string is a UTF-8 encoded binary, and a binary is a bitstring where the number of bits is divisible by 8. Although this shows the flexibility Elixir provides to work with bits and bytes, 99% of the time you will be working with binaries and using the `is_binary/1` and `byte_size/1` functions.
156
155
157
-
## 6.3 Char lists
156
+
## Char lists
158
157
159
158
A char list is nothing more than a list of characters:
In this chapter, we will learn about the `case`, `cond` and `if` control-flow structures.
13
12
14
-
## 5.1 case
13
+
## `case`
15
14
16
15
`case` allows us to compare a value against many patterns until we find a matching one:
17
16
@@ -50,7 +49,7 @@ iex> case {1, 2, 3} do
50
49
51
50
The first clause above will only match when `x` is positive.
52
51
53
-
## 5.2 Expressions in guard clauses.
52
+
## Expressions in guard clauses
54
53
55
54
The Erlang Virtual Machine (VM) only allows a limited set of expressions in guards:
56
55
@@ -134,7 +133,7 @@ iex> f.(-1, 3)
134
133
135
134
The number of arguments in each anonymous function clause needs to be the same, otherwise an error is raised.
136
135
137
-
## 5.3 cond
136
+
## `cond`
138
137
139
138
`case` is useful when you need to match against different values. However, in many circumstances, we want to check different conditions and find the first one that evaluates to true. In such cases, one may use `cond`:
140
139
@@ -175,7 +174,7 @@ iex> cond do
175
174
"1 is considered as true"
176
175
```
177
176
178
-
## 5.4 if and unless
177
+
## `if` and `unless`
179
178
180
179
Besides `case` and `cond`, Elixir also provides the macros `if/2` and `unless/2` which are useful when you need to check for just one condition:
181
180
@@ -205,16 +204,16 @@ iex> if nil do
205
204
206
205
> Note: An interesting note regarding `if/2` and `unless/2` is that they are implemented as macros in the language; they aren't special language constructs as they would be in many languages. You can check the documentation and the source of `if/2` in [the `Kernel` module docs](/docs/stable/elixir/Kernel.html). The `Kernel` module is also where operators like `+/2` and functions like `is_function/2` are defined, all automatically imported and available in your code by default.
207
206
208
-
## 5.5 `do`/`end` blocks
207
+
## `do/end` blocks
209
208
210
-
At this point, we have learned four control structures: `case`, `cond`, `if` and `unless`, and they were all wrapped in `do`/`end` blocks. It happens we could also write `if` as follows:
209
+
At this point, we have learned four control structures: `case`, `cond`, `if` and `unless`, and they were all wrapped in `do/end` blocks. It happens we could also write `if` as follows:
211
210
212
211
```iex
213
212
iex> if true, do: 1 + 2
214
213
3
215
214
```
216
215
217
-
In Elixir, `do`/`end` blocks are a convenience for passing a group of expressions to `do:`. These are equivalent:
216
+
In Elixir, `do/end` blocks are a convenience for passing a group of expressions to `do:`. These are equivalent:
Copy file name to clipboardExpand all lines: getting-started/comprehensions.markdown
+5-6
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,7 @@
1
1
---
2
2
layout: getting-started
3
-
title: 17 Comprehensions
4
-
guide: 17
5
-
redirect_from: "/getting_started/17.html"
3
+
title: Comprehensions
4
+
redirect_from: /getting_started/17.html
6
5
---
7
6
8
7
# {{ page.title }}
@@ -20,7 +19,7 @@ iex> for n <- [1, 2, 3, 4], do: n * n
20
19
21
20
A comprehension is made of three parts: generators, filters and collectables.
22
21
23
-
## 17.1 Generators and filters
22
+
## Generators and filters
24
23
25
24
In the expression above, `n <- [1, 2, 3, 4]` is the **generator**. It is literally generating values to be used in the comprehension. Any enumerable can be passed in the right-hand side of the generator expression:
26
25
@@ -60,7 +59,7 @@ end
60
59
61
60
Keep in mind that variable assignments inside the comprehension, be it in generators, filters or inside the block, are not reflected outside of the comprehension.
62
61
63
-
## 17.2 Bitstring generators
62
+
## Bitstring generators
64
63
65
64
Bitstring generators are also supported and are very useful when you need to comprehend over bitstring streams. The example below receives a list of pixels from a binary with their respective red, green and blue values and converts them into tuples of three elements each:
A bitstring generator can be mixed with the "regular" enumerable generators and provides filters as well.
74
73
75
-
## 17.3 Results other than lists
74
+
## Results other than lists
76
75
77
76
In the examples above, all the comprehensions returned lists as their result. However, the result of a comprehension can be inserted into different data structures by passing the `:into` option to the comprehension.
Copy file name to clipboardExpand all lines: getting-started/enumerables-and-streams.markdown
+6-7
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,14 @@
1
1
---
2
2
layout: getting-started
3
-
title: 10 Enumerables and Streams
4
-
guide: 10
5
-
redirect_from: "/getting_started/10.html"
3
+
title: Enumerables and Streams
4
+
redirect_from: /getting_started/10.html
6
5
---
7
6
8
7
# {{ page.title }}
9
8
10
9
{% include toc.html %}
11
10
12
-
## 10.1 Enumerables
11
+
## Enumerables
13
12
14
13
Elixir provides the concept of enumerables and [the `Enum` module](/docs/stable/elixir/Enum.html) to work with them. We have already learned two enumerables: lists and maps.
15
14
@@ -35,7 +34,7 @@ Since the Enum module was designed to work across different data types, its API
35
34
36
35
We say the functions in the `Enum` module are polymorphic because they can work with diverse data types. In particular, the functions in the `Enum` module can work with any data type that implements [the `Enumerable` protocol](/docs/stable/elixir/Enumerable.html). We are going to discuss Protocols in a later chapter, for now we are going to move on to a specific kind of enumerable called streams.
37
36
38
-
## 10.2 Eager vs Lazy
37
+
## Eager vs Lazy
39
38
40
39
All the functions in the `Enum` module are eager. Many functions expect an enumerable and return a list back:
The example above has a pipeline of operations. We start with a range and then multiply each element in the range by 3. This first operation will now create and return a list with `100_000` items. Then we keep all odd elements from the list, generating a new list, now with `50_000` items, and then we sum all entries.
57
56
58
-
### 10.2.1 The pipe operator
57
+
### The pipe operator
59
58
60
59
The `|>` symbol used in the snippet above is the **pipe operator**: it simply takes the output from the expression on its left side and passes it as the input to the function call on its right side. It's similar to the Unix `|` operator. Its purpose is to highlight the flow of data being transformed by a series of functions. To see how it can make the code cleaner, have a look at the example above rewritten without using the `|>` operator:
Copy file name to clipboardExpand all lines: getting-started/introduction.markdown
+5-6
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,7 @@
1
1
---
2
2
layout: getting-started
3
-
title: 1 Introduction
4
-
guide: 1
5
-
redirect_from: "/getting_started/1.html"
3
+
title: Introduction
4
+
redirect_from: /getting_started/1.html
6
5
---
7
6
8
7
# {{ page.title }}
@@ -22,11 +21,11 @@ Let's get started!
22
21
23
22
> If you find any errors in the tutorial or on the website, [please report a bug or send a pull request to our issue tracker](https://github.com/elixir-lang/elixir-lang.github.com). If you suspect it is a language bug, [please let us know in the language issue tracker](https://github.com/elixir-lang/elixir/issues).
24
23
25
-
## 1.1 Installation
24
+
## Installation
26
25
27
26
If you still haven't installed Elixir, run to our [installation page](/install.html). Once you are done, you can run `elixir -v` to get the current Elixir version.
28
27
29
-
## 1.2 Interactive mode
28
+
## Interactive mode
30
29
31
30
When you install Elixir, you will have three new executables: `iex`, `elixir` and `elixirc`. If you compiled Elixir from source or are using a packaged version, you can find these inside the `bin` directory.
32
31
@@ -43,7 +42,7 @@ iex> "hello" <> " world"
43
42
44
43
It seems we are ready to go! We will use the interactive shell quite a lot in the next chapters to get a bit more familiar with the language constructs and basic types, starting in the next chapter.
45
44
46
-
## 1.3 Running scripts
45
+
## Running scripts
47
46
48
47
After getting familiar with the basics of the language you may want to try writing simple programs. This can be accomplished by putting Elixir code into a file and executing it with `elixir`:
0 commit comments