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

Add mapWithState #122

Open
BalmungSan opened this issue May 21, 2022 · 4 comments
Open

Add mapWithState #122

BalmungSan opened this issue May 21, 2022 · 4 comments

Comments

@BalmungSan
Copy link
Contributor

The signature would be something like:

def mapWithState[S, B](init: S)(f: (S, A) => (S, B)): Col[B]

Is not as common as mapWithIndex but is kind of handy and is very hard to implement generically without much boilerplate.

@benhutchison
Copy link

benhutchison commented May 21, 2022 via email

@BalmungSan
Copy link
Contributor Author

Id do this with a fold normally

The thing is that now you have to construct the new collection by yourself which is redundant and error-prone but also it may be more inefficient than the underlying map

And IMO the final state value needs to be exposed

Yeah, that is a good idea.

PS: For the record, fs2 has this function, is called mapAccumulate
And I am creating a PR to add it to cats.

@He-Pin
Copy link

He-Pin commented Sep 2, 2023

mapAccululate or mapAccumulation or statefulMap

@sageserpent-open
Copy link

Just mentioned this here: https://users.scala-lang.org/t/idiomatic-map-combined-with-fold/9628, which lead me here. Had the need for this on several occasions.

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

4 participants