Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapAccumulate? #539

Closed
mrufsvold opened this issue Nov 16, 2022 · 1 comment
Closed

MapAccumulate? #539

mrufsvold opened this issue Nov 16, 2022 · 1 comment

Comments

@mrufsvold
Copy link

mrufsvold commented Nov 16, 2022

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
Copy link
Member

@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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants