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

An intro to Elixir

Tommy Palmer
November 09, 2018

An intro to Elixir

At a Deliveroo internal conference I gave a quick intro to Elixir.

Tommy Palmer

November 09, 2018
Tweet

More Decks by Tommy Palmer

Other Decks in Programming

Transcript

  1. This talk - About Me - wtf is Elixir -

    Basics - Cool in Elixir - Phoenix
  2. I’m Tommy - ~3 years @ Deliveroo - Ruby for

    ~10 years - Elixir for ~2 years
  3. I’m Tommy - ~3 years @ Deliveroo - Ruby for

    ~10 years - Elixir for ~2 years - I know that I look like Tormund Giantsbane
  4. wtf is Elixir? Elixir is a dynamic, functional language designed

    for building scalable and maintainable applications.
  5. wtf is Elixir? - Created in 2012 by José Valim

    (former Rails Core Team) - Built on top of, and compiles to, Erlang
  6. wtf is Elixir? - Created in 2012 by José Valim

    (former Rails Core Team) - Built on top of, and compiles to, Erlang - Functional
  7. wtf is Elixir? - Created in 2012 by José Valim

    (former Rails Core Team) - Built on top of, and compiles to, Erlang - Functional - Built with distributed resiliency in mind
  8. Built on top of Erlang - Made by Ericsson in

    1986 - Erlang runs: - WhatsApp
  9. Built on top of Erlang - Made by Ericsson in

    1986 - Erlang runs: - WhatsApp - Facebook Messanger
  10. Built on top of Erlang - Made by Ericsson in

    1986 - Erlang runs: - WhatsApp - Facebook Messanger - SMS
  11. Built on top of Erlang - Made by Ericsson in

    1986 - Erlang runs: - WhatsApp - Facebook Messanger - SMS - GPRS and 3G networks
  12. Built on top of Erlang - Made by Ericsson in

    1986 - Erlang runs: - WhatsApp - Facebook Messanger - SMS - GPRS and 3G networks - Lots of Online Gaming
  13. * OTP means Open Telephony Platform * Erlang/OTP is run

    by Ericsson * the syntax is prolog-y & not c-y or ruby-y or python-y, its just too damn erlang-y * no package manager * no list of open source packages * no community site * the mailing list is the community * everyone knows each other from Stockholm * it is too hard to get a first working app * lists for strings, aargh! * no for loops, eek! * variables don't vary, whimper! * people don't learn OTP first but start with Erlang * people don't learn Erlang first but start with OTP * no docs * too many docs but it is the wrong sort * not enough teaching materials * doesn't run on the JVM * we used to think that it was because there were no books... * Erlang Solutions isn't a proper internet company * the language develops too slowly * emacs is the IDE
  14. Distributed resiliency - Elixir is a multi-core language - State

    is held in lightweight Elixir processes - Processes can start, run, crash and restart easily
  15. Equals is pattern match > [2, 4, val] = [1,

    2, 3] ** (MatchError) no match of right hand side value: [1, 2, 3]
  16. Pattern matching a Tuple case ApiFetcher.fetch(url) do {:ok, body} ->

    # Do something with `body` {:error, error} -> # Do something with `error` end
  17. defmodule Text do def to_upcase(arg) do String.upcase(arg) end end >

    Text.to_upcase("hello") "HELLO" Defining functions
  18. defmodule Formatter do def format("up:" <> username) do String.upcase(username) end

    def format(“reverse:" <> title) do String.reverse(title) end end Defining multiple versions of the same function
  19. defmodule Numbers do def double(arr) do Enum.map(arr, fn(x) -> x

    * 2 end) end end > Numbers.double([1, 2, 3]) [2, 4, 6] Loops
  20. defmodule Formatter do def format(data) do Map.get(data, :warning) |> String.upcase

    |> String.reverse end end > Formatter.format(%{warning: "murder"}) "REDRUM" Pipes
  21. defmodule Formatter do def format(data) do data |> Map.get(:warning) |>

    String.upcase |> String.reverse end end Pipes
  22. def publish(%Post{status: status}) when status == :draft do # Publish

    a post end > Publisher.publish(%Post{status: :published}) ** (FunctionClauseError) no function clause matching in Publisher.publish/1 The following arguments were given to Publisher.publish/1: # 1 %Post{status: :published} Guards
  23. defmodule Basket do use GenServer def init(items) do {:ok, item}

    end def handle_call(:pop, _from, [head | tail]) do {:reply, head, tail} end def handle_cast({:push, item}, state) do {:noreply, [item | state]} end end GenServer
  24. > {:ok, pid} = GenServer.start_link(Basket, [:burger]) {:ok, #PID<0.354.0>} > GenServer.call(pid,

    :pop) :burger > GenServer.cast(pid, {:push, :fries}) :ok > GenServer.call(pid, :pop) :fries GenServer
  25. Phoenix - A Rails like web framework for Elixir -

    Controllers - Views (like Presenters) - Templates with Embedded Elixir (.eex) - Ecto (for DB interaction, like Active Record) - WebSockets - Contexts
  26. defmodule ChatWeb.ChatChannel do use Phoenix.Channel def join("chat:lobby", _message, socket) do

    {:ok, socket} end def join("chat:" <> room_id, _params, socket) do socket = assign(socket, :room_id, room_id) {:ok, socket} end def handle_in("join_room", %{"username" => username}, socket) do body = %{ new_user: username } broadcast!(socket, “new_user”, %{body: body}) {:noreply, socket} end end Channels
  27. import {Socket} from "phoenix" let socket = new Socket("ws://localhost:4000/socket") socket.connect()

    let channel = socket.channel("room:lobby", {}) channel.join() channel.on("new_user", (body) => { console.log("New user!", body.user) }) Channels