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

Fix sum() and prod() for tuples #41510

Merged
merged 7 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 3 additions & 10 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,17 +488,10 @@ reverse(t::Tuple) = revargs(t...)

## specialized reduction ##

# TODO: these definitions cannot yet be combined, since +(x...)
# where x might be any tuple matches too many methods.
# TODO: this is inconsistent with the regular sum in cases where the arguments
# require size promotion to system size.
sum(x::Tuple{Any, Vararg{Any}}) = +(x...)

# NOTE: should remove, but often used on array sizes
# TODO: this is inconsistent with the regular prod in cases where the arguments
# require size promotion to system size.
prod(x::Tuple{}) = 1
prod(x::Tuple{Any, Vararg{Any}}) = *(x...)
# This is consistent with the regular prod because there is no need for size promotion
# if all elements in the tuple are of system size.
prod(x::Tuple{Int, Vararg{Int}}) = *(x...)
petvana marked this conversation as resolved.
Show resolved Hide resolved

all(x::Tuple{}) = true
all(x::Tuple{Bool}) = x[1]
Expand Down
15 changes: 15 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@ end
@test prod(()) === 1
@test prod((1,2,3)) === 6

# issue 39182
@test sum((0xe1, 0x1f)) === sum([0xe1, 0x1f])
@test sum((Int8(3),)) === Int(3)
@test sum((UInt8(3),)) === UInt(3)
@test sum((3,)) === Int(3)
@test sum((3.0,)) === 3.0
@test sum(["a"]) == sum(("a",))
petvana marked this conversation as resolved.
Show resolved Hide resolved

# issue 39183
@test prod((Int8(100), Int8(100))) === 10000
@test prod((Int8(3),)) === Int(3)
@test prod((UInt8(3),)) === UInt(3)
@test prod((3,)) === Int(3)
@test prod((3.0,)) === 3.0
petvana marked this conversation as resolved.
Show resolved Hide resolved

@testset "all" begin
@test all(()) === true
@test all((false,)) === false
Expand Down