Skip to content

Commit 43a3ea4

Browse files
tkfKristofferC
authored andcommitted
Fix initial value handling with flatten transducer (#34369)
There was a bug in initial value handling of `FlatteningRF` and the following example failed: @test mapfoldl( x -> (x, x), ((a, b), (c, d)) -> (min(a, c), max(b, d)), Iterators.flatten((1:2, 3:4)), ) == (1, 4) This is because `BottomRF(op.rf)` was called inside `FlatteningRF` where `op.rf` is already a "non-bottom" reducing function; here it's a `MappingRF`. As `BottomRF(rf)` forwards anything on the second argument on the first invocation as the first argument (accumulator) of the next calls, we need to make sure that this value is processed through `MappingRF` in the above example. However, if we do `BottomRF(op.rf)` where `op.rf` is a `MappingRF`, this `BottomRF` bypasses any processing that has to happen in `op.rf`. (cherry picked from commit 0ee3264)
1 parent ca34ee8 commit 43a3ea4

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

base/reduce.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct FlatteningRF{T}
112112
end
113113

114114
@inline function (op::FlatteningRF)(acc, x)
115-
op′, itr′ = _xfadjoint(BottomRF(op.rf), x)
115+
op′, itr′ = _xfadjoint(op.rf, x)
116116
return _foldl_impl(op′, acc, itr′)
117117
end
118118

test/reduce.jl

+8
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,11 @@ x = [j^2 for j in i]
535535
i = Base.Slice(0:0)
536536
x = [j+7 for j in i]
537537
@test sum(x) == 7
538+
539+
@testset "initial value handling with flatten" begin
540+
@test mapfoldl(
541+
x -> (x, x),
542+
((a, b), (c, d)) -> (min(a, c), max(b, d)),
543+
Iterators.flatten((1:2, 3:4)),
544+
) == (1, 4)
545+
end

0 commit comments

Comments
 (0)