Skip to content

Instantly share code, notes, and snippets.

@aziis98
Last active December 3, 2017 23:39
Show Gist options
  • Select an option

  • Save aziis98/b2b5dd6921587260ef8a1002bbdd32be to your computer and use it in GitHub Desktop.

Select an option

Save aziis98/b2b5dd6921587260ef8a1002bbdd32be to your computer and use it in GitHub Desktop.
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();
}
}
}
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