Created
March 2, 2017 04:37
-
-
Save ccorcos/fec8ef226dfca6afad7afdfee0d5163b 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
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