JuliaFolds / Transducers.jl

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

MapAccumulate? #539

Closed mrufsvold closed 1 year ago

mrufsvold commented 1 year ago

I'd like to create a Transducer that works like Base.accumulate where each element is passed into a function along with the result of the previous call. For example:

> A = [1,2,3,4]
> A |> MapAccumulate(*; init=1) |> collect
# [1, 2, 6, 24]

I'm relatively new to the vernacular of functional programming, so it's quite possible I'm overlooking a function that provides this functionality already. But if not, it would be great to have!

MasonProtter commented 1 year ago

@mrufsvold sorry for the late response. This already exists with Scan:

julia> [1,2,3,4] |> Scan(*, 1) |> collect
4-element Vector{Int64}:
  1
  2
  6
 24

See https://juliafolds.github.io/Transducers.jl/dev/reference/manual/#Transducers.Scan in the manual for more details/