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.
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
The most commonly used Erlang modules provide functionality that Elixir's standard library lacks. They are:
timer
module, which provides functions such as sleep/1
, send_after/2
, and send_interval/2
.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).math
module that provides mathematical functions such as sin/1
, cos/1
, log2/1
, log10/1
, pow/2
, and more.queue
module which provides a queue data structure.crypto
module which provides cryptographic functions.zip
module which provides functions for working with zip archives.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.