SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 30 day free trial to unlock unlimited reading.
Pattern matching in Elixir by example - Alexander Khokhlov
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
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"]
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.
def hello(%x{} = person) when x in [Person] do
IO.puts("Hello, #{person.first_name}")
end
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.
def hello() do
result = case {:ok, "Successful!"} do
{:ok, result} -> result
{:error} -> "Shit:("
_ -> "Catch all"
end
# result == "Successful!"
end
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
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.
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.
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.
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