Erlang Libraries
Ringkasan Pelajaran
# Introduction
About
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
timermodule, which provides functions such assleep/1,send_after/2, andsend_interval/2. - The
randmodule, which provides functions such asuniform/0,normal/0, andseed/2. :io_lib.format/2which provides C-style string formatting (using control sequences).- The
mathmodule that provides mathematical functions such assin/1,cos/1,log2/1,log10/1,pow/2, and more. - The
queuemodule which provides a queue data structure. - The
cryptomodule which provides cryptographic functions. - The
zipmodule which provides functions for working with zip archives. - The
calendarmodule which provides functions for working with dates and time, such asiso_week_number/1. To pass aDate,Time, or aNaiveDateTimestruct to one of the Erlang functions, it needs to be changed to the Erlang format with ato_erlfunction 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.
Originally from Exercism elixir concepts