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 Project do | |
def total_cost(shape, cost_per_square_meter) do | |
Shape.area(shape) * cost_per_square_meter | |
end | |
end |
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
defimpl Shape, for: EquilateralTriangle do | |
def area(shape) do | |
# some basic high school math that I've forgotten | |
end | |
end |
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 EquilateralTriangle do | |
defstruct [:side] | |
end |
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
%Square{side: 10} | |
|> Shape.area() #=> 100 |
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
defimpl Shape, for: Circle do | |
def area(shape) do | |
shape.radius * shape.radius * 3.14 | |
end | |
end |
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
defimpl Shape, for: Square do | |
def area(shape) do | |
shape.side * shape.side | |
end | |
end |
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
defprotocol Shape do | |
def area(shape) | |
end |
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 Project do | |
def total_cost(shape, cost_per_square_meter) do | |
Shape.area(shape) * cost_per_square_meter | |
end | |
end |
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 Project do | |
def total_cost(shape = %Square{}, cost_per_square_meter) do | |
Square.area(shape) * cost_per_square_meter | |
end | |
def total_cost(shape = %Circle{}, cost_per_square_meter) do | |
Circle.area(shape) * cost_per_square_meter | |
end | |
end |
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 Project do | |
def total_cost(shape, cost_per_square_meter) do | |
case shape do | |
%Square{} -> Square.area(shape) * cost_per_square_meter | |
%Circle{} -> Circle.area(shape) * cost_per_square_meter | |
end | |
end | |
end |