Skip to content

Instantly share code, notes, and snippets.

@ybur-yug
Created January 21, 2016 14:48
Show Gist options
  • Select an option

  • Save ybur-yug/d279083c61ab2201ecd6 to your computer and use it in GitHub Desktop.

Select an option

Save ybur-yug/d279083c61ab2201ecd6 to your computer and use it in GitHub Desktop.
defmodule Etudes.Geom do
@spec area(atom(), number(), number()) :: number()
def area(:rectangle, x, y) when x > 0 and y > 0 do
x * y
end
def area(:triangle, base, height) when base > 0 and height > 0 do
(base * height) / 2.0
end
def area(:ellipse, x, y) when x > 0 and y > 0 do
:math.pi * x * y
end
@spec sum(number(), number(), number()) :: number()
def sum(x \\ 1, y \\ 1, z \\ 1), do: x + y + z
end
defmodule GeomTest do
use ExUnit.Case
doctest Etudes.Geom
alias Etudes.Geom
test "calculate area" do
assert Geom.area(:rectangle, 2, 4) == 8
assert Geom.area(:triangle, 4, 2) == 4.0
assert Geom.area(:ellipse, 2, 4) == 25.132741228718345
end
test "calculate a sum of up to 3" do
assert Geom.sum(2) == 4
assert Geom.sum(2, 2) == 5
assert Geom.sum(2, 2, 2) == 6
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment