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

Mix it Up With Elixir

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Mix it Up With Elixir

Avatar for Andrew Bredow

Andrew Bredow

March 21, 2015
Tweet

Other Decks in Technology

Transcript

  1. Goals • Understand Elixir's history • Explore Elixir's syntax and

    conventions • Walk through a real world app • Learn more about the ecosystem • Get excited!
  2. Elixir - History • Version 1.0 - Fall, 2014 •

    First appeared in early 2012 • Started development in 2011 • Built on top of Erlang VM • Traces back to 1986, open sourced in 1998
  3. Why Erlang? If somebody came to me and wanted to

    pay me a lot of money to build a large scale message handling system that really had to be up all the time, could never afford to go down for years at a time, I would unhesitatingly choose Erlang to build it in. - Tim Bray
  4. Why Elixir? • All features of Erlang • Scalable •

    Fault Tolerant • Functional • Immutable • OTP • Fun!
  5. Voting App • 1 vote per entry • Vote for

    unlimited entries • Vote must be cast from Grand Rapids • Users can see the current winner
  6. Building Blocks integer 42, 1_000_000, 0b1010, 0xcafe, 0o765 float 1.0,

    0.125, .314159e1, 314159e-5 range 1..10, 5..-25, 42.906554..43.01148 boolean true, false atom :success, :is_string?, :"grand rapids" string "test", "hellõ" pid #PID<0.64.0> port #Port<0.3766> Basic/System Types
  7. Building Blocks list ["A", "B", "C"], [false, 56, :boo] tuple

    {:ok, 42}, {"a", "b", "c"} keyword list [name: "Andrew", destination: "LAX"] map %{"MI" => "Michigan", "CA" => "California"} binary << 1, 2 >> Collection Types
  8. Start A Project • Mix • 1 Project Format to

    Rule Them All • Walk through the project to see what we have
  9. Building Blocks Pattern Matching x = 5
 person = %{


    name: "Andrew",
 occupation: %{ field: "Computer", title: "Software Developer"}
 }
 %{name: my_name, occupation: %{field: _, title: my_title}} = person 
 my_name # "Andrew" 
 my_title # "Software Developer"
  10. Building Blocks Functions add = fn a, b -> a

    + b end
 defmodule Greeter
 def say_hello(name \\ "Friend") do
 "Hello, #{name}"
 end
 end 
 add = &(&1 + &2) 
 puts = &IO.puts/1
  11. Guard Clauses defmodule Greeter
 def say_hello(name) when is_string(name) do
 "Hello,

    #{name}"
 end
 
 def say_hello(name) when is_integer(name) do
 42 + name end
 end Pattern Matching On Steroids
  12. Program Flow IO.puts(multiply(add(round_down(12), 45), 10)) 
 first_value = round_down(12)
 second_value

    = add(first_value, 45)
 third_value = multiply(second_value, 10)
 IO.puts(third_value) 
 12
 |> round_down
 |> add(45)
 |> multiply(10)
 |> IO.puts
  13. Tail Recursion [head|tail] = ["A", "B", "C", "D"]
 #head: "A",

    tail: ["B", "C", "D"] 
 ["A"|["B","C","D"]]
 #["A", "B", "C", "D"] 
 def count_list([head|tail]) do
 1 + count_list(tail)
 end
 def count_list([]), do: 0 
 def downcase_list([head|tail]) do
 [String.downcase(head)|downcase_list(tail)]
 end
 def downcase_list([]), do: []

  14. Major Leagues • Distribution • OTP / Genserver • Supervision

    trees • Hot deploys • Elixir allows you to harness years of advancements
  15. Structs defmodule User do
 defstruct id: nil,
 name: nil
 


    def valid?(user) do
 String.length(user.name) > 0
 end
 end 
 me = %User{id: 1, name: "Andrew Bredouw"} 
 me = %{ me | name: "Andrew Bredow" }
  16. Types defmodule User do
 defstruct id: nil,
 name: nil
 @type

    t :: %User{id: integer, name: String.t}
 
 def valid?(user)
 String.length(user.name) > 0
 end
 end 
 me = %User{user_id: 1, name: "Andrew Bredouw"} 
 me = %{ me | name: "Andrew Bredow" }
  17. Protocols defprotocol URI do
 def to_query(params)
 end 
 URI.to_query(%{bar: "foo",

    event: "baz"}) # ** (UndefinedFunctionError) undefined function: URI.to_query/1 Achieving polymorphism
  18. Protocols defimpl URI, for: Map do
 def to_query(params) do
 Enum.map(params,

    fn {first, second} ->
 "#{first}=#{second}" end)
 |> Enum.join("&")
 end
 end 
 URI.to_query(%{bar: "foo", event: "baz"}) # "bar=foo&event=baz" Achieving Polymorphism