Hi!

Recently I found myself several times in a situation where I needed to sum
from an enumerable after applying a transformation, e.g. sum one field from
a list of structs.
There is a temptation to use Enum.map(list, fun) |> Enum.sum() which would
be wasteful since it needs to build the intermediate list.
Enum.reduce(list, 0, &(fun.(&1) + &2) works but it is less straightforward
and less explicit about the intent.

I wondered if Enum.sum(list, fun) (or Enum.sum_by(list, fun)?) could be a
nice addition that would be consistent with some other Enum functions such
as Enum.count/2, Enum.any?/2. What do you think?

My branch for reference:
https://github.com/elixir-lang/elixir/compare/master...sabiwara:sum/2?expand=1

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CANnyohZtg19ES%2B52KzCX%3DYnY-rgaLoNcnuRaCyrmtbMpTQ1NDg%40mail.gmail.com.

Reply via email to