Skip to content

Instantly share code, notes, and snippets.

@buzzdecafe
Created November 13, 2014 21:56
Show Gist options
  • Save buzzdecafe/933ac4018ec1b9f31831 to your computer and use it in GitHub Desktop.
Save buzzdecafe/933ac4018ec1b9f31831 to your computer and use it in GitHub Desktop.
function cond2() {
var cs = [].slice.call(arguments, 0, arguments.length - 1);
var ow = arguments[arguments.length - 1];
return function() {
var i = -1;
while(++i < cs.length) {
var value = cs[i].apply(this, arguments);
if (value) {
return value;
}
}
return ow.apply(this, arguments);
};
}
function ifTrue(pred, onSat) {
return function() {
return pred.apply(this, arguments) ? onSat.apply(this, arguments) : void 0;
};
}
// example:
grade = cond2(
ifTrue(R.gte(__, 90), R.always('A')),
ifTrue(R.gte(__, 80), R.always('B')),
ifTrue(R.gte(__, 70), R.always('C')),
ifTrue(R.gte(__, 60), R.always('D')),
R.always('F')
);
grade(55) // => 'F'
grade(66) // => 'D'
grade(75) // => 'C'
grade(80) // => 'B'
grade(99) // => 'A'
@fyyyyy
Copy link

fyyyyy commented Nov 13, 2014

Not sure those are really needed, but it may be more extensible in the future compared to array tuples.
Otherwise i don't see why tuples wouln't work as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment