Last active
March 2, 2017 04:40
-
-
Save ccorcos/4545a50fddd730d35199d302c6b79b7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const always = value => | |
new Parser(stream => new Success(value, stream)) | |
const never = value => | |
new Parser(stream => new Failure(value, stream)) | |
const append = (p1, p2) => | |
p1.chain(vs => p2.map(v => vs.concat([v]))) | |
const concat = (p1, p2) => | |
p1.chain(xs => p2.map(ys => xs.concat(ys))) | |
const sequence = list => | |
list.reduce((acc, parser) => append(acc, parser), always([])) | |
sequence([char('a'), char('b'), char('c')]) | |
.run('abc') | |
.fold( | |
v => console.log('success', v) | |
e => console.log('error', e) | |
) | |
// => success ['a', 'b', 'c'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment