Skip to content

Add many1Till to Combinators #30

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

Merged
merged 2 commits into from
Mar 13, 2017
Merged

Conversation

newlandsvalley
Copy link
Contributor

No description provided.

many1Till :: forall a end. Parser a -> Parser end -> Parser (List a)
many1Till p end = do
x <- p
xs <- manyTill p end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we can also write manyTill p end = (end *> pure Nil) <|> many1Till p end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but - forgive my ignorance - surely we can't mutually define manyTill and many1Till each in terms of the other can we?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can, because many1Till only uses manyTill after p, which means the recursive call is guarded by a function (implicit in the do notation block).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - I think I see what you mean - I'd never have spotted it. I'll try it out - it certainly simplifies manyTill.

@paf31 paf31 merged commit 5905939 into purescript-contrib:master Mar 13, 2017
@paf31
Copy link
Contributor

paf31 commented Mar 13, 2017

Looks great, thanks!

@newlandsvalley
Copy link
Contributor Author

Thanks. I've just become a bit concerned by this, though:

  let
    target = (joinWith "" $ replicate 100000 "a") <> "b"
  assert' "manyTill should not blow the stack" $ canParse (manyTill (string "a") (string "b")) (target)

blows the call stack.

@paf31
Copy link
Contributor

paf31 commented Mar 13, 2017

Yes, that's a general issue right now. It's not just this function.

@newlandsvalley
Copy link
Contributor Author

Oh - that's good news. It's just that I saw similar tests in place for many.

@paf31
Copy link
Contributor

paf31 commented Mar 13, 2017

Well, many uses Data.List.manyRec, which is stack-safe, but that's a special case. We could try to write these two functions using MonadRec, it might be an interesting exercise.

@newlandsvalley
Copy link
Contributor Author

Hmm - looks like I need to do some more learning. Thanks again for all your help and advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants