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 #2922

Closed

Conversation

ericpashman
Copy link
Contributor

This PR changes two characters to generalize the type of usingStateT. This permits its use with functions that arbitrarily transform the items in a stream.

Here is a motiving example. I rely on the below function as a way to write stream transformations in which each item in a stream can be transformed in a way 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 presently the type of usingStateT would overly constrain the type of 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.)

@ericpashman
Copy link
Contributor Author

Whoops, I just realized I made the change to the deprecated version of this function. Closing this: I'll make a new PR for the equivalent change in the right place.

@ericpashman ericpashman closed this Jan 6, 2025
@ericpashman ericpashman deleted the generalize-usingStateT branch January 6, 2025 04:32
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.

1 participant