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 Slide

  2. View Slide

  3. View Slide

  4. otpornost na pogreške
    skalabilnost
    responzivnost

    View Slide

  5. Erlang LFE Elixir
    Erlang VM (BEAM)

    View Slide

  6. View Slide

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

    View Slide

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

    View Slide

  9. OS proces
    BEAM proces

    View Slide

  10. scheduler scheduler scheduler scheduler
    BEAM instanca
    CPU CPU CPU CPU

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  19. bank account
    balance

    View Slide

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

    View Slide

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

    View Slide

  22. 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 Slide

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

    View Slide

  24. View Slide

  25. 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 Slide

  26. A
    B
    C
    D
    E
    F

    View Slide

  27. A
    exit notifikacija
    B
    C
    D
    E
    F

    View Slide

  28. S
    S S
    W
    W
    S
    W
    W
    W S
    W
    W

    View Slide

  29. VM
    VM
    VM
    VM
    VM

    View Slide

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

    View Slide

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

    View Slide