Code Concur

Lennart Fridén

codecoupled.org | @DevLCSC | github.com/DevL

Stockholm Google Developer Group March 2016 Meetup

Elixir is a dynamic, functional language designed for building scalable and maintainable applications.
Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.

Influences

Elixir is built on Erlang...


defmodule Hello do
  def world do
    IO.puts "Hello GDG!"
  end
end
		

-module(hello).
-export([world/0]).

world() ->
  io:fwrite("Hello, GDG\n").
		

...runs on the Erlang virtual machine (BEAM)

Ruby


defmodule Hello do
  def world do
    IO.puts "Hello GDG!"
  end
end
		

class Hello
  def world
    puts "Hello GDG!"
  end
end
		

Erlang


defmodule Request do
  def make_it do
    handle(some_function_that_can_return_different_things)
  end

  defp handle({:ok, result}), do: result
  defp handle({:error, message}), do: Logger.error(message)
  defp handle(_), do: Logger.error("An unexpected thing happened")
end
		

Clojure


defmacro divisible_by?(number, divisor) do
  quote do
    rem(unquote(number), unquote(divisor)) == 0
  end
end
		

def double_everything_except_fivers(a_bunch_of_numbers)
  Enum.map(
    Enum.reject(a_bunch_of_numbers,
                fn(element) -> divisible_by?(element, 5) end),
    fn(element) -> element * 2 end)
end
		

ML


def double_everything_except_fivers(a_bunch_of_numbers) do
  a_bunch_of_numbers
  |> Enum.reject(fn(number) -> divisible_by?(number, 5) end)
  |> Enum.map(fn(number) -> number * 2 end)
end
		

def double_everything_except_fivers(a_bunch_of_numbers) do
  a_bunch_of_numbers
  |> Enum.reject(&divisible_by_five?(&1))
  |> Enum.map(&double(&1))
end

defp divisible_by_five?(value), do: divisible_by?(value, 5)

defp double(value), do: value * 2
		

Python


@doc """
Returns a `Stream` of datetimes, starting with `datetime`, stepping backward one second at a time.
## Examples
    iex> {{2015, 2, 27}, {18, 30, 45}} |> all_seconds_before |> Enum.take(3)
    [{{2015, 2, 27}, {18, 30, 45}},
     {{2015, 2, 27}, {18, 30, 44}},
     {{2015, 2, 27}, {18, 30, 43}}]
"""
@spec all_seconds_before(GoodTimes.datetime) :: Enumerable.t
def all_seconds_before(datetime) do
  datetime |> Stream.iterate(&GoodTimes.a_second_before/1)
end
		

Interactive Elixir (1.2.3) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> h GoodTimes.Generate.all_seconds_before

                        def all_seconds_before(datetime)

Returns a Stream of datetimes, starting with datetime, stepping backward one
second at a time.

Examples

┃ iex> {{2015, 2, 27}, {18, 30, 45}} |> all_seconds_before |> Enum.take(3)
┃ [{{2015, 2, 27}, {18, 30, 45}},
┃  {{2015, 2, 27}, {18, 30, 44}},
┃  {{2015, 2, 27}, {18, 30, 43}}]
		

Ecosystem

Tooling

  • iex - the REPL
  • mix - the build tool
  • hex - the package manager

Libraries and interop

  • Elixir standard library
  • hex packages (hex.pm)
  • Erlang standard library
  • OTP
  • Other BEAM languages

Philosophy

Massive concurrency...

...and parallelism

We do not have ONE web-server handling 2 millions sessions.
We have 2 million webservers handling one session each.

Actors

Immutable data

OTP

Let it crash!

Supervision trees

Elixir's hollistic approach

Learning resources

  • Tutorials - elixir-lang.org, elixirschool.com
  • Exercises - exercism.io
  • Books - Programming Elixir, Programming Phoenix
  • Screencasts - Elixir Sips, LearnElixir.tv
  • Conferences - ElixirConf EU, ElixirConf US
  • Meetups - Stockholm Elixir

Code Concur

Lennart Fridén

codecoupled.org | @DevLCSC | github.com/DevL

Stockholm Google Developer Group March 2016 Meetup