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

Elixir - valentine edition

Saša Jurić
February 14, 2017

Elixir - valentine edition

Saša Jurić

February 14, 2017
Tweet

More Decks by Saša Jurić

Other Decks in Programming

Transcript

  1. Elixir
    @sasajuric
    aircloak.com

    View full-size slide

  2. otpornost na pogreške
    skalabilnost
    responzivnost

    View full-size slide

  3. Erlang LFE Elixir
    Erlang VM (BEAM)

    View full-size slide

  4. v1 = f1(p1, p2, ...)
    v2 = f2(...)
    ...

    View full-size slide

  5. spawn(fn -> ... end)
    A
    B

    View full-size slide

  6. OS proces
    BEAM proces

    View full-size slide

  7. scheduler scheduler scheduler scheduler
    BEAM instanca
    CPU CPU CPU CPU

    View full-size slide

  8. pid = spawn(fn -> ... end)

    View full-size slide

  9. pid = spawn(fn -> ... end)
    send(pid, :ping)

    View full-size slide

  10. spawn(fn ->
    receive do
    :ping ->
    # ...
    end
    end)

    View full-size slide

  11. send(pid, {:ping, self()})

    View full-size slide

  12. spawn(fn ->
    receive do
    {:ping, caller_pid} ->
    send(caller_pid, :pong)
    end
    end)

    View full-size slide

  13. send(pid, {:ping, self()})
    receive do
    :pong ->
    # ...
    after :timer.seconds(5) ->
    # ...
    end

    View full-size slide

  14. pid = spawn(fn ->
    server_loop(state)
    end)

    View full-size slide

  15. defp server_loop(state) do
    receive do
    message ->
    new_state = handle(state, message)
    server_loop(new_state)
    end
    end

    View full-size slide

  16. bank account
    balance

    View full-size slide

  17. send(pid, {:deposit, 100})
    send(pid, {:withdraw, 100})
    send(pid, {:balance, self()})
    receive do
    balance ->
    # ...
    end

    View full-size slide

  18. spawn(fn ->
    bank_account_loop(0)
    end)

    View full-size slide

  19. def bank_account_loop(balance) do
    receive do
    {:deposit, amount} ->
    bank_account_loop(balance + amount)
    {:withdraw, amount} ->
    bank_account_loop(balance - amount)
    {:balance, caller_pid} ->
    send(caller_pid, balance)
    bank_account_loop(balance)
    end
    end

    View full-size slide

  20. bank account
    {:deposit, amount}
    {:withdraw, amount}
    :balance

    View full-size slide

  21. db connection
    background job
    request handler
    state
    request handler
    request handler
    request handler
    request handler
    request handler state
    state
    state
    background job
    background job
    background job
    db connection db connection
    db connection

    View full-size slide

  22. A
    exit notifikacija
    B
    C
    D
    E
    F

    View full-size slide

  23. S
    S S
    W
    W
    S
    W
    W
    W S
    W
    W

    View full-size slide

  24. VM
    send(pid, message)
    VM 1
    send(pid, message)
    VM 2

    View full-size slide

  25. elixir-lang.org
    phoenixframework.org
    adventofcode.com
    exercism.io
    elixirforum.com
    meetup.com/lambdazagreb/

    View full-size slide