[Proposal] Add function average or avg to Enum module.

101 views
Skip to first unread message

Julian Somoza

unread,
May 22, 2023, 3:05:58 PM5/22/23
to elixir-l...@googlegroups.com
Just as is there a function to get the min, max, sum, etc values from an Enum, would be useful to have also an average function.

Enum.avg/1 or Enum.average/1

If it's ok for the community, I will be happy to implement it.

Example:

iex> Enum.avg(0..10)
5

Andrey Yugai

unread,
May 22, 2023, 4:25:36 PM5/22/23
to elixir-l...@googlegroups.com
Whether any function is worth of stdlib is a really subtle topic, but I think it'd be helpful to try to consider why average function hasn't been added yet.

From my experience, the order on any kind of elements comes up pretty often, thus min/max values are widely used. In the same vein sum also tends to pop up whenever some sort of data processing happens.

Now, there are domains that involve number crunching, and within them it's imperative to have average, mode, median and whatnot to achieve any result. But it's important to have in mind these are more specialized applications of a language. They're far better served in external libraries, allowing users to make a choice of not including this functionality in app, and also enabling the most efficient implementation possible, like Nx having its own compilation pipeline to bypass limitations of BEAM.





-------- Original Message --------
--
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/CACFi_YPY5QURbQ8G%3DqA%3Dt6iQLWUToQygU-jo1%2BzdmW%3DFE3EmKw%40mail.gmail.com.

Kip

unread,
May 22, 2023, 4:40:32 PM5/22/23
to elixir-lang-core
I would not be in favour of adding average to Enum for the simple reason that there is no guarantee that average has meaning for a given enumerable. Its Erlang's term ordering rules than mean max and min have meaning for all terms. That is not true for averages. 

Marten Wijnja

unread,
May 23, 2023, 3:38:47 AM5/23/23
to elixir-l...@googlegroups.com

Adding on to what Kip and Andrey said: Assuming your Enumerable contains only numbers, there are a bunch of already-existing external libraries that implement max/min/mean/median/mode etc. The Math library is an example that comes to mind.

~Marten / Qqwy

Julian Somoza

unread,
May 23, 2023, 2:01:08 PM5/23/23
to elixir-l...@googlegroups.com
I got your point and will keep it in mind the next time I propose some features.

Thanks so much!

Reply all
Reply to author
Forward
0 new messages