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

add conditional error combinator #29

Open
mattfenwick opened this issue Sep 19, 2016 · 0 comments
Open

add conditional error combinator #29

mattfenwick opened this issue Sep 19, 2016 · 0 comments

Comments

@mattfenwick
Copy link
Owner

mattfenwick commented Sep 19, 2016

Goal: fail to an error based on checking a parsed value using a predicate.

Something like:

// (a -> Bool) -> Parser e s (m t) a -> e -> Parser e s (m t) a
function checkError(predicate, parser, errorValue) {
  checkFunction('checkError', predicate);
  checkParser('checkError', parser);
  return bind(parser, (value) => predicate(value) ? pure(value) : error(errorValue));
  // or:
  return bind(parser, (value) => (predicate(value) ? pure : error)(value));
}

Needs corner cases to be hammered out and a better name, of course.

Cases:

  • parser fails -> fail
  • parser errors -> error
  • parser succeeds
    • predicate passes -> success
    • predicate fails -> error

Another implementation possibility, which doesn't work because it doesn't pass the test case for "parser fails -> fail":

// (a -> Bool) -> Parser e s (m t) a -> e -> Parser e s (m t) a
function checkError(predicate, parser, e) {
    return alt([check(predicate, parser), error(e)]);
}
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

1 participant