Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created March 2, 2017 04:36
Show Gist options
  • Save ccorcos/547854f70cb688ffdeaa6b75fd289981 to your computer and use it in GitHub Desktop.
Save ccorcos/547854f70cb688ffdeaa6b75fd289981 to your computer and use it in GitHub Desktop.
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