Skip to content
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

Add function + two-argument method to reducers #35017

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ for (fname, _fname, op) in [(:sum, :_sum, :add_sum), (:prod, :_prod,
# User-facing methods with keyword arguments
@inline ($fname)(a::AbstractArray; dims=:) = ($_fname)(a, dims)
@inline ($fname)(f, a::AbstractArray; dims=:) = ($_fname)(f, a, dims)
@inline ($fname)(f, a::AbstractArray, b::AbstractArray; dims=:) =
($_fname)(((a,b),)->f(a,b), zip(a,b), dims)
Copy link
Member

Choose a reason for hiding this comment

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

If so, why don't make it more general? sum(f, args...) as _sum(args->f(args...), zip(args...))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

Copy link
Member

Choose a reason for hiding this comment

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

If so, why don't make it more general? sum(f, args...) as _sum(args->f(args...), zip(args...))

This could even replace the single argument method, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could, but I will benchmark that first to ensure there's not a performance hit for the zip.


# Underlying implementations using dispatch
($_fname)(a, ::Colon) = ($_fname)(identity, a, :)
Expand Down