JuliaFolds / Transducers.jl

Efficient transducers for Julia
https://juliafolds.github.io/Transducers.jl/dev/
MIT License
432 stars 24 forks source link

ScanEmit receives the wrong argument #556

Open pepijndevos opened 1 year ago

pepijndevos commented 1 year ago

Here is a reduced example of my code

using Transducers

s = zip(1:6, [0, 1, 0, 2, 1 ,2])

dx = Consecutive(2; step = 1) |> Map((((x1, y), (x2, _)),) -> (x2 - x1, y))
function test(a, b)
    @show a b
    (x, y) = b
    ((a, y), x+a)
end
ix = ScanEmit(test, 0.0)
squash = ReducePartitionBy(((dx, y),) -> y, Completing(((dx1, y1), (dx2, y2)) -> (dx1 + dx2, y2)), (0.0, NaN))

s |> dx |> squash |> collect |> ix |> collect
s |> dx |> squash|> ix |> collect

The case without the extra collect calls test with two floats

ERROR: BoundsError: attempt to access Float64 at index [2]
Stacktrace:
  [1] indexed_iterate(I::Float64, i::Int64, state::Nothing)
    @ Base ./tuple.jl:101
  [2] test(a::Float64, b::Float64)
    @ Main ~/code/CedarSim.jl/test/trans.jl:8
  [3] combine(rf::Transducers.Reduction{…}, a::Transducers.PrivateState{…}, b::Transducers.PrivateState{…})
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/library.jl:1324
  [4] complete(rf::Transducers.Reduction{…}, acc::Transducers.PrivateState{…})
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/partitionby.jl:107
  [5] complete(rf::Transducers.Reduction{…}, result::Transducers.PrivateState{…})
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/core.jl:548
  [6] complete(rf::Transducers.Reduction{…}, acc::Transducers.PrivateState{…})
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/consecutive.jl:97
  [7] __foldl__
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/processes.jl:242 [inlined]
  [8] transduce(rf1::RF, init::Any, coll::Any; simd::Union{Val{true}, Val{false}, Val{:ivdep}, Bool, Symbol}) where RF<:Transducers.AbstractReduction
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/processes.jl:523 [inlined]
  [9] transduce(rf1::RF, init::Any, coll::Any) where RF<:Transducers.AbstractReduction
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/processes.jl:512 [inlined]
 [10] transduce(xform::Transducers.Composition{…}, f::Transducers.AdHocRF{…}, init::BangBang.SafeCollector{…}, coll::Base.Iterators.Zip{…}; kwargs::Base.Pairs{…})
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/processes.jl:506
 [11] transduce(xform::Transducer, f::F, init::Any, coll::Any) where F
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/processes.jl:504 [inlined]
 [12] _collect(xf::Transducers.Composition{…}, coll::Base.Iterators.Zip{…}, ::Transducers.SizeChanging, ::Base.HasShape{…})
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/processes.jl:810
 [13] collect(xf::Transducers.Composition{Consecutive{…}, Transducers.Composition{…}}, coll::Base.Iterators.Zip{Tuple{…}})
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/processes.jl:806 [inlined]
 [14] collect(foldable::Transducers.Eduction{Transducers.Reduction{Consecutive{…}, Transducers.Reduction{…}}, Base.Iterators.Zip{Tuple{…}}})
    @ Transducers ~/.julia/packages/Transducers/IWhZW/src/processes.jl:807 [inlined]
 [15] |>(x::Transducers.Eduction{Transducers.Reduction{Consecutive{…}, Transducers.Reduction{…}}, Base.Iterators.Zip{Tuple{…}}}, f::typeof(collect))
    @ Base ./operators.jl:915
 [16] top-level scope
    @ ~/code/CedarSim.jl/test/trans.jl:15
Some type information was truncated. Use `show(err)` to see complete types.

I guess it's a problem with multiple stateful transducers getting combined incorrectly? No idea how that works exactly.