Tracks
/
Elixir
Elixir
/
Syllabus
/
Erlang Libraries
Er

Erlang Libraries in Elixir

6 exercises

About Erlang Libraries

Elixir code runs in the BEAM virtual machine. BEAM is part of the Erlang Run-Time System. Being inspired by Erlang, and sharing its run environment, Elixir provides great interoperability with Erlang libraries. This means that Elixir developers can use Erlang libraries from within their Elixir code. In fact, writing Elixir libraries for functionality already provided by Erlang libraries is discouraged in the Elixir community.

As a result, certain functionality, like mathematical operations or timer functions, is only available in Elixir via Erlang.

This close relationship between Elixir and Erlang means that to become a proficient Elixir developer, you might want to invest some time in learning the basic syntax of Erlang.

How to use

Erlang's standard library is available for use in our Elixir code without any extra steps necessary.

Erlang functions can be called in the same way we call Elixir functions, with one small difference. Erlang module names are snake_case atoms. For example, to call the Erlang pi/0 function from the math module, one would write:

:math.pi()
# => 3.141592653589793

Most commonly used Erlang modules

The most commonly used Erlang modules provide functionality that Elixir's standard library lacks. They are:

  • The timer module, which provides functions such as sleep/1, send_after/2, and send_interval/2.
  • The rand module, which provides functions such as uniform/0, normal/0, and seed/2.
  • :io_lib.format/2 which provides C-style string formatting (using control sequences).
  • The math module that provides mathematical functions such as sin/1, cos/1, log2/1, log10/1, pow/2, and more.
  • The queue module which provides a queue data structure.
  • The crypto module which provides cryptographic functions.
  • The zip module which provides functions for working with zip archives.
  • The calendar module which provides functions for working with dates and time, such as iso_week_number/1. To pass a Date, Time, or a NaiveDateTime struct to one of the Erlang functions, it needs to be changed to the Erlang format with a to_erl function from the corresponding module.

To discover Erlang's standard library, explore the STDLIB Reference Manual.

Elixir interoperability with Erlang libraries is not limited to Erlang's standard library. Any Erlang library can be used in Elixir's code.

Edit via GitHub The link opens in a new window or tab

Learn Erlang Libraries

Practicing is locked

Unlock 1 more exercise to practice Erlang Libraries