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

Monadic parseOnly #95

Closed
zudov opened this issue Feb 27, 2015 · 2 comments
Closed

Monadic parseOnly #95

zudov opened this issue Feb 27, 2015 · 2 comments

Comments

@zudov
Copy link

zudov commented Feb 27, 2015

Often it's necessary to run a parser inside another parser. It's usually done with something like this:

captureAndParse = do
    raw <- capture
    case parseOnly parse raw where
        Left err -> fail err
        Right res -> return res 

It would be nice to have a function parseOnlyM which runs a parser and calls fail on Left result. It can be implemented in this way:

parseOnlyM :: Monad m => Parser a -> Text -> m a
parseOnlyM parser input = either fail return $ parseOnly parser input

Which would allow to simplify the first example to following:

captureAndParse = capture >>= parseOnlyM parse

What do you think about such addition? I would be happy to make a PR.

@solatis
Copy link

solatis commented Mar 16, 2015

Perhaps what we're really looking for here is a combinator that wraps this ? It looks like a pattern that should be captured (pun intended :)) on a higher level.

@bos
Copy link
Collaborator

bos commented Mar 28, 2015

fail is definitely not the right function to be using here, sorry.

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

3 participants