Skip to content

Commit 60551c8

Browse files
committed
Rephrased some sentences and fixed some grammar in ch. 19
- Rephrased some sentences (mainly to fix inconsistent addressing: "to do that, YOU can do this [...] and so WE need to ...") - Polished some of the code samples
1 parent b9addb3 commit 60551c8

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

getting_started/19.markdown

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ guide: 19
88

99
{% include toc.html %}
1010

11-
We have already learned Elixir provides double-quoted strings and single-quoted char lists. However, this only covers the surface of structures that have textual representation in the language. Atoms are, for example, another structure which are mostly created via the `:atom` representation.
11+
We have already learned that Elixir provides double-quoted strings and single-quoted char lists. However, this only covers the surface of structures that have textual representation in the language. Atoms are, for example, are mostly created via the `:atom` representation.
1212

13-
One of Elixir's goals is extensibility: developers should be able to extend the language to particular domains. Computer science has become such a wide field that it is impossible for a language to tackle many fields as part of its core. Our best bet is to rather make the language extensible, so developers, companies and communities can extend the language to their relevant domains.
13+
One of Elixir's goals is extensibility: developers should be able to extend the language to fit any particular domain. Computer science has become such a wide field that it is impossible for a language to tackle many fields as part of its core. Our best bet is to rather make the language extensible, so developers, companies and communities can extend the language to their relevant domains.
1414

15-
In the chapter, we are going to explore sigils, which are one of the mechanisms provided by the language for working with textual representations.
15+
In this chapter, we are going to explore sigils, which are one of the mechanisms provided by the language for working with textual representations. Sigils start with the tilde (`~`) character which is followed by a letter (which identifies the sigil) and then a delimiter; optionally, modifiers can be added after the final delimiter.
1616

1717
## 19.1 Regular expressions
1818

19-
Sigils start with the tilde (`~`) character which is followed by a letter and then a separator. The most common sigil in Elixir is `~r` for [regular expressions](https://en.wikipedia.org/wiki/Regular_Expressions):
19+
The most common sigil in Elixir is `~r`, which is used to create [regular expressions](https://en.wikipedia.org/wiki/Regular_Expressions):
2020

2121
```iex
22-
# A regular expression that returns true if the text has foo or bar
22+
# A regular expression that matches strings which contain "foo" or "bar":
2323
iex> regex = ~r/foo|bar/
2424
~r/foo|bar/
2525
iex> "foo" =~ regex
@@ -39,7 +39,7 @@ true
3939

4040
Check out the [`Regex` module](/docs/stable/elixir/Regex.html) for more information on other modifiers and the supported operations with regular expressions.
4141

42-
So far, all examples have used `/` to delimit a regular expression. However sigils support 8 different separators:
42+
So far, all examples have used `/` to delimit a regular expression. However sigils support 8 different delimiters:
4343

4444
```
4545
~r/hello/
@@ -52,44 +52,52 @@ So far, all examples have used `/` to delimit a regular expression. However sigi
5252
~r<hello>
5353
```
5454

55-
The reasoning in supporting different operators is that different separators can be more convenient to different sigils. For example, using parentheses for regular expressions may be a confusing choice as they can get mixed with the parentheses inside the regex. However, parentheses can be handy for other sigils, as we will see in the next section.
55+
The reason behind supporting different delimiters is that different delimiters can be more suited for different sigils. For example, using parentheses for regular expressions may be a confusing choice as they can get mixed with the parentheses inside the regex. However, parentheses can be handy for other sigils, as we will see in the next section.
5656

5757
## 19.2 Strings, char lists and words sigils
5858

5959
Besides regular expressions, Elixir ships with three other sigils.
6060

61-
The `~s` sigil is used to generate strings, similar to double quotes:
61+
### 19.2.1 Strings
62+
63+
The `~s` sigil is used to generate strings, like double quotes are. The `~s` sigil is useful, for example, when a string contains both double and single quotes:
6264

6365
```iex
64-
iex> ~s(this is a string with "quotes")
65-
"this is a string with \"quotes\""
66+
iex> ~s(this is a string with "double" quotes, not 'single' ones)
67+
"this is a string with \"double\" quotes, not 'single' ones"
6668
```
6769

68-
While `~c` is used to generate char lists:
70+
### 19.2.2 Char lists
71+
72+
The `~c` sigil is used to generate char lists:
6973

7074
```iex
71-
iex> ~c(this is a string with "quotes")
72-
'this is a string with "quotes"'
75+
iex> ~c(this is a char list containing 'single quotes')
76+
'this is a char list containing \'single quotes\''
7377
```
7478

75-
The `~w` sigil is used to generate a list of words separated by white space:
79+
### 19.2.3 Word lists
80+
81+
The `~w` sigil is used to generate lists of words (*words* are just regular strings). Inside the `~w` sigil, words are separated by whitespace.
7682

7783
```iex
7884
iex> ~w(foo bar bat)
7985
["foo", "bar", "bat"]
8086
```
8187

82-
The `~w` sigil also accepts the `c`, `s` and `a` modifiers (for char lists, strings and atoms, respectively) to choose the format of the result:
88+
The `~w` sigil also accepts the `c`, `s` and `a` modifiers (for char lists, strings and atoms, respectively) which specify the data type of the elements of the resulting list:
8389

8490
```iex
8591
iex> ~w(foo bar bat)a
8692
[:foo, :bar, :bat]
8793
```
8894

89-
Besides lowercase sigils, Elixir supports uppercase sigils. While both `~s` and `~S` will return strings, the first one allows escape codes and interpolation while the second does not:
95+
## 19.3 Interpolation and escaping in sigils
96+
97+
Besides lowercase sigils, Elixir supports uppercase sigils to deal with escaping characters and interpolation. While both `~s` and `~S` will return strings, the former allows escape codes and interpolation while the latter does not:
9098

9199
```elixir
92-
iex> ~s(String with escape codes \x26 interpolation)
100+
iex> ~s(String with escape codes \x26 #{"inter" <> "polation"})
93101
"String with escape codes & interpolation"
94102
iex> ~S(String without escape codes and without #{interpolation})
95103
"String without escape codes and without \#{interpolation}"
@@ -111,10 +119,10 @@ The following escape codes can be used in strings and char lists:
111119
* `\t` – tab
112120
* `\v` – vertical tab
113121
* `\0` - null byte
114-
* `\xDD` - character with hexadecimal representation DD (example: `\x13`)
115-
* `\x{D...}` - character with hexadecimal representation with one or more hexadecimal digits (example: `\x{abc13}`)
122+
* `\xDD` - character with hexadecimal representation DD (e.g., `\x13`)
123+
* `\x{D...}` - character with hexadecimal representation with one or more hexadecimal digits (e.g., `\x{abc13}`)
116124

117-
Sigils also support heredocs, which is when triple double- or single-quotes are used as separators:
125+
Sigils also support heredocs, that is, triple double- or single-quotes as separators:
118126

119127
```iex
120128
iex> ~s"""
@@ -123,7 +131,7 @@ iex> ~s"""
123131
...> """
124132
```
125133

126-
The most common case for heredoc sigils is when writing documentation. For example, if you need to write escape characters in your documentation, it can become error prone as we would need to double-escape some characters:
134+
The most common use case for heredoc sigils is when writing documentation. For example, writing escape characters in documentation would soon become error prone because of the need to double-escape some characters:
127135

128136
```elixir
129137
@doc """
@@ -138,7 +146,7 @@ Converts double-quotes to single-quotes.
138146
def convert(...)
139147
```
140148

141-
By using using `~S`, we can avoid this problem altogether:
149+
By using using `~S`, this problem can be avoided altogether:
142150
143151
```elixir
144152
@doc ~S"""
@@ -153,31 +161,34 @@ Converts double-quotes to single-quotes.
153161
def convert(...)
154162
```
155163

156-
## 19.3 Custom sigils
164+
## 19.4 Custom sigils
157165

158-
As hinted at the beginning of this chapter, sigils in Elixir are extensible. In fact, the sigil `~r/foo/i` is equivalent to calling the `sigil_r` function with a binary and a char list as argument:
166+
As hinted at the beginning of this chapter, sigils in Elixir are extensible. In fact, using the sigil `~r/foo/i` is equivalent to calling the `sigil_r` function with a binary and a char list as argument:
159167

160168
```iex
161169
iex> sigil_r(<<"foo">>, 'i')
162170
~r"foo"i
163171
```
164172

165-
That said, we can access the documentation for the `~r` sigil via the `sigil_r` function:
173+
We can access the documentation for the `~r` sigil via the `sigil_r` function:
166174

167175
```iex
168176
iex> h sigil_r
169177
...
170178
```
171179

172-
We can also provide our own sigils by simply implementing the proper function. For example, let's implement the `~i(13)` sigil that returns an integer:
180+
We can also provide our own sigils by simply implementing functions that follow the `sigil_{identifier}` pattern. For example, let's implement the `~i` sigil that returns an integer (with the optional `n` modifier to make it negative):
173181
174182
```iex
175183
iex> defmodule MySigils do
176184
...> def sigil_i(string, []), do: String.to_integer(string)
185+
...> def sigil_i(string, [?n]), do: String.to_integer(string)
177186
...> end
178187
iex> import MySigils
179188
iex> ~i(13)
180189
13
190+
iex> ~i(42)n
191+
-42
181192
```
182193
183-
Sigils can also be used to do compile-time work with the help of macros. For example, regular expressions in Elixir are compiled into efficient representation during compilation of the source code, therefore skipping this step at runtime. If you have interest in the subject, we recommend you to learn more about macros and check how those sigils are implemented in the `Kernel` module.
194+
Sigils can also be used to do compile-time work with the help of macros. For example, regular expressions in Elixir are compiled into an efficient representation during compilation of the source code, therefore skipping this step at runtime. If you're interested in the subject, we recommend you to learn more about macros and check out how sigils are implemented in the `Kernel` module (where the `sigil_*` functions are defined).

0 commit comments

Comments
 (0)