I have thought about this a couple of times but another discussion we keep
having is to make Enum.sum/1 part of a protocol for performance and then we
wouldn't be able to optimize it in the Enum.sum/2 variant. I wonder if that
can be confusing. So for now, I would go the reduce route if you are really
worried about performance here.

On Fri, Jul 23, 2021 at 6:51 AM Sabiwara Yukichi <sabiw...@gmail.com> wrote:

> 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 elixir-lang-core+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/CANnyohZtg19ES%2B52KzCX%3DYnY-rgaLoNcnuRaCyrmtbMpTQ1NDg%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/CANnyohZtg19ES%2B52KzCX%3DYnY-rgaLoNcnuRaCyrmtbMpTQ1NDg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B5JXO-tt2JN5k-R_vmv8nOgw7bjzO%2BhXJ2CnanoAwp-A%40mail.gmail.com.

Reply via email to