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

what is this elixir thing everyone is talking a...

what is this elixir thing everyone is talking about

some of my fav features of elixir

lightning talk for CodeDaze.io on September 16th, 2016

José Tomás Albornoz

September 16, 2016
Tweet

More Decks by José Tomás Albornoz

Other Decks in Programming

Transcript

  1. pattern matching iex(1)> payload = %{username: "eljojo", stuff: true} %{stuff:

    true, username: "eljojo"} iex(2)> %{username: name} = payload %{stuff: true, username: "eljojo"} iex(3)> name "eljojo"
  2. function overloading def make_sound(:cow) do IO.puts "MOOOOOOOOO" end def make_sound(:cat)

    do IO.puts "MEEEOOWW" end def make_sound(_) do IO.puts "¯\_(ϑ)_/¯" end
  3. function overloading def make_sound(%{animal: :cow}) do IO.puts "MOOOOOOOOO" end def

    make_sound(%{animal: :cat}) do IO.puts "MEEEOOWW" end def make_sound(%{animal: name}) do IO.puts "not sure how to #{name} ¯\_(ϑ)_/¯" end
  4. we also got guards def page_ops(downtime) when downtime < 10

    do IO.puts "¯\_(ϑ)_/¯" end def page_ops(downtime) when downtime >= 10 do PagerDuty.notify(whatever) end
  5. pipe operator def add_one(number), do: number + 1 def multiply_by_three(number),

    do: number * 3 multiply_by_three(add_one(1)) # => 6
  6. pipe operator def add_one(number), do: number + 1 def multiply_by_three(number),

    do: number * 3 1 |> add_one |> multiply_by_three # => 6
  7. processes + messages current_process = self() spawn_link(fn -> send(current_process, {:msg,

    "hello world"}) end) receive do {:msg, contents} -> IO.puts contents end
  8. processes + messages # machine one receive do {:msg, contents}

    -> IO.puts contents end # machine two process_name = "$MACHINE_ONE" spawn_link(fn -> send(process_name, {:msg, "yo!"}) end)
  9. validations def changeset(struct, params \\ %{}) do struct |> cast(params,

    [:email_address, :first_name]) |> validate_required([:email_address]) end def changeset_after_signup(struct, params \\ %{}) do changeset(struct, params) |> validate_required([:first_name]) end