Last active
August 29, 2015 14:01
-
-
Save findjashua/2ed4204247d76849eb81 to your computer and use it in GitHub Desktop.
This file contains 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 Geometry do | |
@moduledoc "geometric formulae" | |
import IO | |
defmodule Quad do | |
@moduledoc "Quadilateral formulae" | |
defmodule Rect do | |
# can't import Geometry | |
# since inner modules are loaded before outer ones | |
@doc "area of rect" | |
@spec area(number, number) :: number | |
def area(a, b) do | |
Geometry.area(a, b) | |
end | |
end | |
defmodule Square do | |
import Rect | |
# since Square imports Rect, | |
# it can only be defined after Rect | |
@doc "area of square" | |
@spec area(number) :: number | |
def area(a) do | |
puts "delegating to rectangle" # inner modules inherit imports | |
area(a, a) | |
end | |
end | |
end | |
defmodule Circle do | |
@pi 3.14 | |
@doc "area of circle" | |
@spec area(number) :: number | |
def area(r) do | |
Geometry.area(@pi, r*r) | |
end | |
end | |
@doc "area of geometric shape" | |
@spec area(number, number) :: number | |
def area(a, b) do | |
prod(a, b) | |
end | |
@spec prod(number, number) :: number | |
defp prod(a, b\\0) do | |
a*b | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment