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

FEATURE REQUEST: Create "lazy" flatten() alternative that respects map(), pick(n), etc. #188

Open
rsevero opened this issue Jan 6, 2025 · 1 comment

Comments

@rsevero
Copy link

rsevero commented Jan 6, 2025

It would be great to have a "lazy" alternative to flatten() that respects map(), pick(n) etc.

I don´t know if there is something that would only work with this new feature but some intuitive options would "work as expected" like:

  Parser bracketStringTemplate(Parser content) =>
      (char('[') & content & char(']')).pick(1).lazyFlatten();
  Parser bracketStringGeneral() => bracketStringTemplate(noneOf('[]').star());

or

  Parser bracketStringTemplate(Parser content) =>
      content.skip(before: char('['), after:  char(']')).lazyFlatten();
  Parser bracketStringGeneral() => bracketStringTemplate(noneOf('[]').star());

or even

  Parser bracketStringTemplate(Parser content) =>
      content.skip(before: char('['), after:  char(']'));
  Parser bracketStringGeneral() => bracketStringTemplate(noneOf('[]').star()).lazyFlatten();

which must currently be rewritten to:

  Parser bracketStringTemplate(Parser content) =>
      (char('[') & content.flatten() & char(']')).pick(1);
  Parser bracketStringGeneral() => bracketStringTemplate(noneOf('[]').star());
@renggli
Copy link
Member

renggli commented Jan 6, 2025

Thank you for the suggestion, but not clear how that could be implemented in a general way?

I typically create explicit helper functions that create more complicated compositions of common parser patterns, much like in your last example.

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

No branches or pull requests

2 participants