Created
August 7, 2015 14:13
-
-
Save fastjames/88f6ee2ac8079bfda22e to your computer and use it in GitHub Desktop.
Probably bad mean in elixir
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 FastMath do | |
def mean([]), do: 0 | |
def mean([a]), do: a | |
def mean(list) do | |
_mean(list, 0, 0) | |
end | |
# terminal condition | |
defp _mean([], total, count) do | |
total / count | |
end | |
defp _mean([head|tail], total, count) do | |
new_count = count + 1 | |
new_total = total + head | |
_mean(tail, new_total, new_count) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment