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

Elixir

 Elixir

Looking at the programming language Elixir

Joseph Wilk

August 20, 2013
Tweet

More Decks by Joseph Wilk

Other Decks in Technology

Transcript

  1. Install $ brew install elixir iex, mix, elixir $ wget

    http://bit.ly/1aN4Ftt Erlang, version R16B https://www.erlang-solutions.com/downloads/download-erlang-otp $ brew tap homebrew/versions $ brew link erlang-r16 Elixir
  2. $ brew install elixir iex, mix, elixir $ wget http://bit.ly/1aN4Ftt

    Erlang, version R16B https://www.erlang-solutions.com/downloads/download-erlang-otp Elixir $ brew tap homebrew/versions $ brew link erlang-r16 Install iex, mix, elixir
  3. defmodule A do def ping do "pong" end IO.puts "ping"

    end defmodule B do def ping do A.ping end end defmodule C do def ping do B.ping end end
  4. { x, y, _ } = { 10, 2, 3

    } IO.inspect x #=> 10 IO.inspect y #=> 2 Pattern Matching
  5. if is_list(thing) && !Enum.empty?(thing) do IO.puts "matches" else IO.puts "no

    match" end case thing do [_] -> IO.puts "matches" _ -> IO.puts "no match" end Pattern Matching
  6. Pattern Matching defmodule Eating do def meeting([head| tail]) do [rules(head)

    | meeting(tail)] end def meeting([]) do [] end def rules({ :lion, :cat }), do: "lion eats cat" def rules({ :lion, :shark }), do: "shark eats lion" def rules({ :rabbit, other }), do: "rabbit eats #{other}" def rules({ a, b }), do: "#{a} eats #{b}" def rules({ a }), do: "#{a} eats itself" def rules(_), do: "nothing gets eaten" end Eating.meeting([{:lion, :cat}, {:lion, :shark}])
  7. Protocols defprotocol Enumerable do def reduce(item) #... end end defimpl

    Enumerable, for: Set do def reduce(set, acc, fun), do: Set.reduce(set, acc, fun) end Enum.reduce HashSet.new, [], fn element, accum -> List.concat(element, accum) end
  8. Records compile time pattern defrecord ExUnit.Test, [:name, :case, :failure, :time]

    do @moduledoc """ A record that keeps information about the test. It is received by formatters and also accessible in the metadata under the key `:test`. """ end def handle_cast({:test_finished, ExUnit.Test[failure: nil] = test}, config) do if config.trace do IO.puts success(trace_test_result(test)) else IO.write success(".") end { :noreply, config.update_tests_counter(&1 + 1) } end
  9. { _, _, _ } defmacro hello do end Macro

    tuples { _, _, _ } tuples
  10. Amrita https://github.com/josephwilk/amrita A polite, well mannered and thoroughly upstanding testing

    framework for Elixir fact "about factorial" do factorial(0) |> ! 0 factorial(0) |> 1 list_of_factorials = Enum.map 0..3, fn n -> factorial(n) end list_of_factorials |> contains 1 list_of_factorials |> !contains 2 list_of_factorials |> [ _, 1, _, 6 ] end
  11. fact "mock with a _ wildcard" do provided [MocksTest.Funk.hip?(_) |>

    false] Funk.hip?(:yes) |> falsey Funk.hip?(:whatever) |> falsey end end
  12. fact "mock with a _ wildcard" do provided [MocksTest.Funk.hip?(_) |>

    false] Funk.hip?(:yes) |> falsey Funk.hip?(:whatever) |> falsey end end |>(MocksTest.Funk.hip?(_), false)
  13. fact "mock with a _ wildcard" do provided [MocksTest.Funk.hip?(_) |>

    false] Funk.hip?(:yes) |> falsey Funk.hip?(:whatever) |> falsey end end [{ :|>, [line: 2], [ , false] }] { {:., [line: 2], } , [line: 2], [:_] } [ { :__aliases__, [line: 2], [:MocksTest, :Funk] }, :hip? ] |>(MocksTest.Funk.hip?(_), false)
  14. defmodule Provided.Parse do defexception Error, form: [] do def message(exception)

    do "Amrita could not understand your `provided`:\n" <> " " <> Macro.to_string(exception.form) <> "\n" <> " Make sure it uses this format: [Module.fun |> :return_value]" end end def prerequisites(forms) do prerequisites = Enum.map(forms, fn form -> extract(form) end) Provided.Prerequisites.new(prerequisites) end defp extract({:|>, _, [{fun, _, args}, value]}) do { module_name, function_name } = extract(fun) { module_name, function_name, args, value } end defp extract({:., _, [ns, method_name]}) do { extract(ns), method_name } end defp extract({:__aliases__, _, ns}) do Module.concat ns end defp extract(form) do raise Error.new(form: form) end end
  15. defmodule Provided.Parse do defexception Error, form: [] do def message(exception)

    do "Amrita could not understand your `provided`:\n" <> " " <> Macro.to_string(exception.form) <> "\n" <> " Make sure it uses this format: [Module.fun |> :return_value]" end end def prerequisites(forms) do prerequisites = Enum.map(forms, fn form -> extract(form) end) Provided.Prerequisites.new(prerequisites) end defp extract({:|>, _, [{fun, _, args}, value]}) do { module_name, function_name } = extract(fun) { module_name, function_name, args, value } end defp extract({:., _, [ns, method_name]}) do { extract(ns), method_name } end defp extract({:__aliases__, _, ns}) do Module.concat ns end defp extract(form) do raise Error.new(form: form) end end
  16. Immutability “Elixir assignments are compiled down to Static Single Assignment.”

    defmodule Immutable do def mutation do x = 1 # => v0 = 1 x = 2 # => v1 = 2 x = 3 # => v2 = 3 end end