Created
November 13, 2014 21:56
-
-
Save buzzdecafe/933ac4018ec1b9f31831 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
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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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