Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Last active March 2, 2017 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccorcos/4545a50fddd730d35199d302c6b79b7d to your computer and use it in GitHub Desktop.
Save ccorcos/4545a50fddd730d35199d302c6b79b7d to your computer and use it in GitHub Desktop.
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