Skip to content

Commit 5ecc17f

Browse files
authored
Implement accumulate and friends for arbitrary iterators (#34656)
1 parent 4c45a91 commit 5ecc17f

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ New library features
9292

9393
* `isapprox` (or ``) now has a one-argument "curried" method `isapprox(x)` which returns a function, like `isequal` (or `==`)` ([#32305]).
9494
* `Ref{NTuple{N,T}}` can be passed to `Ptr{T}`/`Ref{T}` `ccall` signatures ([#34199])
95-
* `accumulate`, `cumsum`, and `cumprod` now support `Tuple` ([#34654]).
95+
* `accumulate`, `cumsum`, and `cumprod` now support `Tuple` ([#34654]) and arbitrary iterators ([#34656]).
9696
* In `splice!` with no replacement, values to be removed can now be specified with an
9797
arbitrary iterable (instead of a `UnitRange`) ([#34524]).
9898

base/accumulate.jl

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ function cumsum(A::AbstractArray{T}; dims::Integer) where T
9292
end
9393

9494
"""
95-
cumsum(itr::Union{AbstractVector,Tuple})
95+
cumsum(itr)
9696
9797
Cumulative sum an iterator. See also [`cumsum!`](@ref)
9898
to use a preallocated output array, both for performance and to control the precision of the
9999
output (e.g. to avoid overflow).
100100
101101
!!! compat "Julia 1.5"
102-
`cumsum` on a tuple requires at least Julia 1.5.
102+
`cumsum` on a non-array iterator requires at least Julia 1.5.
103103
104104
# Examples
105105
```jldoctest
@@ -117,6 +117,12 @@ julia> cumsum([fill(1, 2) for i in 1:3])
117117
118118
julia> cumsum((1, 1, 1))
119119
(1, 2, 3)
120+
121+
julia> cumsum(x^2 for x in 1:3)
122+
3-element Array{Int64,1}:
123+
1
124+
5
125+
14
120126
```
121127
"""
122128
cumsum(x::AbstractVector) = cumsum(x, dims=1)
@@ -170,14 +176,14 @@ function cumprod(A::AbstractArray; dims::Integer)
170176
end
171177

172178
"""
173-
cumprod(itr::Union{AbstractVector,Tuple})
179+
cumprod(itr)
174180
175181
Cumulative product of an iterator. See also
176182
[`cumprod!`](@ref) to use a preallocated output array, both for performance and
177183
to control the precision of the output (e.g. to avoid overflow).
178184
179185
!!! compat "Julia 1.5"
180-
`cumprod` on a tuple requires at least Julia 1.5.
186+
`cumprod` on a non-array iterator requires at least Julia 1.5.
181187
182188
# Examples
183189
```jldoctest
@@ -195,6 +201,12 @@ julia> cumprod([fill(1//3, 2, 2) for i in 1:3])
195201
196202
julia> cumprod((1, 2, 1))
197203
(1, 2, 2)
204+
205+
julia> cumprod(x^2 for x in 1:3)
206+
3-element Array{Int64,1}:
207+
1
208+
4
209+
36
198210
```
199211
"""
200212
cumprod(x::AbstractVector) = cumprod(x, dims=1)
@@ -210,6 +222,9 @@ also [`accumulate!`](@ref) to use a preallocated output array, both for performa
210222
to control the precision of the output (e.g. to avoid overflow). For common operations
211223
there are specialized variants of `accumulate`, see: [`cumsum`](@ref), [`cumprod`](@ref)
212224
225+
!!! compat "Julia 1.5"
226+
`accumulate` on a non-array iterator requires at least Julia 1.5.
227+
213228
# Examples
214229
```jldoctest
215230
julia> accumulate(+, [1,2,3])
@@ -250,6 +265,10 @@ julia> accumulate(+, fill(1, 3, 3), dims=2)
250265
```
251266
"""
252267
function accumulate(op, A; dims::Union{Nothing,Integer}=nothing, kw...)
268+
if dims === nothing && !(A isa AbstractVector)
269+
# This branch takes care of the cases not handled by `_accumulate!`.
270+
return collect(Iterators.accumulate(op, A; kw...))
271+
end
253272
nt = kw.data
254273
if nt isa NamedTuple{()}
255274
out = similar(A, promote_op(op, eltype(A), eltype(A)))

base/iterators.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -443,40 +443,51 @@ reverse(f::Filter) = Filter(f.flt, reverse(f.itr))
443443

444444
# Accumulate -- partial reductions of a function over an iterator
445445

446-
struct Accumulate{F,I}
446+
struct Accumulate{F,I,T}
447447
f::F
448448
itr::I
449+
init::T
449450
end
450451

451452
"""
452-
Iterators.accumulate(f, itr)
453+
Iterators.accumulate(f, itr; [init])
453454
454455
Given a 2-argument function `f` and an iterator `itr`, return a new
455456
iterator that successively applies `f` to the previous value and the
456457
next element of `itr`.
457458
458459
This is effectively a lazy version of [`Base.accumulate`](@ref).
459460
461+
!!! compat "Julia 1.5"
462+
Keyword argument `init` is added in Julia 1.5.
463+
460464
# Examples
461465
```jldoctest
462-
julia> f = Iterators.accumulate(+, [1,2,3,4])
463-
Base.Iterators.Accumulate{typeof(+),Array{Int64,1}}(+, [1, 2, 3, 4])
466+
julia> f = Iterators.accumulate(+, [1,2,3,4]);
464467
465468
julia> foreach(println, f)
466469
1
467470
3
468471
6
469472
10
473+
474+
julia> f = Iterators.accumulate(+, [1,2,3]; init = 100);
475+
476+
julia> foreach(println, f)
477+
101
478+
103
479+
106
470480
```
471481
"""
472-
accumulate(f, itr) = Accumulate(f, itr)
482+
accumulate(f, itr; init = Base._InitialValue()) = Accumulate(f, itr, init)
473483

474484
function iterate(itr::Accumulate)
475485
state = iterate(itr.itr)
476486
if state === nothing
477487
return nothing
478488
end
479-
return (state[1], state)
489+
val = Base.BottomRF(itr.f)(itr.init, state[1])
490+
return (val, (val, state[2]))
480491
end
481492

482493
function iterate(itr::Accumulate, state)
@@ -491,7 +502,7 @@ end
491502
length(itr::Accumulate) = length(itr.itr)
492503
size(itr::Accumulate) = size(itr.itr)
493504

494-
IteratorSize(::Type{Accumulate{F,I}}) where {F,I} = IteratorSize(I)
505+
IteratorSize(::Type{<:Accumulate{F,I}}) where {F,I} = IteratorSize(I)
495506
IteratorEltype(::Type{<:Accumulate}) = EltypeUnknown()
496507

497508
# Rest -- iterate starting at the given state

test/iterators.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,17 @@ end
793793
@test collect(Iterators.accumulate(+, [1,2])) == [1,3]
794794
@test collect(Iterators.accumulate(+, [1,2,3])) == [1,3,6]
795795
@test collect(Iterators.accumulate(=>, [:a,:b,:c])) == [:a, :a => :b, (:a => :b) => :c]
796+
@test collect(Iterators.accumulate(+, (x for x in [true])))::Vector{Int} == [1]
797+
@test collect(Iterators.accumulate(+, (x for x in [true, true, false])))::Vector{Int} == [1, 2, 2]
798+
@test collect(Iterators.accumulate(+, (x for x in [true]), init=10.0))::Vector{Float64} == [11.0]
796799
@test length(Iterators.accumulate(+, [10,20,30])) == 3
797800
@test size(Iterators.accumulate(max, rand(2,3))) == (2,3)
798801
@test Base.IteratorSize(Iterators.accumulate(max, rand(2,3))) === Base.IteratorSize(rand(2,3))
799802
@test Base.IteratorEltype(Iterators.accumulate(*, ())) isa Base.EltypeUnknown
800803
end
804+
805+
@testset "Base.accumulate" begin
806+
@test cumsum(x^2 for x in 1:3) == [1, 5, 14]
807+
@test cumprod(x + 1 for x in 1:3) == [2, 6, 24]
808+
@test accumulate(+, (x^2 for x in 1:3); init=100) == [101, 105, 114]
809+
end

0 commit comments

Comments
 (0)