#Define a Protocol
A Protocol is a way to dispatch to a particular implementation of a function based on the type of the parameter.
The macros defprotocol and defimpl are used to define Protocols and Protocol implementations for different types in the following example.
defprotocol Triple do
def triple(input)
end
defimpl Triple, for: Integer do
def triple(int) do
int * 3
end
end
defimpl Triple, for: List do
def triple(list) do
list ++ list ++ list
end
end Load the code into iex and execute
iex> Triple.triple(3)
9
Triple.triple([1, 2])
[1, 2, 1, 2,1,2]