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

Elixir – Belarus Ruby User Group 25 Jan 2014

Elixir – Belarus Ruby User Group 25 Jan 2014

Sergey Nartimov

January 25, 2014
Tweet

More Decks by Sergey Nartimov

Other Decks in Programming

Transcript

  1. Metaprogramming defmacro unless(clause, options) do do_clause = Keyword.get(options, :do, nil)

    else_clause = Keyword.get(options, :else, nil) quote do if(unquote(clause), do: unquote(else_clause), else: unquote(do_clause)) end end
  2. Metaprogramming Lisps traditionally empowered developers because you can eliminate anything

    that’s tedious through macros, and that power is really what people keep going back for. — Rich Hickey
  3. Function capture iex> list = ["foo", "bar", "baz"] ["foo","bar","baz"] iex>

    Enum.map list, fn(x) -> size(x) end [3,3,3] iex> Enum.map list, &size/1 [3,3,3] iex> fun = &rem(&1, 2) #Function<6.80484245 in :erl_eval.expr/5> iex> fun.(4) 0
  4. Function capture iex> list = ["foo", "bar", "baz"] ["foo","bar","baz"] iex>

    Enum.map list, fn(x) -> size(x) end [3,3,3] iex> Enum.map list, &size/1 [3,3,3] iex> fun = &rem(&1, 2) #Function<6.80484245 in :erl_eval.expr/5> iex> fun.(4) 0
  5. Partial application iex> list = ["foo", "bar", "baz"] ["foo","bar","baz"] iex>

    Enum.map list, fn(x) -> size(x) end [3,3,3] iex> Enum.map list, &size/1 [3,3,3] iex> fun = &rem(&1, 2) #Function<6.80484245 in :erl_eval.expr/5> iex> fun.(4) 0
  6. list |> Enum.filter(&(&1 > 0)) # take positive numbers |>

    Enum.map(&(&1 * &1)) # square each one |> Enum.reduce(0, &(&1 + &2)) # calculate sum
  7. Protocols defprotocol Inspect do def inspect(thing, opts) end defimpl Inspect,

    for: Atom do def inspect(false), do: "false" def inspect(true), do: "true" def inspect(nil), do: "nil" def inspect(atom) do # ... end end
  8. ExUnit ExUnit.start defmodule MyTest do use ExUnit.Case test "the truth"

    do assert true end end $ elixir assertion_test.exs
  9. ExUnit ExUnit.start defmodule MyTest do use ExUnit.Case test "the truth"

    do assert true end end $ elixir assertion_test.exs
  10. ExUnit assert 1 + 1 == 2 refute 1 +

    3 == 3 assert 1 + 1 == 3 # Expected 2 to be equal to (==) 3
  11. Mix defmodule MyProject.Mixfile do use Mix.Project def project do [

    app: :my_project, version: "0.0.1", deps: deps ] end # Configuration for the OTP application def application do [] end defp deps do [] end end
  12. Mix defp deps do [ { :some_project, github: "some_project/other", tag:

    "0.3.0" }, { :another_project, ">= 1.0.2", git: "https://example.com/another/repo.git" } ] end