Feature request: Add `Enum.sum_by/2` (like existing `Enum.max_by/2`)

84 views
Skip to first unread message

Alexey Chernenkov

unread,
Feb 2, 2018, 2:48:08 AM2/2/18
to elixir-lang-core
I'm missing `Enum.sum_by/2` func. There are several other `xxx_by` methods (e.g. `sort_by` and `max_by`).

Kelvin Raffael Stinghen

unread,
Feb 2, 2018, 5:27:46 AM2/2/18
to elixir-l...@googlegroups.com
The thing with `x_by` functions is that they still return the whole list (like in `sort_by`) or an element of it (like in `max_by`). 

Example right from `h Enum.max_by`:

```elixir
iex> Enum.max_by(["a", "aa", "aaa"], fn(x) -> String.length(x) end)
"aaa"
```

`Enum.sum` totally changes the returning value type to another, so I guess there is no point of having them since you can totally use the function `map` on your enumerable to get what you want. For example:

```elixir
iex> [“a”, “aa”, “aaa”] |> Stream.map(&String.length/1) |> Enum.sum()
=> 6
```

Best,
Kelvin Stinghen
kelvin....@me.com

On Feb 2, 2018, at 05:48, Alexey Chernenkov <a.y.che...@gmail.com> wrote:

I'm missing `Enum.sum_by/2` func. There are several other `xxx_by` methods (e.g. `sort_by` and `max_by`).

--
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-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/f7c51011-5ff4-4667-8125-75ced517887a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrea Leopardi

unread,
Feb 2, 2018, 5:38:06 AM2/2/18
to elixir-lang-core
Also, sum/1 is easily replaceable with Enum.reduce(enum, &+/2). sum_by(enum, fun) could be written as:

Enum.reduce(enum, 0, &(fun.(&1) + &2))

If you use this a lot in your code, you can extract it to a function and it will maintain the same clarity as sum_by :).


Andrea Leopardi

On Fri, Feb 2, 2018 at 11:27 AM, Kelvin Raffael Stinghen <kelvin....@gmail.com> wrote:
The thing with `x_by` functions is that they still return the whole list (like in `sort_by`) or an element of it (like in `max_by`). 

Example right from `h Enum.max_by`:

```elixir
iex> Enum.max_by(["a", "aa", "aaa"], fn(x) -> String.length(x) end)
"aaa"
```

`Enum.sum` totally changes the returning value type to another, so I guess there is no point of having them since you can totally use the function `map` on your enumerable to get what you want. For example:

```elixir
iex> [“a”, “aa”, “aaa”] |> Stream.map(&String.length/1) |> Enum.sum()
=> 6
```

Best,
Kelvin Stinghen
kelvin....@me.com

On Feb 2, 2018, at 05:48, Alexey Chernenkov <a.y.che...@gmail.com> wrote:

I'm missing `Enum.sum_by/2` func. There are several other `xxx_by` methods (e.g. `sort_by` and `max_by`).

--
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+unsubscribe@googlegroups.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/7D23AA55-92B7-4EAA-A062-09F6493C73C5%40gmail.com.
Reply all
Reply to author
Forward
0 new messages