Skip to content

Instantly share code, notes, and snippets.

@Adzz
Created June 16, 2019 14:55
Show Gist options
  • Save Adzz/a096d85ae02a9ebd2be86754049b3aca to your computer and use it in GitHub Desktop.
Save Adzz/a096d85ae02a9ebd2be86754049b3aca to your computer and use it in GitHub Desktop.
defprotocol Add do
def calculate(a, b)
end
# Now we can define implementations of it for the various kinds of
# data types we might get inside our collections that we are zipping.
defimpl Add, for: Integer do
def calculate(a, b) when is_integer(b), do: a + b
end
defimpl Add, for: Decimal do
def calculate(decimal, decimal_2 = %Decimal{}), do: Decimal.add(decimal, decimal_2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment