Created
June 20, 2018 09:18
-
-
Save titouancreach/4894ac13d04338159da0816fa3016cb1 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 matchString = (s, patterns) => { | |
if (patterns.length === 0) { | |
throw new Error('Not exhaustive string matching'); | |
} | |
const [matching, ...rest] = patterns; | |
const [re, fn] = matching; | |
if (re.test(s)) { | |
const matches = re.exec(s); | |
return matches ? fn(...matches) : fn(); | |
} | |
return matchString(s, rest); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment