Elixir is a dynamic, functional language designed for building scalable and maintainable applications.
Ruby | syntax | intelligent sugar |
---|---|---|
Erlang | semantics | BEAM, concurrency model |
Lisp | extensibility | metaprogramming, macros |
The syntax is the most obvious difference between Erlang et Elixir but not the only difference.
Both languages shares a common ground: same VM, same OTP library, ... but Elixir also aims to improve some of the Erlang flaws.
https://en.wikipedia.org/wiki/Herb_Sutter#The_Free_Lunch_Is_Over
Concurrency is the composition of independently executing computations.
Call of duty black ops, best selling videogame.
"We’re converting our notifications system from Java to Elixir. The Java version used an Actor system and weighed in at around 10,000 lines of code. The new Elixir system has shrunk this to around 1000 lines. The Elixir based system is also faster and more consistent than the Java one and runs on half the number of servers."
"Our code base is vastly simplified now, and we have less issues with reliability. We are an incredibly peaky business and we find that at the peak of trading we are able to provide our customer with a much more predictable quality product using Erlang,"
What is Elixir/Erlang good for?
Chat servers (WhatsApp, ejabberd)
Game servers (Wooga)
Web frameworks (Phoenix)
Distributed databases (Riak and CouchDB)
Real-time bidding servers
Long-running services/daemons
Command-line applications
Agner erlang: was a Danish mathematician, statistician and engineer, who invented the fields of traffic engineering and queueing theory.
Erlang the movie
Created by Jose Valim
Superb standard library
Added namespaces
Added Structs
Allowed variable rebinding
Added Pipe-operator |>
Added polymorphism
Added Lisp-style macros
Changed syntax
|> — The Pipe Operator
people = DB.find_customers
orders = Orders.for_customers(people)
tax = sales_tax(orders, 2013)
filing = prepare_filing(tax)
|> — The Pipe Operator
prepare_filing(sales_tax(Orders.-
for_customers(DB.find_customers), 2013))
|> — The Pipe Operator
filing = DB.find_customers
|> Orders.for_customers
|> sales_tax(2013)
|> prepare_filing
Pattern matching
iex> a = 1
1
iex> 1 = a
1
iex> { m, n } = { 2, 3 }
{2, 3}
iex> [ e, f, g ] = [ 4, 5, 6 ]
[4, 5, 6]
iex> "Elixir" <> rest = "Elixir Rocks!"
"Elixir Rocks!"
iex> rest
" Rocks!"
iex> [head | tail] = [1, 2, 3]
[1, 2, 3]
iex> head
1
iex> tail
[2, 3]
Recursion and Pattern matching
defmodule Fib do
def fib(0), do: 0
def fib(1), do: 1
def fib(n), do: fib(n-1) + fib(n-2)
end
Recursion and Pattern matching
defmodule MyList do
def sum([], total), do: total
def sum([head | tail], total), do: sum(tail, head+total)
end
MyList.sum([1,2,3,4,5], 0)
Pattern matching
handle_open = fn
({:ok, file}) -> "First line: #{IO.read(file, :line)}"
{_, error} -> "Error: #{:file.format_error(error)}"
end
IO.puts handle_open.(File.open("file.ex"))
Immutability
iex> name = "elixir"
"elixir"
iex> String.capitalize name
"Elixir"
iex> name
"elixir"
“In a functional language, we always transform data. We never modify it in place”
Quote from “Programming Elixir”.
Immutability
m = %{ a: 1, b: 2, c: 3 }
%{a: 1, b: 2, c: 3}
iex> m1 = %{ m | b: "two", c: "three" }
%{a: 1, b: "two", c: "three”}
iex> m2 = %{ m1 | a: "one" }
%{a: "one", b: "two", c: "three"}
1. Mix
A task runner. It is used to start servers, install dependencies, run tests,...
2. Hex
Package manager allowing to download Erlang and Elixir packages
3. Iex
Read eval print loop
Nothing to do, just compile and use your new .ex
you don't need to reboot your app,
static typing would have been complicated with this
> :observer.start
A very useful debug console
Dialyzer: let you do a static analysis
Reltool: tool to make erlang releases
(used by rebar)
Erlang.mk: Alternative to rebar based on a makefile
WombatOAM: commercial product for system supervision
Primitive
OTP
Tasks and Agents
Technical requirement | Server A | Server B |
---|---|---|
Http Server | Nginx | Elixir |
Request Processing | Ruby on rails/Node | Elixir |
Long running requests | Go | Elixir |
Server-Wide State | Redis | Elixir |
Persistable data | Redis & Mongo | Elixir |
Background jobs | Cron, bash scripts | Elixir |
Server crash recovery | Upstart | Elixir |