2
2
============
3
3
4
4
Elixir对Erlang库提供了完善的交互。实际上,Elixir不是简单地去对Erlang库进行语言包装,
5
- 而是直接连接Erlang的代码。本章将展示一些Elixir中没有,但是常用(常见+有用)的Erlang功能。
5
+ 而是直接连接Erlang的代码(因为同源,Elixir以特定语法来直接调用Erlang库函数)。
6
+ 本章将展示一些Elixir中没有,但是常用(常见+有用)的Erlang功能函数。
6
7
7
- 随着对Elixir更加深入的学习和使用,你可能会更多地参考Erlang的
8
+ > 随着对Elixir更加深入的学习和使用,你可能会更多地阅读参考Erlang的
8
9
[ 标准库手册] ( http://erlang.org/doc/apps/stdlib/index.html ) 。
9
10
10
11
## 二进制串模块
@@ -82,18 +83,14 @@ iex> Base.encode16(:crypto.hash(:sha256, "Elixir"))
82
83
end
83
84
```
84
85
85
- ## The digraph module
86
+ ## 有向图模块
86
87
87
- [ The digraph module] ( http://erlang.org/doc/man/digraph.html ) (as well as
88
- [ digraph_utils] ( http://erlang.org/doc/man/digraph_utils.html ) ) contains
89
- functions for dealing with directed graphs built of vertices and edges.
90
- After constructing the graph, the algorithms in there will help finding
91
- for instance the shortest path between two vertices, or loops in the graph.
88
+ [ 有向图模块] ( http://erlang.org/doc/man/digraph.html ) (以及
89
+ [ 有向图模块工具] ( http://erlang.org/doc/man/digraph_utils.html ) )
90
+ 包含了处理有向图(有丁点和边构成)的函数。
91
+ 创建了一个图实例之后,模块的算法即可帮助找寻顶点最短路径、图中的环等。
92
92
93
- Note that the functions in ` :digraph ` alter the graph structure indirectly
94
- as a side effect, while returning the added vertices or edges.
95
-
96
- Given three vertices, find the shortest path from the first to the last.
93
+ 给出三个顶点,找寻从第一个到最后一个顶点的最短路径:
97
94
98
95
``` iex
99
96
iex> digraph = :digraph.new()
@@ -105,19 +102,16 @@ iex> :digraph.get_short_path(digraph, v0, v2)
105
102
[{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}]
106
103
```
107
104
108
- ## Erlang Term Storage
105
+ ## ETS( Erlang Term Storage):Erlang的“Term存储”机制
109
106
110
- The modules [ ` ets ` ] ( http://erlang.org/doc/man/ets.html ) and
111
- [ ` dets ` ] ( http://erlang.org/doc/man/dets.html ) handle storage of large
112
- data structures in memory or on disk respectively.
107
+ 模块 [ ` ets ` ] ( http://erlang.org/doc/man/ets.html ) 以及
108
+ [ ` dets ` ] ( http://erlang.org/doc/man/dets.html )
109
+ 分别处理在内存中、磁盘上存储大型数据结构。
113
110
114
- ETS lets you create a table containing tuples. By default, ETS tables
115
- are protected, which means only the owner process may write to the table
116
- but any other process can read. ETS has some functionality to be used as
117
- a simple database, a key-value store or as a cache mechanism.
111
+ ETS创建一个表来存储元祖。默认情况下,ETS表是受保护的:只有owner进程才能写表,
112
+ 其它进程只可以读。ETS提供了一些功能,可被当做简单的数据库、键值存储或cache机制使用。
118
113
119
- The functions in the ` ets ` module will modify the state of the table as a
120
- side-effect.
114
+ 作为副作用,` ets ` 模块中的函数会修改表的状态。
121
115
122
116
``` iex
123
117
iex> table = :ets.new(:ets_test, [])
@@ -131,11 +125,10 @@ iex> :ets.i(table)
131
125
<3 > {"India", 1_284_000_000}
132
126
```
133
127
134
- ## The math module
128
+ ## 数学模块
135
129
136
- [ The ` math ` module] ( http://erlang.org/doc/man/math.html ) contains common
137
- mathematical operations covering trigonometry, exponential and logarithmic
138
- functions.
130
+ [ 数学模块] ( http://erlang.org/doc/man/math.html ) 包含了常用数学操作,
131
+ 如三角函数、指数或底数函数等等。
139
132
140
133
``` iex
141
134
iex> angle_45_deg = :math.pi() * 45.0 / 180.0
@@ -147,10 +140,10 @@ iex> :math.log(7.694785265142018e23)
147
140
55.0
148
141
```
149
142
150
- ## The queue module
143
+ ## 队列( queue)模块
151
144
152
- The [ ` queue ` is a data structure ] ( http://erlang.org/doc/man/queue.html )
153
- that implements (double-ended) FIFO (first-in first-out) queues efficiently:
145
+ [ 队列 ` queue ` 是一种数据结构 ] ( http://erlang.org/doc/man/queue.html )
146
+ 实现了双向先进先出的高效率队列:
154
147
155
148
``` iex
156
149
iex> q = :queue.new
@@ -167,10 +160,9 @@ iex> value
167
160
:empty
168
161
```
169
162
170
- ## The rand module
163
+ ## 随机值( rand)模块
171
164
172
- [ ` rand ` has functions] ( http://erlang.org/doc/man/rand.html ) for returning
173
- random values and setting the random seed.
165
+ [ ` rand ` 模块函数] ( http://erlang.org/doc/man/rand.html ) 可以返回随机值或是设置随机seed:
174
166
175
167
``` iex
176
168
iex> :rand.uniform()
@@ -182,20 +174,20 @@ iex> :rand.uniform(6)
182
174
6
183
175
```
184
176
185
- ## The zip and zlib modules
177
+ ## zip和zlib模块
186
178
187
- [ The ` zip ` module ] ( http://erlang.org/doc/man/zip.html ) lets you read and write zip files to and from disk or memory,
188
- as well as extracting file information.
179
+ [ ` zip ` 模块 ] ( http://erlang.org/doc/man/zip.html ) 可以让你读写磁盘或内存中的zip文件,
180
+ 以及提取其文件信息等。
189
181
190
- This code counts the number of files in a zip file:
182
+ 以下代码计算了zip压缩包中的文件数量:
191
183
192
184
``` iex
193
185
iex> :zip.foldl(fn _, _, _, acc -> acc + 1 end, 0, :binary.bin_to_list("file.zip"))
194
186
{:ok, 633}
195
187
```
196
188
197
- [ The ` zlib ` module ] ( http://erlang.org/doc/man/zlib.html ) deals with data compression in zlib format, as found in the
198
- ` gzip ` command.
189
+ [ ` zlib ` 模块 ] ( http://erlang.org/doc/man/zlib.html ) 处理zlib格式的数据压缩
190
+ (如gzip中用的):
199
191
200
192
``` iex
201
193
iex> song = "
0 commit comments