Elixir
-
Upload
marek-kirejczyk -
Category
Technology
-
view
179 -
download
3
Transcript of Elixir
Some random Rails problems
● gem explosion (e.g. background processing)● websockets● scaling, deployment, transparent failover● ddd, dci, microservices
Elixir?
● Purely functional (no state!)● Concurrency oriented, distributed● Fault tolerant● Strong metaprogramming● Growing Ecosystem
Some basic data types
● (Immutable) Lists: [1, 2, 3]● (Immutable) Tuples: {:meaning_of_life, 42}● (Immutable) Strings: “Hello Elixir”
List & recursion
def sum_list([head|tail], accumulator) do sum_list(tail, head + accumulator)end
sum_list([1,2,3], 0)
Protocols implementationdefimpl Blank, for: Integer do def blank?(_), do: falseend
defimpl Blank, for: List do def blank?([]), do: true def blank?(_), do: falseend
Pattern matching and tuples
{a, b, c} = {:hello, "world", 42}
{:hello, _, c} = {:hello, "world", 42}
{:yellow, _, c} = {:hello, "world", 42}
Why Pattern matching
● tuples are used all the time for everything (remember: no classes!)
● exceptions usage is discouraged● managing messages (see later)
Quote
quote do: sum(1, 2, 3){:sum, [], [1, 2, 3]}
quote do: 1 + 2{:+, [context: Elixir, import: Kernel], [1, 2]}
defmacro macro_unless(clause, expression) do quote do if(!unquote(clause), do: unquote(expression)) endend