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

Elixir, Zero to Web

Elixir, Zero to Web

An introduction to the Elixir programming language that takes us from install to a functioning web application built with the Phoenix framework.

thegrubbsian

August 13, 2014
Tweet

More Decks by thegrubbsian

Other Decks in Technology

Transcript

  1. DevMynd 1 # integer 1.0 # float :atom # atom

    {1,2,"r"} # tuple ["a",2,3] # list %{foo: "bar"} # map
  2. DevMynd "foo" <> "bar" #=> foobar true and true #=>

    true false or is_atom(:a) #=> true 1 || true #=> true nil && 13 #=> nil [1,2] ++ [3,4] #=> [1,2,3,4] [1,2,3] -- [2] #=> [1,3]
  3. DevMynd defmodule Stringer do def down(str) do String.downcase(str) end def

    split(str) do String.split(str, " ") end end ! "The Quick Fox" |> Stringer.down |> Stringer.split ! #=> ["the", "quick", "fox"]
  4. DevMynd defmodule Calc do def add(a, b \\ 1) do

    "Answer: #{a + b}" end end ! Calc.add(1,2) #=> Answer: 3
  5. DevMynd name = "JC" ! greet = fn(greeting) -> "#{greeting},

    #{name}!" end ! greet.("Hello") #=> Hello, JC!
  6. DevMynd x = 1 #=> 1 x #=> 1 1

    = x #=> 1 2 = x #=> (MatchError) no match of #=> right hand side value: 1
  7. DevMynd list = [1, 2, 3] case list do [a|b]

    -> IO.puts(a) IO.puts(b) [] -> IO.puts "empty" end #=> 1 #=> [2, 3]
  8. DevMynd defmodule NumList do def sum(list) do sum(list, 0) end

    ! defp sum([head|rest], acc) do sum(rest, head + acc) end ! defp sum([], acc) do acc end end NumList.sum(1,2,3) #=> 6
  9. DevMynd a = 1 #=> 1 a = 2 #=>

    2 ^a = 1 #=> (NoMatchError)
  10. DevMynd use Foo Execute the __USING__ function import Foo Import

    functions as local require Foo Bring in Foo’s macros alias Foo Call Foo’s functions on an alias
  11. DevMynd defmodule Foo.Bar do def baz(arg) do IO.puts arg end

    end ! defmodule Foo do import Foo.Bar def boo do baz(42) end end
  12. DevMynd defmodule Foo.Bar do def baz(arg) do end def quux(arg)

    do end end ! defmodule Foo do import Foo.Bar, only: [:baz] def boo do quux(42) #=> undefined function end end
  13. DevMynd defmodule Foo.Bar do def quux(arg) do end end !

    defmodule Foo do alias Foo.Bar, as: Baz def boo do Baz.quux(42) end end
  14. DevMynd defmodule Foo.Bar do def __USING__ do IO.puts "Using Foo.Bar"

    end end ! defmodule Foo do use Foo.Bar end #=> Using Foo.Bar
  15. DevMynd defmodule Person do defstruct name: "", age: 0 end

    ! john = %Person{name: "John", age: 42} john.name #=> John john.age #=> 42 ! %{john | age: 43}
  16. DevMynd defmodule CalcTest do use ExUnit.Case ! test "it sums"

    do assert Calc.sum([1,2,3]) == 6 end end
  17. DevMynd defmodule MyPlug do import Plug.Conn def init(options) do #

    initialize options ! options end def call(conn, _opts) do conn |> put_resp_content_type("text/plain") |> send_resp(200, "Hello world") end end ! Plug.Adapters.Cowboy.http MyPlug, []