Created
March 2, 2017 04:36
-
-
Save ccorcos/547854f70cb688ffdeaa6b75fd289981 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 either = parsers => input => { | |
for (var i = 0; i < parsers.length; i++) { | |
const parser = parsers[i] | |
const {success, rest} = parser(input) | |
if (success) { | |
return {success, rest} | |
} | |
} | |
return {success: false, rest: input} | |
} | |
either([string('abc'), string('abab')])('abcd') | |
// => { success: true, rest: 'd' } | |
either([string('abc'), string('abab')])('ababab') | |
// => { success: true, rest: 'ab' } | |
either([string('abc'), string('abab')])('aba') | |
// => { success: false, rest: 'aba' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment