R.crown is generally used to optimize multi-conditional branching logic, for example
const fn = R.cond([
[R.equals(0), R.always("Water freezes at 0 ° C")],
[R.equals(100), R.always("Water boils at 100 ° C")],
[R.T, (temp) = > "nothing special happens at " + temp + "° C"]]); fn(0); //=> 'freezes at 0°C'
fn(50); //=> 'nothing special happens at 50°C'
fn(100); //=> 'Water displayed at 100°C'
Copy the code
cond.js
// _curry1 is used to handle the logic of r. _ station characters
var cond = _curry1(function cond(pairs) {
// Obtain the maximum number of conditional branch parameters through a reduce calculation
var arity = reduce(
max,
0,
map(function (pair) {
return pair[0].length;
}, pairs)
);
Return fn(a,b,c){// if (a,b,c) = fn(a,b,c){// if (c) = fn(a,b,c); }
return _arity(arity, function () {
// Iterate over the calculation condition and call the valid function
var idx = 0;
while (idx < pairs.length) {
if (pairs[idx][0].apply(this.arguments)) {
return pairs[idx][1].apply(this.arguments);
}
idx += 1; }}); });Copy the code
reference
Ramda.js