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

Intro To Elixir

Intro To Elixir

An overview of Elixir syntax and an introduction to some of the core concepts of Elixir/Erlang

Marc Delagrammatikas

February 01, 2017
Tweet

Other Decks in Programming

Transcript

  1. Other Types list = [16, "p‐langs", :atom] keyword = [drink:

    "beer", food: "chicken", cost: 3.50] tuple = {:atom, 1, [3,2,1], "pizza"} map = %{side: "kimchi", sourness: 7} struct = %Person{ name: "Dolan", glove_size: "sm" }
  2. Binding iex(1)> a = 1 1 iex(2)> 1 = a

    1 iex(3)> a = 2 2 iex(4)> 1 = a ** (MatchError) no match of right hand side value: 2 iex(5)> ^a = 1 ** (MatchError) no match of right hand side value: 1 iex(6)> ^a = 2 2
  3. IEx Debugging defmodule Adder do def add(a, b) do c

    = a + b end end iex ‐S mix iex(1)> Adder.add 1, 2 3
  4. Puts Debugging defmodule Adder do def add(a, b) do c

    = a + b |> IO.puts end end iex(1)> IO.puts "Hi" Hi iex(2)> IO.puts [1,2,3] ^A^B^C iex(3)> IO.inspect [1,2,3] [1,2,3]
  5. Pry Debugging defmodule Adder do def add(a, b) do c

    = a + b require IEx; IEx.pry end end
  6. Immutable‑ish Data defmodule Person do defstruct name: nil, glove_size: nil

    end dolan = %Person{ name: "Dolan", glove_size: "sm" } dolan.glove_size = "lg" ** (CompileError) iex:3: cannot invoke remote function dolan.glove_size/0 inside match dolan_clone = %{dolan | glove_size: "lg"}
  7. Add / Remove From Lists iex(1)> list = [2,3,4] [2,

    3, 4] iex(2)> bigger = [1 | list] [1, 2, 3, 4] iex(4)> [first | smaller] = bigger [1, 2, 3, 4] iex(5)> first 1 iex(6)> smaller [2, 3, 4]
  8. Functions def function_name(argument) do IO.puts argument end function_name(1) def function_name(argument),

    do: IO.puts argument function_name = fn(argument) ‐> IO.puts argument end function_name = &(IO.puts &1)
  9. Pattern Matching {a, b, c} = {:hello, "world", 42} {a,

    b, c} = {:hello, "world"} ** (MatchError) no match of right hand side value: {:hello, "world"} def handle_result({:ok, result}), do: IO.puts result def handle_result({:error}), do: IO.puts "Error!"
  10. Guard Clauses def function_name(arg) when is_integer(arg), do: arg def function_name(arg)

    when is_float(arg), do: round(arg) def handle_result({:ok, result}) when is_string(result) do IO.puts result end def handle_result({:ok, result}) do IO.inspect result end
  11. Advanced Topics BEAM: the virtual machine that Erlang and Elixir

    run on. OTP (Open Telecom Platform): A framework and set of patterns. Processes: lightweight, isolated, concurrent, and distributed. Applications: reusable component, comprised of a set of modules. Supervisor: restarts processes when they crash or fail.
  12. Advanced Topics GenServer: a server abstraction for communication between processes.

    Agent: an abstraction around GenServer with a more usable API. Task: a main form of concurrency in Elixir. Let It Crash: "Rather than write defensive code to handle errors, Erlang/Elixir developers create supervised processes and handle their exit case."
  13. Advanced Topics Typespecs: allow you to specify types explicitly for

    documentation and analysis (not used by the compiler). Behaviors: a way of defining an interface which the implementing module must export (e.g. GenServer, Supervisor).