-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Adds support for Enum.sum/2 #11381
Conversation
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
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 |
There was a problem hiding this comment.
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 🙌
There was a problem hiding this comment.
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.
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. :) |
Ah @josevalim I wasn't aware of 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 😊 |
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 |
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! |
This PR adds a mapper function argument to the
sum
function argument.There are at least two main benefits with this addition:
sum
becomes available to a wider range of enumerables (e.g. maps)