1121-Erlang库
22============
33
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功能。
86
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 ) 。
129
13- ## The binary module
10+ ## 二进制串模块
1411
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编码的:
1815
1916``` iex
2017iex> String.to_char_list "Ø"
@@ -23,25 +20,22 @@ iex> :binary.bin_to_list "Ø"
2320[195, 152]
2421```
2522
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 ` 是原始的数据字节。
2825
29- ## Formatted text output
26+ ## 格式化的字符串输出
3027
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 ` 函数。作为一个可选项,你可以使用字符串插值来完成同样的功能:
3429
3530``` iex
3631iex> f = Float.to_string(:math.pi, decimals: 3) |> String.rjust(10)
3732iex> str = "Pi is approximately given by: #{f}"
3833"Pi is approximately given by: 3.142"
3934```
4035
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 ) :
4539
4640``` iex
4741iex> :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
5145"Pi is approximately given by: 3.142\n"
5246```
5347
54- Also note that Erlang's formatting functions require special attention to
55- Unicode handling.
48+ 另外需要注意的是Erlang的格式化函数中对Unicode的处理。
5649
57- ## The calendar module
50+ ## 日历模块
5851
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+ 以及其它一些时间函数。
6254
6355``` iex
6456iex> :calendar.day_of_the_week(1980, 6, 28)
@@ -70,20 +62,19 @@ iex> time
7062{22, 4, 55}
7163```
7264
73- ## The crypto module
65+ ## 加密模块
7466
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+ 加密等功能函数:
7769
7870``` iex
7971iex> Base.encode16(:crypto.hash(:sha256, "Elixir"))
8072"3315715A7A3AD57428298676C5AE465DADA38D951BDFAC9348A8A31E9C7401CB"
8173```
8274
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 ` 来加载该模块:
8778
8879``` elixir
8980 def application do
0 commit comments