-
Notifications
You must be signed in to change notification settings - Fork 18
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 atEndOfInput #27
base: main
Are you sure you want to change the base?
Add atEndOfInput #27
Conversation
What I mean by saying that this is compatible with forall x. StateT (Producer a m x) m r And this function is StateT (Producer a m r) m (Maybe r) That is, the only difference is that the return type is constrained to be the same as the one from the underlying |
Would you be okay with generalizing this to a variation on |
That would be probably more useful, yes. I still won't fit in the What should be the name of this function? I think that ideally I would like if returning an |
So I was studying your code more closely and it seems that you use |
Wait, never mind. I realized that neither |
I've changed it to I understand what you say about done :: Proxy a' a b' b m r -> Maybe r
done (Pure r) = Just r
done _ = Nothing The hard problem is finding a nice name for that function :) |
Just to be clear: I'm satisfied with just using |
And thanks for the hint :) |
So peek :: Monad m => Producer a m r -> m (Either r (a, Producer a m r))
peek p = do
x <- next p
return $ case x of
Left r -> Left r
Right (a, p') -> Right (a, yield a >> p') The reason the result has to be wrapped in the base monad is that you have to go through done (return r) /= done (lift (return r)) Also, since it goes through |
I agree with your general concerns about the monad transformer laws. In your -- | 'peek' returns 'Left r' if the given `Producer` is depleted,
-- otherwise a 'Producer' guaranteed to be not-depleted.
peek :: Monad m => Producer a m r -> m (Either r (Producer a m r))
peek p = do
x <- next p
return $ case x of
Left r -> Left r
Right (a, p') -> Right (yield a >> p') Though probably the name “peek” is not the best for this function. |
Actually, I think your |
Alright, then I'll include something like my original
Feel free to suggest any other names, too. Also, I will reopen this so that I don't forget to address this. |
Other naming ideas : |
I've written this function (and variants of it) a couple of times already. In fact all of
pipes-aeson
,pipes-binary
andpipes-attoparsec
include their own version of this function (see here for example https://github.com/k0001/pipes-attoparsec/blob/master/src/Pipes/Attoparsec.hs#L177)At first I hesitated about whether it was a nice idea to include this function or not as it doesn't fit in the
Parser
type, but it is really useful to have it at hand, and it is compatible with theParser
type after all.If you agree to include this, then I'll submit similar tools to
pipes-bytestring
andpipes-text
(which ignore trailing emptyText
/ByteString
.