Last active
December 3, 2017 23:39
-
-
Save aziis98/b2b5dd6921587260ef8a1002bbdd32be to your computer and use it in GitHub Desktop.
This file contains hidden or 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 otherwise = () => true; | |
| const cases = (value, branches) => { | |
| for (let i = 0; i < branches.length; i += 2) { | |
| const predicate = branches[i]; | |
| const action = branches[i + 1]; | |
| if (predicate(value)) { | |
| return action(); | |
| } | |
| } | |
| } |
This file contains hidden or 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 inRange = (a, b) => (value) => a <= value && b >= value; | |
| let a = 11; | |
| cases(a, [ | |
| inRange(5, 10), () => { | |
| console.log('5 to 10!'); | |
| }, | |
| inRange(0, 5), () => { | |
| console.log('0 to 4!'); | |
| }, | |
| otherwise, () => { | |
| console.log('Default, a = ' + a + '!'); | |
| } | |
| ]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment