diff --git a/src/chainedvector.jl b/src/chainedvector.jl index a5dd69e..d0d401a 100644 --- a/src/chainedvector.jl +++ b/src/chainedvector.jl @@ -761,26 +761,16 @@ Base.map(f::F, x::ChainedVector) where {F} = ChainedVector([map(f, y) for y in x function Base.map!(f::F, A::AbstractVector, x::ChainedVector) where {F} length(A) >= length(x) || throw(ArgumentError("destination must be at least as long as map! source")) - idx = eachindex(A) - st = iterate(idx) - for array in x.arrays - for y in array - @inbounds A[st[1]] = f(y) - st = iterate(idx, st[2]) - end + for (i, j) in zip(eachindex(A), eachindex(x)) + @inbounds A[i] = f(x[j]) end return A end function Base.map!(f::F, x::ChainedVector, A::AbstractVector) where {F} length(x) >= length(A) || throw(ArgumentError("destination must be at least as long as map! source")) - idx = eachindex(A) - st = iterate(idx) - for array in x.arrays - for j in eachindex(array) - @inbounds array[j] = f(A[st[1]]) - st = iterate(idx, st[2]) - end + for (i, j) in zip(eachindex(x), eachindex(A)) + @inbounds x[i] = f(A[j]) end return x end diff --git a/test/chainedvector.jl b/test/chainedvector.jl index faf3d93..8b9ced4 100644 --- a/test/chainedvector.jl +++ b/test/chainedvector.jl @@ -225,6 +225,9 @@ y = ChainedVector([[1,2,3], [4,5,6,7], [8,9,10]]) map!(x -> x + 1, x, y) @test all(x -> x[1] + 1 == x[2], zip(y, x)) + x = ChainedVector([[1,2,3], [4,5,6]]) + y = [1,2] + @test map!(x -> x + 10, x, y)== [11,12,3,4,5,6] # reductions x = ChainedVector([[1,2,3], [4,5,6], [7,8,9,10]])