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
an.leopa...@gmail.com

On Fri, Feb 2, 2018 at 11:27 AM, Kelvin Raffael Stinghen <
kelvin.sting...@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.sting...@me.com
>
> On Feb 2, 2018, at 05:48, Alexey Chernenkov <a.y.chernen...@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+unsubscr...@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
> <https://groups.google.com/d/msgid/elixir-lang-core/f7c51011-5ff4-4667-8125-75ced517887a%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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/7D23AA55-92B7-4EAA-A062-09F6493C73C5%40gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/7D23AA55-92B7-4EAA-A062-09F6493C73C5%40gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAM9Rf%2BLexw7Vu67NghkhxVEx5knuygA3OtAYG064y%3DSwSSCb-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to