Created
October 15, 2015 17:08
-
-
Save mzemel/a3e426963ef3b08f600e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Basic function | |
defmodule A do | |
def add(a, b) do | |
a + b | |
end | |
end | |
A.add(1, 2) | |
# => 3 | |
# Guard clauses | |
defmodule B do | |
def add(a, b) when is_string(a) do | |
a <> b | |
end | |
end | |
B.add("foo", "bar") | |
# => "foobar" | |
# Combine to overload functions | |
defmodule C do | |
def add(a, b) when is_string(a), do: a <> b | |
def add(a, b), do: a + b | |
end | |
C.add(1,2) # => 3 | |
C.add("foo","bar") # => "foobar" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment