Skip to content

Commit d827fd7

Browse files
authored
reduce: unalias arrays for hcat/vcat (map)reduce (#58490)
Implements mapreduce_first for hcat and vcat
1 parent 3f1fc0d commit d827fd7

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

base/reduce.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ reduce_first(::typeof(add_sum), x::BitUnsignedSmall) = UInt(x)
407407
reduce_first(::typeof(mul_prod), x) = reduce_first(*, x)
408408
reduce_first(::typeof(mul_prod), x::BitSignedSmall) = Int(x)
409409
reduce_first(::typeof(mul_prod), x::BitUnsignedSmall) = UInt(x)
410+
reduce_first(::typeof(vcat), x) = vcat(x)
411+
reduce_first(::typeof(hcat), x) = hcat(x)
410412

411413
"""
412414
Base.mapreduce_first(f, op, x)

test/reduce.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,3 +771,9 @@ end
771771
Val(any(in((:one,:two,:three)),(:four,:three)))
772772
end |> only == Val{true}
773773
end
774+
775+
# `reduce(vcat, A)` should not alias the input for length-1 collections
776+
let A=[1;;]
777+
@test reduce(vcat, Any[A]) !== A
778+
@test reduce(hcat, Any[A]) !== A
779+
end

0 commit comments

Comments
 (0)