length(accumulate(op, input)) == length(input)
I’m looking for a very similar function that satisfies
length(accumulate(op, input; [init])) == length(input) + 1
The last property is satisfied for
accumulate, except for empty inputs.
A rough implementation would be
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)
Here is a way to do this with Transducers.jl:
accumulate′(op, xs; init) = append!!(Scan(op, init), [init], xs)
julia> accumulate′(+, ; init = 0)
julia> accumulate′(+, [1, 2]; init = 0)
Thanks! I’ve been meaning to learn about
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?
Yeah, you can do this:
ScanInit(op, init) = ScanEmit((acc, x) -> (acc, op(acc, x)), init, identity)