Skip to content

Adds support for Enum.sum/2 #11381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

tiagonbotelho
Copy link

@tiagonbotelho tiagonbotelho commented Nov 11, 2021

This PR adds a mapper function argument to the sum function argument.

There are at least two main benefits with this addition:

  1. sum becomes available to a wider range of enumerables (e.g. maps)
  2. Added expliciteness without requiring an Enum.reduce/3 or Enum.map/2 |> Enum.sum/1 to be done

This commit adds a mapper function argument to the `sum` function argument.

There are at least two main benefits with this addition:
1. `sum` becomes available to a wider range of enumerables (e.g. maps)
2. Added expliciteness without requiring an Enum.reduce/3 or Enum.map/2 |> Enum.sum/1 to be done
@tiagonbotelho
Copy link
Author

I probably should've opened a discussion in the mailing list but the change felt so small that it almost didn't seem worth it. If that'd be preferred I'm more than happy to create it 😊

@spec sum(t, (element -> number)) :: number
def sum(enumerable, fun \\ fn item -> item end)

def sum(first..last//step, fun) do
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably the bit I'm more concerned about, from my testing it seems like the approach of using the fun on the "edges" of the range and then coercing it back into a range, seems like the way to go. But I'm open to feedback on this 🙌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only work if the function is linear, which we cannot guarantee. For example, &:math.sin/1 wouldn’t work as callback.

@josevalim
Copy link
Member

Hi @tiagonbotelho! 👋

As you noted, a custom sum can be achieved using reduce, as you noted, and the other aggregation function with arity 2 that we have, count/2, has the second function working as a filtering. So this would add some inconsistency and it can be easily achievable in other ways, for this reason I am 👎 on this change. :)

@tiagonbotelho
Copy link
Author

tiagonbotelho commented Nov 11, 2021

Ah @josevalim I wasn't aware of count/2 that is a good point 👍 I'd still prefer to have a straightforward Enum.sum/2 with an explicit mapper function (similar to what we have in Enum.sort_by/3) rather than an Enum.reduce/3 which requires a bit of parsing in order to understand what is really going on.

It is just a matter of preference though, if we are confident this would lead to some inconsistency in the public API and don't feel like this is a strong enough reason to move forward with this change, I'm more than happy to drop it 😊

@josevalim
Copy link
Member

Right, sum_by may make more sense then but I am not strongly in favor of the function.

@tiagonbotelho
Copy link
Author

Right, sum_by may make more sense then but I am not strongly in favor of the function.

Agreed 👍 , as I mentioned this might come down to a matter of preference, so I'm happy to close the PR if we think this shouldn't be added to the Enum public API

@josevalim
Copy link
Member

Hi @tiagonbotelho, I am closing the PR due to the points above. Feel free to open up a discussion in the mailing list if you want a more general discussion!

@josevalim josevalim closed this Nov 11, 2021
@v0idpwn v0idpwn mentioned this pull request Dec 7, 2021
@hauleth hauleth mentioned this pull request Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants