Skip to content

Instantly share code, notes, and snippets.

@mzemel
Last active October 15, 2015 17:18
Show Gist options
  • Save mzemel/38dbfd2d82c492192e4c to your computer and use it in GitHub Desktop.
Save mzemel/38dbfd2d82c492192e4c to your computer and use it in GitHub Desktop.
defmodule D do
def add(a, b, c), do: a + b + c # Arity 3
def add(b, b), do: 3 * b # Pattern matching
def add("i", b), do: "#{b}i" # Pattern matching
def add(a, _), do: a + a # Strange default, but we'll go with it
def add(_), do: IO.puts "Can't add one number, silly"
end
D.add(1, 2, 3)
# => 6
D.add(5, 5)
# => 15
D.add("i", 10)
# => 10i
D.add(3, 4)
# => 6
D.add(10)
# => "Can't add one number, silly"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment