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

Say Hello to Elixir

Say Hello to Elixir

Presented at The Guild on Feb 27th, 2014, this is a look at Elixir, how it works, and what we can learn from its concepts and philosophy in our everyday object-oriented programming.

Arjan van der Gaag

February 27, 2014
Tweet

More Decks by Arjan van der Gaag

Other Decks in Programming

Transcript

  1. Arjan van der Gaag
    @avdgaag • arjanvandergaag.nl

    View Slide

  2. View Slide

  3. – erlang.org
    Erlang is a programming language used to build massively scalable
    soft real-time systems with requirements on high availability.
    Erlang's runtime system has built-in support for concurrency,
    distribution and fault tolerance.

    View Slide

  4. Anonymous functions
    iex> Enum.map [1, 2, 3], fn n -> n * 2 end
    [2, 4, 6]
    iex> Enum.map [1, 2, 3], &(&1 * 2)
    [2, 4, 6]

    View Slide

  5. Named functions & modules
    defmodule Greeter do
    def greet(name) do
    IO.puts "Hello, #{name}"
    end
    end
    iex> Greeter.greet("James")
    "Hello, James"
    iex> Enum.each ["john", “paul”], &Greeter.greet/1
    "Hello, john"
    "Hello, paul"

    View Slide

  6. defmodule MyMap do
    !
    !
    !
    !
    !
    end
    Pattern matching
    !
    def map([], _), do: []
    !
    !
    !
    def map([head | tail], fun) do
    [fun.(head) | map(tail, fun)]
    end

    View Slide

  7. Pipelines
    iex> String.last(String.upcase("foo"))
    "O"
    iex> "foo"
    ...> |> String.upcase
    ...> |> String.last
    "O"

    View Slide

  8. Assert
    defmodule TruthTest do
    use ExUnit.Case
    test "considers two strings equal" do
    assert "foo" == "bar"
    end
    end
    !
    !
    !
    !
    !
    !
    !
    !
    1) test considers two strings equal (TruthTest)
    ** (ExUnit.ExpectationError)
    expected: "foo"
    to be equal to (==): "bar"
    at test/demo/truth_test.exs:5

    View Slide

  9. Macros for

    Metaprogramming

    Mayhem

    View Slide

  10. Actors
    fib = spawn(Fibonacci, :server, [self])
    fib <- { :number, 17 }
    !
    !
    !
    receive do
    !
    !
    !
    !
    !
    !
    end
    !
    !
    !
    !
    { :ok, result } ->
    IO.puts "Received response:"
    IO.puts result
    !
    !
    !
    !
    !
    !
    !
    { _, error } ->
    IO.puts “An error occured!”
    IO.puts error

    View Slide

  11. encapsulated

    state
    message message

    View Slide

  12. View Slide

  13. Re-learn programming

    View Slide

  14. Practice makes perfect

    View Slide

  15. Pure functions and immutable
    data are easy to test.

    View Slide

  16. FP OOP

    View Slide

  17. ♥ docs

    View Slide

  18. Macros allow awesome
    metaprogramming

    View Slide

  19. Find out more
    • elixir-lang.org
    • erlang.org
    • Programming Elixir (Dave Thomas)
    • Meet Elixir (Peepcode play-by-play with José Valim)
    • exercism.io
    • Gary Bernhardt: Boundaries

    View Slide