Skip to content

Commit f059ccf

Browse files
committed
c20 progress
1 parent 2613e60 commit f059ccf

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

19-sigils.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ Sigil原意是“魔符,图章,印记”,
1111
本章(或者本文)所有的*sigil*都会翻译成“魔法印”或是采用英文原文。
1212

1313
我们已经学习了Elixir提供的字符串(双引号包裹)和字符列表(单引号包裹)。
14-
但是对于Elixir中所有的*文本描述型数据类型*来说,这些只是冰山一角。其它的,例如*原子*也是一种文本描述型数据类型。
14+
但是对于Elixir中所有的*文本描述型数据类型*来说,这些只是冰山一角。其它的,
15+
例如*原子*也是一种文本描述型数据类型。
1516

1617
Elixir的一个特点就是高可扩展性:开发者能够为特定的领域来扩展语言。
1718
计算机科学的领域已是如此广阔。几乎无法设计一门语言来涵盖所有范围。
18-
我们的打算是,与其创造出一种万能的语言,不如创造一种可扩展的语言,让开发者可以根据所从事的领域来对语言进行扩展。
19+
我们的打算是,与其创造出一种万能的语言,不如创造一种可扩展的语言,
20+
让开发者可以根据所从事的领域来对语言进行扩展。
1921

2022
本章将讲述“魔法印(sigils)”,它是Elixir提供的处理文本描述型数据的一种机制。
2123

2224
## 19.1-正则表达式
23-
魔法印以波浪号(~)起头,后面跟着一个字母,然后是分隔符。
24-
最常用的魔法印是~r,代表[正则表达式](https://en.wikipedia.org/wiki/Regular_Expressions)
25+
魔法印以波浪号(~)起头,后面跟着一个字母,然后是分隔符。最常用的魔法印是~r,
26+
代表[正则表达式](https://en.wikipedia.org/wiki/Regular_Expressions)
27+
2528
```elixir
2629
# A regular expression that returns true if the text has foo or bar
2730
iex> regex = ~r/foo|bar/
@@ -35,6 +38,7 @@ false
3538
Elixir提供了Perl兼容的正则表达式(regex),由[PCRE库](http://www.pcre.org/)实现。
3639

3740
正则表达式支持修饰符(modifiers),例如```i```修饰符使该正则表达式无视大小写:
41+
3842
```elixir
3943
iex> "HELLO" =~ ~r/hello/
4044
false
@@ -45,6 +49,7 @@ true
4549
阅读[Regex模块](http://elixir-lang.org/docs/stable/elixir/Regex.html)获取关于其它修饰符的及其所支持的操作的更多信息。
4650

4751
目前为止,所有的例子都用了```/```界定正则表达式。事实上魔法印支持8种不同的分隔符:
52+
4853
```elixir
4954
~r/hello/
5055
~r|hello|
@@ -64,33 +69,38 @@ true
6469
除了正则表达式,Elixir还提供了三种魔法印。
6570

6671
魔法印```~s``` 用来生成字符串,类似双引号的作用:
72+
6773
```elixir
6874
iex> ~s(this is a string with "quotes")
6975
"this is a string with \"quotes\""
7076
```
7177
通过这个例子可以看出,如果文本中有双引号,又不想逐个转义,可以用这种魔法印来包裹字符串。
7278

7379
魔法印```~c``` 用来生成字符列表:
80+
7481
```elixir
7582
iex> ~c(this is a string with "quotes")
7683
'this is a string with "quotes"'
7784
```
7885

7986
魔法印```~w``` 用来生成单词,以空格分隔开:
87+
8088
```elixir
8189
iex> ~w(foo bar bat)
8290
["foo", "bar", "bat"]
8391
```
8492

8593
魔法印```~w``` 还接受```c``````s``````a```修饰符(分别代表字符列表,字符串和原子)
8694
来选择结果的类型:
95+
8796
```elixir
8897
iex> ~w(foo bar bat)a
8998
[:foo, :bar, :bat]
9099
```
91100

92101
除了小写的魔法印,Elixir还支持大写的魔法印。如,```~s``````~S```都返回字符串,
93102
前者会解释转义字符而后者不会:
103+
94104
```elixir
95105
iex> ~s(String with escape codes \x26 interpolation)
96106
"String with escape codes & interpolation"
@@ -118,6 +128,7 @@ iex> ~S(String without escape codes and without #{interpolation})
118128

119129

120130
魔法印还支持多行文本(heredocs),使用的是三个双引号或单引号:
131+
121132
```elixir
122133
iex> ~s"""
123134
...> this is
@@ -127,6 +138,7 @@ iex> ~s"""
127138
128139
最常见的有多行文本的魔法印就是写注释文档了。
129140
例如,如果你要在注释里写一些转义字符,这有可能会报错。
141+
130142
```elixir
131143
@doc """
132144
Converts double-quotes to single-quotes.
@@ -141,6 +153,7 @@ def convert(...)
141153
```
142154

143155
使用```~S```,我们就可以避免问题:
156+
144157
```elixir
145158
@doc ~S"""
146159
Converts double-quotes to single-quotes.
@@ -157,18 +170,22 @@ def convert(...)
157170
### 19.3-自定义魔法印
158171
本章开头提到过,魔法印是可扩展的。事实上,魔法印```~r/foo/i```等于是
159172
用两个参数调用了函数```sigil_r```:
173+
160174
```elixir
161175
iex> sigil_r(<<"foo">>, 'i')
162176
~r"foo"i
163177
```
178+
164179
就是说,我们可以通过该函数阅读魔法印```~r```的文档:
180+
165181
```elixir
166182
iex> h sigil_r
167183
...
168184
```
169185

170186
我们也可以通过实现相应的函数来提供我们自己的魔法印。例如,我们来实现一个```~i(N)```魔法印,
171187
返回整数:
188+
172189
```elixir
173190
iex> defmodule MySigils do
174191
...> def sigil_i(string, []), do: String.to_integer(string)

20-typespecs-behaviors.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Elixir是一门动态类型语言,Elixir中所有数据类型都是在运行
1313

1414
默认地,Elixir提供了一些基础数据类型,比如 `integer` 或者 `pid`。还有其它一些复杂的:
1515
如函数`round/1`,它对一个float类型的数值四舍五入。
16-
它以一个`number`(一个`integer``float`)作为参数,返回一个`integer`
16+
它以一个`number`(一个`integer``float`)作为参数,返回一个`integer`
1717

1818
[round函数的文档](http://elixir-lang.org/docs/stable/elixir/Kernel.html#round/1)
1919
里面描述`round/1`的函数签名为:
@@ -53,11 +53,13 @@ defmodule LousyCalculator do
5353
end
5454
```
5555

56-
从例子中可以看出,元祖是复合类型。每个元祖都定义了其具体元素类型
56+
从例子中可以看出,元组是复合类型。每个元组都定义了其具体元素类型
5757
至于为何是`String.t`而不是`string`,可以参考
5858
[这篇文章](http://elixir-lang.org/docs/stable/elixir/typespecs.html#Notes)
5959

60-
Defining function specs this way works, but it quickly becomes annoying since we're repeating the type `{number, String.t}` over and over. We can use the `@type` directive in order to declare our own custom type.
60+
像这样定义函数规格说明是没问题,但是一次次重复写这种复合类型的
61+
样式名称`{number, String.t}`,很快会厌烦。
62+
我们可以使用`@type`指令来声明我们自定义的类型:
6163

6264
```elixir
6365
defmodule LousyCalculator do
@@ -74,9 +76,9 @@ defmodule LousyCalculator do
7476
end
7577
```
7678

77-
The `@typedoc` directive, similarly to the `@doc` and `@moduledoc` directives, is used to document custom types.
79+
指令`@typedoc`,和`@doc``@moduledoc`指令类似,被用来记录自定义类型。
7880

79-
Custom types defined through `@type` are exported and available outside the module they're defined in:
81+
自定义类型通过`@type`定义,可以从其定义的模块导出并被外界访问:
8082

8183
```elixir
8284
defmodule QuietCalculator do
@@ -88,11 +90,15 @@ defmodule QuietCalculator do
8890
end
8991
```
9092

91-
If you want to keep a custom type private, you can use the `@typep` directive instead of `@type`.
93+
如果想要将某个自定义类型保持私有,可以使用 `@typep` 指令代替 `@type`
9294

93-
### Static code analysis
95+
### 静态代码分析
9496

95-
Typespecs are not only useful to developers and as additional documentation. The Erlang tool [Dialyzer](http://www.erlang.org/doc/man/dialyzer.html), for example, uses typespecs in order to perform static analysis of code. That's why, in the `QuietCalculator` example, we wrote a spec for the `make_quiet/1` function even if it was defined as a private function.
97+
Typespecs的作用不仅仅是被用来作为程序文档说明。举个例子,
98+
Erlang工具[Dialyzer][Dialyzer](http://www.erlang.org/doc/man/dialyzer.html)
99+
使用typespecs来进行代码静态分析。
100+
这就是为什么我们在 `QuiteCalculator` 例子中,
101+
即使 `make_quite/1` 是个私有函数,也写了函数规格说明。
96102

97103
## Behaviours
98104

0 commit comments

Comments
 (0)