1
1
21-Erlang库
2
2
============
3
3
4
- Elixir provides excellent interoperability with Erlang libraries. In fact,
5
- Elixir discourages simply wrapping Erlang libraries in favor of directly
6
- interfacing with Erlang code. In this section we will present some of the
7
- most common and useful Erlang functionality that is not found in Elixir.
4
+ Elixir对Erlang库提供了完善的交互。实际上,Elixir不是简单地去对Erlang库进行语言包装,
5
+ 而是直接连接Erlang的代码。本章将展示一些Elixir中没有,但是常用(常见+有用)的Erlang功能。
8
6
9
- As you grow more proficient in Elixir, you may want to explore the Erlang
10
- [ STDLIB Reference Manual] ( http://erlang.org/doc/apps/stdlib/index.html ) in more
11
- detail.
7
+ 随着对Elixir更加深入的学习和使用,你可能会更多地参考Erlang的
8
+ [ 标准库手册] ( http://erlang.org/doc/apps/stdlib/index.html ) 。
12
9
13
- ## The binary module
10
+ ## 二进制串模块
14
11
15
- The built-in Elixir String module handles binaries that are UTF-8 encoded.
16
- [ The binary module ] ( http://erlang.org/doc/man/binary.html ) is useful when
17
- you are dealing with binary data that is not necessarily UTF-8 encoded.
12
+ 内建的Elixir字符串模块处理UTF-8编码过的二进制串( binaries)。而Erlang的
13
+ [ 二进制串模块 ] ( http://erlang.org/doc/man/binary.html ) 可能对你更加有用,
14
+ 因为它可以处理的二进制串不一定非要是UTF-8编码的:
18
15
19
16
``` iex
20
17
iex> String.to_char_list "Ø"
@@ -23,25 +20,22 @@ iex> :binary.bin_to_list "Ø"
23
20
[195, 152]
24
21
```
25
22
26
- The above example shows the difference; the ` String ` module returns UTF-8
27
- codepoints, while ` :binary ` deals with raw data bytes.
23
+ 从上面的例子你就能看出一些区别来了; ` String ` 模块返回UTF-8的字符码,
24
+ 而 ` :binary ` 是原始的数据字节。
28
25
29
- ## Formatted text output
26
+ ## 格式化的字符串输出
30
27
31
- Elixir does not contain a function similar to ` printf ` found in C and other
32
- languages. An option is to rely on string interpolation to achieve a similar
33
- result:
28
+ Elixir中并没有类似于C中的` printf ` 函数。作为一个可选项,你可以使用字符串插值来完成同样的功能:
34
29
35
30
``` iex
36
31
iex> f = Float.to_string(:math.pi, decimals: 3) |> String.rjust(10)
37
32
iex> str = "Pi is approximately given by: #{f}"
38
33
"Pi is approximately given by: 3.142"
39
34
```
40
35
41
- Alternatively, the Erlang standard library functions ` :io.format/2 ` and
42
- ` :io_lib.format/2 ` may be used. The first formats to terminal output, while
43
- the second formats to an iolist. The format specifiers differ from ` printf ` ,
44
- [ refer to the Erlang documentation for details] ( http://erlang.org/doc/man/io.html#format-1 ) .
36
+ 另外,还可以用Erlang标准库中的` :io.format/2 ` 和` :io_lib.format/2 ` 函数。
37
+ 第一个格式化后输出到终端,而第二个输出到一个iolist。具体格式化的语法和` pringf ` 略有区别,
38
+ 详见[ Erlang文档] ( http://erlang.org/doc/man/io.html#format-1 ) :
45
39
46
40
``` iex
47
41
iex> :io.format("Pi is approximately given by:~10.3f~n", [:math.pi])
@@ -51,14 +45,12 @@ iex> to_string :io_lib.format("Pi is approximately given by:~10.3f~n", [:math.pi
51
45
"Pi is approximately given by: 3.142\n"
52
46
```
53
47
54
- Also note that Erlang's formatting functions require special attention to
55
- Unicode handling.
48
+ 另外需要注意的是Erlang的格式化函数中对Unicode的处理。
56
49
57
- ## The calendar module
50
+ ## 日历模块
58
51
59
- [ The calendar module] ( http://erlang.org/doc/man/calendar.html ) contains
60
- functions for conversion between local and universal time, as well as
61
- time conversion functions.
52
+ [ 日历模块] ( http://erlang.org/doc/man/calendar.html ) 包含本地时间和标准时间的转换函数,
53
+ 以及其它一些时间函数。
62
54
63
55
``` iex
64
56
iex> :calendar.day_of_the_week(1980, 6, 28)
@@ -70,20 +62,19 @@ iex> time
70
62
{22, 4, 55}
71
63
```
72
64
73
- ## The crypto module
65
+ ## 加密模块
74
66
75
- [ The crypto module ] ( http://erlang.org/doc/man/crypto.html ) contains hashing
76
- functions, digital signatures, encryption and more:
67
+ [ 加密模块 ] ( http://erlang.org/doc/man/crypto.html ) 包含哈希方法,数字签名,
68
+ 加密等功能函数:
77
69
78
70
``` iex
79
71
iex> Base.encode16(:crypto.hash(:sha256, "Elixir"))
80
72
"3315715A7A3AD57428298676C5AE465DADA38D951BDFAC9348A8A31E9C7401CB"
81
73
```
82
74
83
- The ` :crypto ` module is not part of the Erlang standard library, but is
84
- included with the Erlang distribution. This means you must list ` :crypto `
85
- in your project's applications list whenever you use it. To do this,
86
- edit your ` mix.exs ` file to include:
75
+ ` :crypto ` 模块不是Erlang的标准库,但是包含在了Erlang发行包中。
76
+ 这要求你必须在项目的配置文件中列出` :crypto ` 模块作为依赖项。
77
+ 通过修改` mix.exs ` 来加载该模块:
87
78
88
79
``` elixir
89
80
def application do
0 commit comments