Sum of list of maps

hi i am having problem with enum reduce. i am trying to sum values from lat of maps. getting error can any body help please

list = [
%{“price” => “100”, “trans_type” => “cr”},
%{“price” => “100”, “trans_type” => “db”},
%{“price” => “300”, “trans_type” => “cr”}
]

Enum.reduce(list, fn item, acc → %{val: String.to_integer(item[“price”]) + String.to_integer(acc[“price”])} end)
** (ArgumentError) errors were found at the given arguments:

  • 1st argument: not a binary

    :erlang.binary_to_integer(nil)
    (stdlib 3.15.1) erl_eval.erl:685: :erl_eval.do_apply/6
    (stdlib 3.15.1) erl_eval.erl:477: :erl_eval.expr/5
    (stdlib 3.15.1) erl_eval.erl:791: :erl_eval.eval_map_fields/5
    (stdlib 3.15.1) erl_eval.erl:260: :erl_eval.expr/5
    (elixir 1.12.2) lib/enum.ex:2385: Enum.“-reduce/2-lists^foldl/2-0-”/3

thanks

list 
|> Enum.map(fn %{"price" => price} -> price 
|> String.to_integer() end) 
|> Enum.sum()

Result: 500

2 Likes

If the end result you want is an integer, then it’s best to start with an integer as the accumulator, like this:

Enum.reduce(list, 0, fn item, acc ->
  acc + String.to_integer(item[”price”])
end)
4 Likes

Could do pure recursion:

def reduce(list), do: reduce(list, 0)

def reduce([], acc), do: acc
def reduce([%{"price" => value} | rest], acc), do: reduce(rest, acc + String.to_integer(value))
def reduce([_hd | rest], acc), do: reduce(rest, acc)
2 Likes

I just want to point out why you received that error.

reduce(list, fn item, acc -> do_something(...) end)

The result of the do_something(...) will be passed as the new acc. It is a map (%{val: 200}) after the first round of run.

String.to_integer(%{val: 200}["price"]}) leads to the exception. So you just need to change :val into "price" as the map key to bypass the exception. But the above approaches provided by @hudsonbay @APB9785 and @mpope are better IMO.

3 Likes