Skip to content

Instantly share code, notes, and snippets.

@Adzz
Last active May 30, 2018 16:06
Show Gist options
  • Select an option

  • Save Adzz/bbd9749a66199ca572214927d02676ad to your computer and use it in GitHub Desktop.

Select an option

Save Adzz/bbd9749a66199ca572214927d02676ad to your computer and use it in GitHub Desktop.
defmodule Square do
defstruct [:side]
end
defmodule Circle do
defstruct [:radius]
end
defmodule Area do
def calculate(shape) do
case shape do
%Square{side: side} -> side * side
%Circle{radius: radius} -> radius * radius * 3.14
end
end
end
Area.calculate(%Square{side: 10}) #=> 100
Area.calculate(%Circle{radius: 10}) #=> 314.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment