Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A sip of Elixir

A sip of Elixir

An introduction to Elixir

Avatar for Emanuele

Emanuele

May 26, 2018
Tweet

Other Decks in Programming

Transcript

  1. Erlang • 1987 - First version written in Prolog. Good

    but slow • Early ’90 - BEAM • 1992 - Erlang in production • In 1998 Ericsson announced the AXD301 switch, containing over a million lines of Erlang and reported to achieve a high availability of nine “9"s. • 1999 Ericsson banned the use of Erlang • 2004 Ericsson lifted the ban
  2. COP Languages In languages like <noname> they forgot about concurrency.

    It either wasn’t designed in from the beginning or else it was added on as an afterthought.
 Armstrong, 2002
  3. COP Languages • Processes are truly independent • No penalty

    for massive parallelism • No unavoidable penalty for distribution • Concurrent behaviour of program same on all OSs • Can deal with failure
  4. COP is nice • The world is parallel • The

    world is distributed • Things fail • Make more powerful by adding more processors • Our brain intuitively understand parallelism
  5. Open Telecom Platform • Delivered with Erlang and written in

    Erlang, it contains: • an Erlang interpreter (which is called BEAM) • an Erlang compiler • a protocol for communication between servers (nodes) • An Object Request Broker • a static analysis tool called Dialyzer • a distributed database server (Mnesia) • many other libraries and utilities
  6. Elixir • R&D project form Plataformatec (2011) • His goals

    were to enable higher extensibility and productivity in the Erlang VM while keeping compatibility with Erlang's ecosystem
  7. Features • Compiles to bytecode for the BEAM • Functional

    language, everything is an expression • Erlang functions can be called without costs • Metaprogramming with macros • Polymorphism via a mechanism called protocols and behaviours • Pattern matching • Expressive language that programmers love (like Ruby!)
  8. Elixir today • Version 1.6.5 • New version every 6

    months (1.7 in July) • Hex package management (~6k packages) • Vibrant community
  9. Processes • Everything runs in a process • Process is

    isolated • Process communicate using messages • Inside a process code runs in a single thread
  10. GenServer • The basic behaviour for a general purpose process

    • Has state • A “public interface” (client api vs. server callbacks) • Callbacks to handle messages and send responses • Terminate callback
  11. GenServer • init(args) :: {:ok, state}
 A sort of “constructor”

    for the server • handle_call(request, from, state) :: {:reply, reply, new_state}
 Handle synchronous messages • handle_cast(request, state) :: {:noreply, new_state}
 Handle asynchronous messages Redux?
  12. Actor model • GenServer is the basic block for the

    actor model • Code inside GenServer is single thread • GenServer has it’s own message queue • GenServer can create other Genservers
  13. Supervisor • Restarting strategy • :one_for_one - if a child

    process terminates, only that process is restarted. • :one_for_all - if a child process terminates, all other child processes are terminated and then all child processes are restarted. • :rest_for_one - if a child process terminates, the “rest” of the child processes are terminated. Then the terminated child process and the rest of the child processes are restarted.
  14. Applications • Applications are a bundle of sub-application • Application

    consists of a manifest • Dependencies are sub-application • Usually it starts the main supervisors and the dependent applications
  15. Other gems • Macros • Hot code swapping • Umbrella

    projects • Remote debugging and inspection • Community ❤
  16. Tooling • mix is rake for Elixir • https://hex.pm is

    the package registry • Spec and Dyalizer • Release with distillery (erts inside…no prerequisites!)
  17. FAQ • Should I learn Erlang to use Elixir? •

    Is it fast? • To spawn or not to spawn? • Does it run on windows?