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

An Introduction to Elixir

An Introduction to Elixir

Covers the basics of the language, what it can be used for and why it's particularly helpful for certain situations. A great talk for people new to or curious about Elixir.

Links:

* Mike Zornek: For Hire | http://mikezornek.com/for-hire/
* Elixir Language Website Guide | https://elixir-lang.org/getting-started/introduction.html
* Elixir School | https://elixirschool.com/en/
* Elixir in Action (Book) | https://www.manning.com/books/elixir-in-action
* The Pragmatic Studio (Videos) | https://pragmaticstudio.com/elixir
* ElixirConf | https://www.youtube.com/channel/UC0l2QTnO1P2iph-86HHilMQ/videos

Mike Zornek

April 14, 2020
Tweet

More Decks by Mike Zornek

Other Decks in Programming

Transcript

  1. An Introduction to Elixir
    April 2020 • Mike Zornek

    View full-size slide

  2. http://mikezornek.com

    View full-size slide

  3. Elixir is a dynamic, functional language designed for
    building scalable and maintainable applications. Elixir
    leverages the Erlang VM, often called the BEAM,
    which is known for running low-latency, distributed
    and fault-tolerant systems.

    View full-size slide

  4. Elixir is a dynamic, functional language designed for
    building scalable and maintainable applications. Elixir
    leverages the Erlang VM, often called the BEAM,
    which is known for running low-latency, distributed
    and fault-tolerant systems.

    View full-size slide

  5. * Code Expectations

    View full-size slide

  6. Functional Language

    View full-size slide

  7. defmodule Greeter do
    def hello(name) do
    "Hello, " <> name
    end
    end
    iex> Greeter.hello("Sean")
    "Hello, Sean"

    View full-size slide

  8. # Other Languages
    baz(new_function(other_function())
    # Elixir’s Pipe Operator
    other_function() |> new_function() |> baz()

    View full-size slide

  9. iex> String.split("Elixir rocks")
    ["Elixir", "rocks"]
    iex> "Elixir rocks" |> String.split()
    ["Elixir", "rocks"]

    View full-size slide

  10. iex> String.split("bread;milk;eggs", ";")
    ["bread", "milk", "eggs"]
    iex> "bread;milk;eggs"
    |> String.upcase()
    |> String.split(";")
    ["BREAD", "MILK", "EGGS"]

    View full-size slide

  11. No Strict Typing

    View full-size slide

  12. defmodule Greeter do
    def hello(name) do
    "Hello, " <> name
    end
    end
    iex(3)> Greeter.hello(6)
    ** (ArgumentError) argument error
    :erlang.byte_size(6)
    iex:3: Greeter.hello/1
    iex(3)> Greeter.hello(true)
    ** (ArgumentError) argument error
    :erlang.byte_size(true)
    iex:3: Greeter.hello/1

    View full-size slide

  13. defmodule Greeter do
    @spec hello(String.t()) :: String.t()
    def hello(name) do
    "Hello, " <> name
    end
    end
    # ** (CompileError) greeter.ex:8: undefined function hello/1
    hello(6)

    View full-size slide

  14. Pattern Matching

    View full-size slide

  15. # Algebra
    10 = x * 2

    View full-size slide

  16. iex(5)> x = 1
    1

    View full-size slide

  17. iex(5)> x = 1
    1
    iex(6)> 1 = x
    1

    View full-size slide

  18. iex(5)> x = 1
    1
    iex(6)> 1 = x
    1
    iex(7)> 2 = x
    ** (MatchError) no match of right hand side value: 1
    iex(7)>

    View full-size slide

  19. iex(7)> 3 = y
    ** (CompileError) iex:7: undefined function y/0

    View full-size slide

  20. # Tuples
    iex> {:ok, value} = {:ok, "Successful!"}
    {:ok, "Successful!"}
    iex> value
    "Successful!"
    iex> {:ok, value} = {:error}
    ** (MatchError) no match of right hand side value:
    {:error}

    View full-size slide

  21. Pattern Matching

    View full-size slide

  22. Metaprogramming

    View full-size slide

  23. defmodule Friends.Person do
    use Ecto.Schema
    schema "people" do
    field :first_name, :string
    field :last_name, :string
    field :age, :integer
    end
    end

    View full-size slide

  24. Process
    Mailbox
    State

    View full-size slide

  25. Process A
    Mailbox
    State
    Process B
    Mailbox
    State
    Messages

    View full-size slide

  26. defmodule Example do
    def listen do
    receive do
    {:ok, "coffee"} -> IO.puts("Coffee time!")
    {:ok, "tea"} -> IO.puts("Tea, Earl Gray, Hot.")
    end
    listen()
    end
    end

    View full-size slide

  27. iex> pid = spawn(Example, :listen, [])
    #PID<0.108.0>
    iex> send(pid, {:ok, "coffee"})
    Coffee time!
    {:ok, "coffee"}
    iex> send(pid, :ok)
    :ok

    View full-size slide

  28. CPU CPU CPU CPU
    BEAM
    OS Thread OS Thread
    OS Thread
    OS Thread
    Process
    Process
    Process
    scheduler
    Process
    Process
    Process
    Process
    Process
    Process
    Process
    Process
    Process
    scheduler scheduler scheduler
    OS process

    View full-size slide

  29. BEAM
    Server
    CPU CPU CPU CPU
    CPU CPU CPU CPU

    View full-size slide

  30. BEAM
    Server
    CPU CPU CPU CPU
    CPU CPU CPU CPU
    Server
    CPU CPU CPU CPU
    CPU CPU CPU CPU
    Server
    CPU CPU CPU CPU
    CPU CPU CPU CPU
    Server
    CPU CPU CPU CPU
    CPU CPU CPU CPU

    View full-size slide

  31. Fault-Tolerant

    View full-size slide

  32. Process
    Supervisor

    View full-size slide

  33. Process
    Supervisor
    Process
    Process

    View full-size slide

  34. Supervisor
    Process
    Process
    Process
    Supervisor
    Process
    Process
    Process
    Supervisor
    Process
    Process
    Process
    Supervisor
    Supervisor

    View full-size slide

  35. Maintainable

    View full-size slide

  36. $ iex -S mix
    iex(1)> :observer.start()

    View full-size slide

  37. Notable Projects
    • Building web applications using Phoenix.

    • Working with databases using Ecto.

    • Assemble data processing pipelines with Broadway.

    • Crafting GraphQL APIs using Absinthe.

    • Deploying embedded software using Nerves.

    View full-size slide

  38. • Elixir Language Website Guide 

    https://elixir-lang.org/getting-started/introduction.html

    • Elixir School

    https://elixirschool.com/en/

    • Elixir in Action (Book)

    https://www.manning.com/books/elixir-in-action

    • The Pragmatic Studio (Videos)

    https://pragmaticstudio.com/elixir

    • ElixirConf

    https://www.youtube.com/channel/UC0l2QTnO1P2iph-86HHilMQ/videos

    View full-size slide

  39. Thanks!
    Available For Hire:
    http://mikezornek.com/for-hire/
    Contact:
    @zorn on Micro.Blog + Twitter

    View full-size slide