@@ -7,24 +7,20 @@ title: Erlang libraries
7
7
8
8
{% include toc.html %}
9
9
10
- Elixir provides excellent interoperability with Erlang libraries. You will not
11
- find Elixir wrappers for libraries and applications from the Erlang standard
12
- library in the Elixir standard library. Instead, you are encouraged to use the
13
- Erlang libraries directly. In this section we will present some of the most
14
- common and useful Erlang libraries that are not found in Elixir core libraries.
10
+ Elixir provides excellent interoperability with Erlang libraries. In fact,
11
+ Elixir discourages simply wrapping Erlang libraries in favor of directly
12
+ interfacing with Erlang code. In this section we will present some of the
13
+ most common and useful Erlang functionality that are not found in Elixir.
15
14
16
15
As you grow more proficient in Elixir, you may want to explore the Erlang
17
16
[ STDLIB Reference Manual] ( http://erlang.org/doc/apps/stdlib/index.html ) in more
18
17
detail.
19
18
20
-
21
-
22
-
23
19
## The binary module
24
20
25
- The built-in Elixir String module handles binaries that are encoded in utf-8
26
- format. The binary module is useful when you are dealing with binary data that
27
- is not necessarily utf -8 encoded.
21
+ The built-in Elixir String module handles binaries that are UTF-8 encoded.
22
+ [ The binary module] ( http://erlang.org/doc/man/binary.html ) is useful when
23
+ you are dealing with binary data that is not necessarily UTF -8 encoded.
28
24
29
25
``` iex
30
26
iex> String.to_char_list "Ø"
@@ -33,40 +29,42 @@ iex> :binary.bin_to_list "Ø"
33
29
[195, 152]
34
30
```
35
31
36
- The above example shows the difference; the ` String ` module returns utf -8
32
+ The above example shows the difference; the ` String ` module returns UTF -8
37
33
codepoints, while ` :binary ` deals with raw data bytes.
38
34
39
35
## Formatted text output
40
36
41
- Elixir does not contain a function similar to C ` printf ` . An option is relying
42
- on string interpolation that is built into the language to do this, eg.:
37
+ Elixir does not contain a function similar to ` printf ` found in C and other
38
+ languages. An option is to rely on string interpolation to achieve similar
39
+ result:
43
40
44
41
``` iex
45
42
iex> f = Float.to_string(:math.pi, decimals: 3) |> String.rjust(10)
46
43
iex> str = "Pi is approximately given by: #{f}"
47
44
"Pi is approximately given by: 3.142"
48
45
```
49
46
50
- Alternatively, the Erlang standard library functions ` :io.format\ 2 ` and
51
- ` :io_lib.format\ 2 ` may be used. The first formats to terminal output, while the
52
- second formats to a string . The format specifiers differ from ` printf ` , refer
53
- to the Erlang documentation for details.
47
+ Alternatively, the Erlang standard library functions ` :io.format/ 2 ` and
48
+ ` :io_lib.format/ 2 ` may be used. The first formats to terminal output, while
49
+ the second formats to an iolist . The format specifiers differ from ` printf ` ,
50
+ [ refer to the Erlang documentation for details] ( http://erlang.org/doc/man/io.html#format-1 ) .
54
51
55
52
``` iex
56
53
iex> :io.format("Pi is approximately given by:~10.3f~n", [:math.pi])
57
54
Pi is approximately given by: 3.142
58
55
:ok
59
- iex> str = :io_lib.format("Pi is approximately given by:~10.3f~n", [:math.pi]) |> IO.iodata_to_binary
56
+ iex> to_string :io_lib.format("Pi is approximately given by:~10.3f~n", [:math.pi])
60
57
"Pi is approximately given by: 3.142\n"
61
58
```
62
59
63
60
Also note that Erlangs formatting functions require special attention to
64
- unicode handling.
61
+ Unicode handling.
65
62
66
63
## The calendar module
67
64
68
- The calendar module contains functions for conversion between local and
69
- universal time, as well as time conversion functions.
65
+ [ The calendar module] ( http://erlang.org/doc/man/calendar.html ) contains
66
+ functions for conversion between local and universal time, as well as
67
+ time conversion functions.
70
68
71
69
``` iex
72
70
iex> :calendar.day_of_the_week(1980, 6, 28)
@@ -77,37 +75,35 @@ iex> :calendar.now_to_local_time(:erlang.timestamp)
77
75
78
76
## The crypto module
79
77
80
- The crypto module contains hashing functions, digital signatures, encryption
81
- and more. The library also contains the ` crypto ` application that must be
82
- registered as a dependency to your application for some of this functionality
83
- to work.
78
+ [ The crypto module] ( http://erlang.org/doc/man/crypto.html ) contains hashing
79
+ functions, digital signatures, encryption and more:
84
80
85
- To do this, edit your ` mix.exs ` file to include:
81
+ ``` iex
82
+ iex> Base.encode16(:crypto.hash(:sha256, "Elixir"))
83
+ "3315715A7A3AD57428298676C5AE465DADA38D951BDFAC9348A8A31E9C7401CB"
84
+ ```
85
+
86
+ The ` :crypto ` module is not part of the Erlang standard library, but is
87
+ included with the Erlang distribution. This means you must list ` :crypto `
88
+ in your project's applications list whenever you use it. To do this,
89
+ edit your ` mix.exs ` file to include:
86
90
87
91
``` elixir
88
92
def application do
89
93
[applications: [:crypto ]]
90
94
end
91
95
```
92
96
93
- The ` crypto ` module is not part of the Erlang standard library, but is included
94
- with the Erlang distribution. The documentation is found at
95
- [ this page] ( http://erlang.org/doc/man/crypto.html ) .
96
-
97
- ``` iex
98
- iex> Base.encode16(:crypto.hash(:sha256, "Elixir"))
99
- "3315715A7A3AD57428298676C5AE465DADA38D951BDFAC9348A8A31E9C7401CB"
100
- ```
101
-
102
97
## The digraph module
103
98
104
- The ` digraph ` and ` digraph_utils ` modules contain functions for dealing with
105
- directed graphs built of vertices and edges. After constructing the graph, the
106
- algorithms in here will help finding for instance the shortest path between two
107
- vertices, or loops in the graph.
99
+ [ The digraph module] ( http://erlang.org/doc/man/digraph.html ) (as well as
100
+ [ digraph_utils] ( http://erlang.org/doc/man/digraph_utils.html ) ) contains
101
+ functions for dealing with directed graphs built of vertices and edges.
102
+ After constructing the graph, the algorithms in there will help finding
103
+ for instance the shortest path between two vertices, or loops in the graph.
108
104
109
- Note that the functions in : digraph alter the graph structure indirectly as a
110
- side effect, while returning the added vertices or edges.
105
+ Note that the functions in ` :digraph ` alter the graph structure indirectly
106
+ as a side effect, while returning the added vertices or edges.
111
107
112
108
Given three vertices, find the shortest path from the first to the last.
113
109
@@ -122,19 +118,19 @@ iex> :digraph.get_short_path(digraph, v0, v2)
122
118
[{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}]
123
119
```
124
120
125
-
126
121
## Erlang Term Storage
127
122
128
- The modules ` ets ` and ` dets ` handle storage of large data structures in memory
129
- or on disk respectively.
123
+ The modules [ ` ets ` ] ( http://erlang.org/doc/man/ets.html ) and
124
+ [ ` dets ` ] ( http://erlang.org/doc/man/dets.html ) handle storage of large
125
+ data structures in memory or on disk respectively.
130
126
131
- ETS lets you create a table containing tuples that is owned by a single
132
- process. For large amounts of data, ETS may be more performant than storing
133
- data as large Elixir data structures . ETS has some functionality to be used as
134
- a simple database or key-value store.
127
+ ETS lets you create a table containing tuples. By default, ETS tables
128
+ are protected, which means only the owner process may write to the table
129
+ but any other process can read . ETS has some functionality to be used as
130
+ a simple database, a key-value store or as a cache mechanism .
135
131
136
- The functions in the ` ets ` module will modify the state of the table as a side
137
- effect.
132
+ The functions in the ` ets ` module will modify the state of the table as a
133
+ side- effect.
138
134
139
135
``` iex
140
136
iex> table = :ets.new(:ets_test, [])
@@ -147,12 +143,11 @@ iex> :ets.i(table)
147
143
<3 > {#{name => <<"India">>,population => 1284000000}}
148
144
```
149
145
150
- ETS is described in more detail in it's own section.
151
-
152
146
## The math module
153
147
154
- The ` math ` module contains common mathematical operations covering trigonometry,
155
- exponential and logarithmic functions.
148
+ [ The ` math ` module] ( http://erlang.org/doc/man/math.html ) contains common
149
+ mathematical operations covering trigonometry, exponential and logarithmic
150
+ functions.
156
151
157
152
``` iex
158
153
iex> angle_45_deg = :math.pi() * 45.0 / 180.0
@@ -164,15 +159,10 @@ iex> :math.log(7.694785265142018e23)
164
159
55.0
165
160
```
166
161
167
-
168
162
## The queue module
169
163
170
- The ` queue ` is a data structure that allows efficient FIFO (first in first out)
171
- operation.
172
-
173
- A regular Elixir list may not be performant as removing the first element in
174
- the list requires building a new list with the remaining elements, not reusing
175
- any data.
164
+ The [ ` queue ` is a data structure] ( http://erlang.org/doc/man/queue.html )
165
+ that implements (double-ended) FIFO (first-in first-out) queues efficiently:
176
166
177
167
``` iex
178
168
iex> q = :queue.new
@@ -191,8 +181,8 @@ iex> {_, q} = :queue.out(q)
191
181
192
182
## The rand module
193
183
194
- This module has functions for returning random values and setting the random
195
- seed.
184
+ [ ` rand ` has functions] ( http://erlang.org/doc/man/rand.html ) for returning
185
+ random values and setting the random seed.
196
186
197
187
``` iex
198
188
iex> :rand.uniform()
@@ -204,9 +194,9 @@ iex> :rand.uniform(6)
204
194
6
205
195
```
206
196
207
- ## The zlib and zip modules
197
+ ## The zip and zlib modules
208
198
209
- The ` zip ` module lets you read and write zip files to and from disk or memory,
199
+ [ The ` zip ` module] ( http://erlang.org/doc/man/zip.html ) lets you read and write zip files to and from disk or memory,
210
200
as well as extracting file information.
211
201
212
202
This code counts the number of files in a zip file:
@@ -216,7 +206,7 @@ iex> :zip.foldl(fn _, _, _, acc -> acc + 1 end, 0, :binary.bin_to_list("file.zip
216
206
{:ok, 633}
217
207
```
218
208
219
- The ` zlib ` module deals with data compression in zlib format, as found in the
209
+ [ The ` zlib ` module] ( http://erlang.org/doc/man/zlib.html ) deals with data compression in zlib format, as found in the
220
210
` gzip ` command.
221
211
222
212
``` iex
0 commit comments