Skip to content

Instantly share code, notes, and snippets.

@lucca65
Created August 20, 2015 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucca65/0dcf81c628fd10d6122f to your computer and use it in GitHub Desktop.
Save lucca65/0dcf81c628fd10d6122f to your computer and use it in GitHub Desktop.
Gist fixing Tiago Davi code
defmodule Tax do
def get_tax_rates do
[ NC: 0.075, TX: 0.08 ]
end
def get_orders do
[
[ id: 123, ship_to: :NC, net_amount: 100.00 ],
[ id: 124, ship_to: :OK, net_amount: 35.50 ],
[ id: 125, ship_to: :TX, net_amount: 24.00 ],
[ id: 126, ship_to: :TX, net_amount: 44.80 ],
[ id: 127, ship_to: :NC, net_amount: 25.00 ],
[ id: 128, ship_to: :MA, net_amount: 10.00 ],
[ id: 129, ship_to: :CA, net_amount: 102.00 ],
[ id: 120, ship_to: :NC, net_amount: 50.00 ]
]
end
def calculate(orders, tax_rates), do: _calculate(orders, tax_rates)
defp _calculate(orders, tax_rates) do
Enum.map(orders, &(_add_tax(&1,tax_rates)))
end
defp _add_tax(order = [id: id, ship_to: ship_to, net_amount: net_amount], tax_rates) do
tax_rate = Keyword.get(tax_rates, ship_to, 0)
total = (net_amount * tax_rate) + net_amount
Keyword.put(order, :total_amount, total)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment