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

Generalize type signature of usingStateT #2923

Merged
merged 1 commit into from
Jan 6, 2025

Conversation

ericpashman
Copy link
Contributor

This PR changes two characters to generalize the type of usingStateT, making it possible to use it with functions that arbitrarily transform the items in a stream.

Here's an example motivating the need for this change. I rely on the below function to transform streams in a manner in which each item in a stream can be mapped to a new value that depends on an arbitrary state (which is typically built up from previous items in the stream):

import qualified Streamly.Data.Stream.Prelude  as S
import qualified Streamly.Internal.Data.Stream as S

stateMap
  :: Monad m => (a -> StateT s m (Maybe b)) -> s -> Stream m a -> Stream m b
stateMap f s = S.catMaybes . S.evalStateT (return s) . S.mapM f . S.liftInner

It should be possible to refactor this function as stateMap f s = S.catMaybes . S.usingStateT (return s) (S.mapM f), but the present type of usingStateT overly constrains the type of the argument f.

(I'll also take this opportunity to ask: Is there is an established approach to doing the sort of thing I wrote stateMap for? I couldn't find any such pre-packaged functionality in the library.)

@harendra-kumar
Copy link
Member

LGTM. Thanks for the PR.

There is no prepackaged functionality for this but scanMaybe may be relevant for your use case.

@harendra-kumar harendra-kumar merged commit b48885e into composewell:master Jan 6, 2025
19 of 21 checks passed
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

Successfully merging this pull request may close these issues.

2 participants