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

5 minutes with Elixir

5 minutes with Elixir

My lightning talk from Brighton Ruby 2016 where we tour the highlights of Elixir and the surrounding ecosystem.

Andy Pike

July 05, 2016
Tweet

More Decks by Andy Pike

Other Decks in Programming

Transcript

  1. NJOVUFTXJUI&MJYJS
    !BOEZQJLF

    View full-size slide

  2. Hello Mike
    Hello Robert Hello Joe
    Erlang The Movie
    https://youtu.be/xrIjfIjssLE

    View full-size slide


  3. defmodule Remark do
    def question?(remark) do
    String.ends_with?(remark, "?")
    end
    end

    View full-size slide

  4. "Brighton Ruby"
    |> String.upcase
    |> String.split
    # => ["BRIGHTON", "RUBY"]

    View full-size slide

  5. String.ends_with?("Brighton Ruby", "y")
    # same as:
    "Brighton Ruby"
    |> String.ends_with?("y")

    View full-size slide

  6. ["Brighton", language] = ["Brighton", "Ruby"]
    "Ruby"
    ["Brighton", language] = ["London", "Ruby"]
    ** (MatchError) no match of right hand side…

    View full-size slide

  7. def convert(units, :miles_to_km) do
    units * 1.6
    end
    def convert(units, :km_to_miles) do
    units / 1.6
    end
    IO.puts convert(16, :km_to_miles)
    # => 10.0

    View full-size slide

  8. spawn fn ->
    IO.puts "Doing work in a process"
    end

    View full-size slide

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

    View full-size slide

  10. defmodule Brighton do
    def hello do
    receive do
    { :say, text } -> IO.puts(text)
    { :think, text } -> Logger.debug(text)
    end
    end
    end
    pid = spawn(Brighton, :hello, [])
    send pid, { :say, "Hello Brighton Ruby" }

    View full-size slide

  11. 051
    Open Telephony Protocol

    View full-size slide

  12. {:ok, account} = GenServer.start(BankAccount, 0)
    GenServer.cast(account, {:deposit, 50})
    GenServer.cast(account, {:withdraw, 25})

    View full-size slide

  13. balance = GenServer.call(account, :balance)
    # => 25

    View full-size slide

  14. account = BankAccount.start(0)
    BankAccount.deposit(account, 50)
    BankAccount.withdraw(account, 25)
    BankAccount.balance(account)
    # => 25

    View full-size slide

  15. Supervisor
    Supervisor Supervisor Supervisor
    Process Process Process Supervisor
    Process

    View full-size slide

  16. Framework Throughput (req/s)
    Phoenix 179,685
    Sinatra 30,561
    Rails 11,903
    https://gist.github.com/omnibs/e5e72b31e6bd25caf39a
    * Needs re-running on latest versions

    View full-size slide

  17. http://www.phoenixframework.org/blog/the-road-to-2-million-websocket-connections

    View full-size slide

  18. $ mix phoenix.new hello
    $ cd hello
    $ mix ecto.create
    $ mix phoenix.server

    View full-size slide

  19. [info] GET /
    [debug] Processing by Hello.PageController.index/2
    Parameters: %{}
    Pipelines: [:browser]
    [info] Sent 200 in 198µs

    View full-size slide

  20. Router
    Controller
    View
    Template
    Smug Mode

    View full-size slide

  21. scope "/", Hello do
    pipe_through :browser
    get "/", PageController, :index
    resources "/users", UserController
    end
    Router

    View full-size slide

  22. defmodule Hello.PageController do
    use Hello.Web, :controller
    def index(conn, _params) do
    conn
    |> assign(:name, "Andy")
    |> render("index.html")
    end
    end
    Controller

    View full-size slide

  23. View
    defmodule Hello.PageView do
    use Hello.Web, :view
    def message do
    "Hello #{@name}!"
    end
    end

    View full-size slide

  24. Template
    <%= message %>
    Brighton Ruby is Ace!

    View full-size slide

  25. Metaprogramming
    Ecto
    Doctests
    Enum
    IEX
    Comprehensions
    Hot upgrades
    T
    ail Call Optimisation
    Mix
    Hex
    Credo
    ExUnit
    [ head | tail ]
    Nodes
    Plug

    View full-size slide

  26. elixir-lang.org
    exercism.io
    elixirschool.com
    elixirsips.com
    learnelixir.tv
    learnphoenix.tv
    elixirfountain.com
    codewars.com
    redfour.io

    View full-size slide