Add useful binary operators to Curry, move it under module Fun and write a general curryfication function

This commit is contained in:
Tissevert 2021-01-07 21:27:37 +01:00
parent 3b69884615
commit 80355cd86d
2 changed files with 124 additions and 47 deletions

View File

@ -1,47 +0,0 @@
return {
curry: curry,
flip: flip,
uncurry: uncurry,
plus: plus,
minus: minus,
substractTo: substractTo,
times: times,
dividedBy: dividedBy,
divide: divide
};
function plus(a) {
return function(b) {
return a+b;
}
}
function minus(a) {
return function(b) {
return b-a; // reversed to match the expected infix feel : «minus(4)» should substract 4 to whatever's passed to it, not the other way round — which is implemented by substractTo
}
}
function substractTo(a) {
return function(b) {
return a-b;
}
}
function times(a) {
return function(b) {
return a*b;
}
}
function dividedBy(a) {
return function(b) {
return b/a; // same natural order as for minus — the other way round equivalent is divide
}
}
function divide(a) {
return function(b) {
return a/b;
}
}

124
src/UnitJS/Fun/Curry.js Normal file
View File

@ -0,0 +1,124 @@
return {
curry: curry,
flip: flip,
plus: plus,
minus: minus,
substractTo: substractTo,
times: times,
dividedBy: dividedBy,
divide: divide,
gt: gt,
ge: ge,
eq: eq,
neq: neq,
teq: teq,
tneq: tneq,
lt: lt,
le: le
};
function curry(f, n) {
n = n == undefined ? f.length-1 : n;
if(n == 0) {
return f;
} else {
return function(x) {
return curry(function() {
var args = Array.prototype.slice.call(arguments);
return f.apply(null, [].concat(x, args));
}, n-1);
};
}
}
function flip(f) {
return function(a) {
return function(b) {
return f(b)(a);
}
}
}
function plus(a) {
return function(b) {
return a+b;
}
}
function minus(a) {
return function(b) {
return b-a; // reversed to match the expected infix feel : «minus(4)» should substract 4 to whatever's passed to it, not the other way round — which is implemented by substractTo
}
}
function substractTo(a) {
return function(b) {
return a-b;
}
}
function times(a) {
return function(b) {
return a*b;
}
}
function dividedBy(a) {
return function(b) {
return b/a; // same natural order as for minus — the other way round equivalent is divide
}
}
function divide(a) {
return function(b) {
return a/b;
}
}
function gt(a) {
return function(b) {
return b > a;
};
}
function ge(a) {
return function(b) {
return b >= a;
};
}
function eq(a) {
return function(b) {
return b == a;
};
}
function neq(a) {
return function(b) {
return b != a;
};
}
function teq(a) {
return function(b) {
return b === a;
};
}
function tneq(a) {
return function(b) {
return b !== a;
};
}
function lt(a) {
return function(b) {
return b < a;
};
}
function le(a) {
return function(b) {
return b <= a;
};
}