Skip to content

Instantly share code, notes, and snippets.

@karlosmid
Created February 23, 2021 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlosmid/284220586e73bc4aaaf504b26c93c54e to your computer and use it in GitHub Desktop.
Save karlosmid/284220586e73bc4aaaf504b26c93c54e to your computer and use it in GitHub Desktop.
Example of function guards
defmodule Vat do
@moduledoc """
Elixir In Action book example for function overloading
"""
def price(%{country: :germany, price: price}), do: calc_total_price(1.19, price)
def price(%{country: :greece, price: price}), do: calc_total_price(1.24, price)
def price(%{country: :croatia, price: price}), do: calc_total_price(1.25, price)
def price(unknown), do: {:unknown_country_data, unknown}
defp calc_total_price(vat, price) when vat > 1.20, do: "Expensive country: " <> "#{vat * price}"
defp calc_total_price(vat, price), do: vat * price
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment