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

Contra-flattening a Fold #2734

Open
rihardsk opened this issue Mar 16, 2024 · 2 comments
Open

Contra-flattening a Fold #2734

rihardsk opened this issue Mar 16, 2024 · 2 comments

Comments

@rihardsk
Copy link

I hope I'm using the term contravariant here correctly. Basically I want to flatten the inputs of a fold. I.e., I need a function with the following type:

contraFlatten :: Fold m a b -> Fold m [a] b
-- or maybe something more general is possible? e.g.,
-- contraFlatten :: Foldable f => Fold m a b -> Fold m (f a) b

I looked through streamly-core and nothing like this seems to exist?

@harendra-kumar
Copy link
Member

harendra-kumar commented Mar 17, 2024

You can try unfoldMany internal API. See https://streamly.composewell.com/haddocks/streamly-core-0.2.2/Streamly-Internal-Data-Fold.html#v:unfoldMany . You can pass the Unfold.fromList to it to unfold a list.

The unfoldMany implementation does not fuse though, so if fusion/efficiency is important you cannot use it. Fusing such operation would require changing the type.

@rihardsk
Copy link
Author

Thank you!

Maybe it's worth to consider adding something like this as an example?

contraFlatten :: Monad m => Fold m a b -> Fold m [a] b
contraFlatten = unfoldMany fromList

It wasn't obvious to me at first how to use unfoldMany to get what I want.

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