bodil.github.com icon indicating copy to clipboard operation
bodil.github.com copied to clipboard

Mention and_then combinator

Open pickfire opened this issue 6 years ago • 0 comments

As for https://github.com/bodil/bodil.github.com/blob/master/content/parser-combinators/index.md

It would be nice to have include

fn and_then<'a, P, F, A, B, NextP>(parser: P, f: F) -> impl Parser<'a, B>
where
    P: Parser<'a, A>,
    NextP: Parser<'a, B>,
    F: Fn(A) -> NextP,
{
    move |input| parser.parse(input)
        .and_then(|(next_input, result)| f(result).parse(next_input))
}

as an alternative to

fn and_then<'a, P, F, A, B, NextP>(parser: P, f: F) -> impl Parser<'a, B>
where
    P: Parser<'a, A>,
    NextP: Parser<'a, B>,
    F: Fn(A) -> NextP,
{
    move |input| match parser.parse(input) {
        Ok((next_input, result)) => f(result).parse(next_input),
        Err(err) => Err(err),
    }
}

Maybe it might be good to mention the previous version as well.

pickfire avatar May 06 '19 07:05 pickfire