Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created March 2, 2017 04:37
Show Gist options
  • Save ccorcos/fec8ef226dfca6afad7afdfee0d5163b to your computer and use it in GitHub Desktop.
Save ccorcos/fec8ef226dfca6afad7afdfee0d5163b to your computer and use it in GitHub Desktop.
class Parser {
constructor(parse) {
this.parse = parse
}
run(iterable) {
if (iterable instanceof Stream) {
return this.parse(iterable)
} else {
return this.parse(new Stream(iterable))
}
}
map(f) {
return new Parser(stream => this.parse(stream).map(f))
}
bimap(s, f) {
return new Parser(stream => this.parse(stream).bimap(s, f))
}
chain(f) {
return new Parser(stream =>
this.parse(stream).chain((v, s) => f(v).run(s)))
}
fold(s, f) {
return new Parser(stream => this.parse(stream).fold(s, f))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment