どうもキーワードリストの car はリストらしい。むむむむ。
昨晩の動かないやつ、以下な観点で修正をしています。
- いくつかのリストのパターンを copyorder でフォロー
- head は total_amount を計算してしまう
- リストが
[head | []]
と[]
のケイスにマッチさせる - 内包表記で [] が出てくるケイスをフォロー
def copyorder([head | tail], tax_rates) do
[_calc_total(head, tax_rates) | copyorder(tail, tax_rates)]
end
def copyorder([head | []], tax_rates) do
_calc_total(head, tax_rates)
end
def copyorder([], tax_rates), do: []
defp _calc_total(head, tax_rates) do
rates = for x <- Keyword.values(head), y <- Keyword.keys(tax_rates), x == y, do: Keyword.get(tax_rates, y)
_ret_from_calc_total(head, rates)
end
defp _ret_from_calc_total(kl, []) do
Keyword.put(kl, :total_amount, Keyword.get(kl, :net_amount))
end
defp _ret_from_calc_total(kl, num) do
Keyword.put(kl, :total_amount, Keyword.get(kl, :net_amount) + Keyword.get(kl, :net_amount) * List.first(num))
end
なんとなくまだ微妙感がありますがエントリ投入しちゃえ。
一応動いてるみたい?
なのですがどうなのだろうか。
iex(45)> MyEnum.copyorder(orders, tax_rates)
[
[total_amount: 107.5, id: 123, ship_to: :NC, net_amount: 100.0],
[total_amount: 35.5, id: 124, ship_to: :OK, net_amount: 35.5],
[total_amount: 25.92, id: 125, ship_to: :TX, net_amount: 24.0],
[total_amount: 48.384, id: 126, ship_to: :TX, net_amount: 44.8],
[total_amount: 26.875, id: 127, ship_to: :NC, net_amount: 25.0],
[total_amount: 10.0, id: 128, ship_to: :MA, net_amount: 10.0],
[total_amount: 102.0, id: 129, ship_to: :CA, net_amount: 102.0],
[total_amount: 53.75, id: 130, ship_to: :NC, net_amount: 50.0]
]