# \`Base.accumulate\`, but with initial value

**URL:** <https://discourse.julialang.org/t/base-accumulate-but-with-initial-value/39203>\
**Category:** General Usage\
**Created:** [May 10, 2020, 12:22am UTC](https://discourse.julialang.org/t/base-accumulate-but-with-initial-value/39203 "2020-05-10T00:22:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [May 10, 2020, 12:22am UTC](https://discourse.julialang.org/t/base-accumulate-but-with-initial-value/39203/1 "2020-05-10T00:22:37Z")

</div>

`Base.accumulate` satisfies  
`length(accumulate(op, input)) == length(input)`

I’m looking for a very similar function that satisfies  
`length(accumulate(op, input; [init])) == length(input) + 1`  
and  
`isequal(last(accumulate′\prime(args...;kwargs...)), reduce(args...;kwargs...))`

The last property is satisfied for `accumulate`, except for empty inputs.

A rough implementation would be

```julia
accumulate′(args...; kwargs...) = vcat([get(kwargs, :init, :TODO)], accumulate(args...; kwargs...))

```

Is there a cleaner way to get that behavior with existing primitives? It would be cool if there were some composable way to get this behavior, so that e.g. `last(cumsum′(Int[])) == sum(Int[])`

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [May 10, 2020, 12:47am UTC](https://discourse.julialang.org/t/base-accumulate-but-with-initial-value/39203/2 "2020-05-10T00:47:40Z")

</div>

Here is a way to do this with [Transducers.jl](https://github.com/tkf/Transducers.jl):

```julia
using Transducers
accumulate′(op, xs; init) = append!!(Scan(op, init), [init], xs)

```

then

```julia
julia> accumulate′(+, []; init = 0)
1-element Array{Int64,1}:
 0

julia> accumulate′(+, [1, 2]; init = 0)
3-element Array{Int64,1}:
 0
 1
 3

```

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [May 10, 2020, 1:01am UTC](https://discourse.julialang.org/t/base-accumulate-but-with-initial-value/39203/3 "2020-05-10T01:01:43Z")

</div>

Thanks! I’ve been meaning to learn about `Transducers.jl`.

I think it would be great if there were a way to get this behavior without needing to define a helper, and optionally mentioning `init` just once, using the ontology of InitialValues.jl.

Does something like that fit into the transducers model? Would something like `append!!(ScanInit(+, -10), [], [1,2,3])` be possible?

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [May 10, 2020, 1:24am UTC](https://discourse.julialang.org/t/base-accumulate-but-with-initial-value/39203/4 "2020-05-10T01:24:50Z")

</div>

> [@goretkin](#):
>
> Does something like that fit into the transducers model? Would something like `append!!(ScanInit(+, -10), [], [1,2,3])` be possible?

Yeah, you can do this:

```julia
ScanInit(op, init) = ScanEmit((acc, x) -> (acc, op(acc, x)), init, identity)

```

See: [https://tkf.github.io/Transducers.jl/dev/manual/#Transducers.ScanEmit](https://tkf.github.io/Transducers.jl/dev/manual/#Transducers.ScanEmit)
