Created
May 27, 2018 12:15
-
-
Save Adzz/e97d0be53ebe5a75f97129d689bf2c8f 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
defmodule Square do | |
defstruct [:side] | |
end | |
defmodule Circle do | |
defstruct [:radius] | |
end | |
defprotocol Area do | |
def calculate_for(shape) | |
end | |
defprotocol Perimeter do | |
def calculate_for(shape) | |
end | |
defimpl Area, for: Square do | |
def calculate_for(square) do | |
square.side * square.side | |
end | |
end | |
defimpl Perimeter, for: Circle do | |
def calculate_for(circle) do | |
circle.radius * 2 * 3.14 | |
end | |
end | |
Area.calculate_for(%Square{side: 10}) | |
Perimeter.calculate_for(%Circle{radius: 10}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment