Skip to content

Commit 9cdcbf7

Browse files
author
José Valim
committed
Tidy up erlang modules
1 parent 5bda264 commit 9cdcbf7

File tree

1 file changed

+57
-67
lines changed

1 file changed

+57
-67
lines changed

getting-started/erlang-libraries.markdown

+57-67
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,20 @@ title: Erlang libraries
77

88
{% include toc.html %}
99

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.
1514

1615
As you grow more proficient in Elixir, you may want to explore the Erlang
1716
[STDLIB Reference Manual](http://erlang.org/doc/apps/stdlib/index.html) in more
1817
detail.
1918

20-
21-
22-
2319
## The binary module
2420

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.
2824

2925
```iex
3026
iex> String.to_char_list "Ø"
@@ -33,40 +29,42 @@ iex> :binary.bin_to_list "Ø"
3329
[195, 152]
3430
```
3531

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
3733
codepoints, while `:binary` deals with raw data bytes.
3834

3935
## Formatted text output
4036

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:
4340

4441
```iex
4542
iex> f = Float.to_string(:math.pi, decimals: 3) |> String.rjust(10)
4643
iex> str = "Pi is approximately given by: #{f}"
4744
"Pi is approximately given by: 3.142"
4845
```
4946

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).
5451

5552
```iex
5653
iex> :io.format("Pi is approximately given by:~10.3f~n", [:math.pi])
5754
Pi is approximately given by: 3.142
5855
: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])
6057
"Pi is approximately given by: 3.142\n"
6158
```
6259

6360
Also note that Erlangs formatting functions require special attention to
64-
unicode handling.
61+
Unicode handling.
6562

6663
## The calendar module
6764

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.
7068

7169
```iex
7270
iex> :calendar.day_of_the_week(1980, 6, 28)
@@ -77,37 +75,35 @@ iex> :calendar.now_to_local_time(:erlang.timestamp)
7775

7876
## The crypto module
7977

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:
8480

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:
8690

8791
```elixir
8892
def application do
8993
[applications: [:crypto]]
9094
end
9195
```
9296

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-
10297
## The digraph module
10398

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.
108104

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.
111107

112108
Given three vertices, find the shortest path from the first to the last.
113109

@@ -122,19 +118,19 @@ iex> :digraph.get_short_path(digraph, v0, v2)
122118
[{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}]
123119
```
124120

125-
126121
## Erlang Term Storage
127122

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.
130126

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.
135131

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.
138134

139135
```iex
140136
iex> table = :ets.new(:ets_test, [])
@@ -147,12 +143,11 @@ iex> :ets.i(table)
147143
<3 > {#{name => <<"India">>,population => 1284000000}}
148144
```
149145

150-
ETS is described in more detail in it's own section.
151-
152146
## The math module
153147

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.
156151

157152
```iex
158153
iex> angle_45_deg = :math.pi() * 45.0 / 180.0
@@ -164,15 +159,10 @@ iex> :math.log(7.694785265142018e23)
164159
55.0
165160
```
166161

167-
168162
## The queue module
169163

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:
176166

177167
```iex
178168
iex> q = :queue.new
@@ -191,8 +181,8 @@ iex> {_, q} = :queue.out(q)
191181

192182
## The rand module
193183

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.
196186

197187
```iex
198188
iex> :rand.uniform()
@@ -204,9 +194,9 @@ iex> :rand.uniform(6)
204194
6
205195
```
206196

207-
## The zlib and zip modules
197+
## The zip and zlib modules
208198

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,
210200
as well as extracting file information.
211201

212202
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
216206
{:ok, 633}
217207
```
218208

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
220210
`gzip` command.
221211

222212
```iex

0 commit comments

Comments
 (0)