Skip to content

Instantly share code, notes, and snippets.

@zacck-zz
Last active November 1, 2017 11:27
Show Gist options
  • Save zacck-zz/ba4ef4d35a0a35371853c2a9ab8bd448 to your computer and use it in GitHub Desktop.
Save zacck-zz/ba4ef4d35a0a35371853c2a9ab8bd448 to your computer and use it in GitHub Desktop.
defmodule Budget do
def sub_totals(expenses) do
allCategories =
Enum.uniq for %{type: t, cost: _} <- expenses, do: t
category_costs = Enum.map(allCategories, fn(x) -> category_total(expenses, x) end)
end
def category_total(expenses, category) do
catExpenses =
Enum.sum for %{type: ^category, cost: c} <- expenses, do: c
%{category: category, total: catExpenses}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment