galbangi

galbangi

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

Most Liked

APB9785

APB9785

Creator of ECSx

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)
qhwa

qhwa

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.

hudsonbay

hudsonbay

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

Result: 500

Where Next?

Popular in Discussions Top

arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19327 150
New
New
PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

We're in Beta

About us Mission Statement