Skip to content

Instantly share code, notes, and snippets.

@fastjames
Created August 7, 2015 14:13
Show Gist options
  • Save fastjames/88f6ee2ac8079bfda22e to your computer and use it in GitHub Desktop.
Save fastjames/88f6ee2ac8079bfda22e to your computer and use it in GitHub Desktop.
Probably bad mean in elixir
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