Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Pattern matching in Elixir by example - Alexander Khokhlov

65 views

Published on

Elixir Club 9
November 18, 2017
Kharkiv

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Pattern matching in Elixir by example - Alexander Khokhlov

  1. 1. Александр Хохлов @apoint Pattern matching 
 in Elixir by example
  2. 2. Founder at Nots http://nots.io
  3. 3. TL;DR
  4. 4. https://images.techhive.com/images/article/2016/06/integration-projects-disasters-9-100669080-gallery.idge.jpg
  5. 5. https://twitter.com/bloerwald/status/448415935926255618
  6. 6. История
  7. 7. Erlang The Movie II: The Sequel https://www.youtube.com/watch?v=rRbY3TMUcgQ https://img00.deviantart.net/de6b/i/2011/044/b/9/erlang_the_movie_by_mcandre-d39hupa.png
  8. 8. Нет присваивания (assignment)
  9. 9. iex > a = 1 1 iex > 1 = a 1 iex > 2 = a ** (MatchError) no match of right hand side value: 1
  10. 10. Переменные можно связывать заново (rebind)
  11. 11. iex > b = 2 2 iex > b = 3 3 iex > ^b = 4 ** (MatchError) no match of right hand side value: 4 iex > ^b = 3 3
  12. 12. Можно сопоставлять структуры данных
  13. 13. iex > {:ok, value} = {:ok, "Successful!"} {:ok, "Successful!"} iex > value “Successful!” iex > {:ok, value} = {:error, "Shit:("} ** (MatchError) no match of right hand side value: {:error, “Shit:(“} iex > {:ok, value} = [:ok, "Success"] ** (MatchError) no match of right hand side value: [:ok, "Success"]
  14. 14. iex > %{key: value} = %{key: “hash value"} %{key: “hash value"} iex > value “hash value” iex > %{key1: value} = %{key1: "value1", key2: "value2"} %{key1: "value1", key2: "value2"} iex > value “value1" iex > key = :key1 :key1 iex > %{^key => value} = %{key1: "value1", key2: "value2"} %{key1: "value1", key2: "value2"} iex > value "value1"
  15. 15. А еще списки
  16. 16. iex > list = [1, 2, 3] [1, 2, 3] iex > [1 | tail] = list [1, 2, 3] iex > tail [2, 3] iex > [2 | _] = list ** (MatchError) no match of right hand side value: [1, 2, 3] iex > [head | tail] = list [1, 2, 3] iex > head 1
  17. 17. Функции
  18. 18. iex > defmodule Hello do ... > def hello(name) do ... > "Hello, #{name}" ... > end ... > end … skipped … iex > Hello.hello("point") "Hello, point"
  19. 19. iex > defmodule Hello do ... > def hello(:point) do ... > "Greeting, my lord" ... > end ... > def hello(name) do ... > "Hello, #{name}" ... > end ... > end iex > Hello.hello(:point) "Greeting, my lord" iex > Hello.hello("John") "Hello, John"
  20. 20. def hello(:point) def hello("Alex" <> _) def hello([name1, name2 | _]) def hello(%{first_name: name}) def hello(_) def hello(_name)
  21. 21. Хардкор
  22. 22. iex > defmodule Person do ... > defstruct first_name: "", last_name: "" ... > end iex > def hello(%Person{} = person) do ... > IO.puts("Hello, #{person.first_name}") ... > end iex > Hello.hello(%Person{first_name: "Arthur", last_name: "Dent"}) Hello, Arthur def hello(%Person{first_name: first_name})
  23. 23. def hello(%x{} = person) when x in [Person] do IO.puts("Hello, #{person.first_name}") end
  24. 24. defmodule Person do defstruct age: 0 end defmodule Greeting do def hello(%{age: age}) when 6 < age and age < 12, do: "Hiya" def hello(%{age: age}) when age in 12..18, do: "Whatever" def hello(%{age: age}) when 60 < age, do: “You kids get off my lawn" def hello(_), do: "Hello" end https://hexdocs.pm/elixir/master/guards.html
  25. 25. def hello() do result = case {:ok, "Successful!"} do {:ok, result} -> result {:error} -> "Shit:(" _ -> "Catch all" end # result == "Successful!" end
  26. 26. defmodule Factorial do def of(0), do: 1 def of(n) when n > 0 do n * of(n - 1) end end iex > Factorial.of(10) 3628800
  27. 27. defmodule ImageTyper do @png_signature <<137::size(8), 80::size(8), 78::size(8), 71::size(8), 13::size(8), 10::size(8), 26::size(8), 10::size(8)>> @jpg_signature <<255::size(8), 216::size(8)>> def type(<<@png_signature, rest::binary>>), do: :png def type(<<@jpg_signature, rest::binary>>), do: :jpg def type(_), do :unknown end
  28. 28. def create(params) do case validate_name(params["name"]) do {:ok, name} -> case validate_email(params["email"]) do {:ok, email} -> create_db_record(name, email) {:error, message} -> conn |> put_flash(:error, "Wrong email: #{message}") |> redirect(to: "/") end {:error, message} -> conn |> put_flash(:error, "Wrong name: #{message}") |> redirect(to: "/") end end
  29. 29. def create(params) do with {:ok, name} <- validate_name(params["name"]), {:ok, email} <- validate_email(params["email"]) do create_db_record(name, email) else {:name_error, message} -> conn |> put_flash(:error, "Wrong name: #{message}") |> redirect(to: "/") {:email_error, message} -> conn |> put_flash(:error, "Wrong email: #{message}") | > redirect(to: "/") end end
  30. 30. defmodule MyAppWeb.PageController do action_fallback MyAppWeb.FallbackController def show(params) do with {:ok, username} <- get_username(params), {:ok, cms_page} <- CMS.get_page(username, params) do render(conn, "show.html", page: page) end end end defmodule MyAppWeb.FallbackController do def call(conn, {:username_error, message}) do conn |> put_flash(:error, "Wrong usernname: #{message}") |> redirect(to: "/") end def call(conn, {:cms_page_not_found, message}) do conn |> put_flash(:error, "Page not found: #{message}") |> redirect(to: "/") end end
  31. 31. defmodule NotsappWeb.ProjectsFallbackController do use Phoenix.Controller def call(conn, _) do conn |> put_status(:not_found) |> put_layout(false) |> render(NotsappWeb.ErrorView, :”501") end end
  32. 32. @apoint point@nots.io http://nots.io/jobs @nots_io facebook.com/nots.io

×