Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Guards and Pattern Matching

guards_and_pattern_matching.livemd

Guards and Pattern Matching

Polymorphism

map = %{key1: "value", key2: "value"}
list = [1, 2, 3]

Enum.reduce(map, 0, fn each, acc ->
  IO.inspect(each)
end)
Enum.reduce("hello", 0, fn each, acc ->
  IO.inspect(each)
end)
# double integer -> integer
# Integer.to_string integer -> string
defmodule Greeter do
  def hello([name1, name2]) do
    "Hello #{name1} and #{name2}"
  end

  def hello([name1]) do
    "Hello #{name1}"
  end

  def hello(_) do
    "Hello everyone!"
  end
end
defmodule FireWaterGrass do
  defstruct [:name]
  def play(:fire, :water), do: "water beats fire"
  def play(:fire, :grass), do: "fire beats grass"
  def play(guess1, guess2), do: "Draw!"
end
defmodule Money do
  def add({type, amount1}, amount2) do
    {type, amount1 + amount2}
  end

  def add(amount1, amount2) do
    amount1 + amount2
  end
end

cad = {:CAD, 10}
us = {:US, 10}

Money.add(cad, 10)
Money.add(cad, 10)

With

sender = %{email: "Jon@mail.com"}
receiver = %{email: "Drew@mail.com"}

case sender do
  %{email: sender_email} ->
    case receiver do
      %{email: receiver_email} ->
        "FROM: #{sender_email} TO: #{receiver_email}"
    end
end

# %{email: sender_email} = sender

with %{email: sender_email} <- sender, %{email: receiver_email} <- receiver do
  "FROM: #{sender_email} TO: #{receiver_email}"
end