2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,23 +1,34 @@
|
|||
let
|
||||
fix = // This is a variant of the Applicative order Y combinator
|
||||
f => (f => f(f))(g => f((...a) => g(g)(...a))),
|
||||
curry =
|
||||
f => (
|
||||
fix(
|
||||
z => (n,...a) => (
|
||||
n>0
|
||||
?b => z(n-1,...a,b)
|
||||
:f(...a)))
|
||||
(f.length)),
|
||||
curryrest =
|
||||
f => (
|
||||
fix(
|
||||
z => (n,...a) => (
|
||||
n>0
|
||||
?b => z(n-1,...a,b)
|
||||
:(...b) => f(...a,...b)))
|
||||
(f.length)),
|
||||
curriedmax=curry(Math.max),
|
||||
curryrestedmax=curryrest(Math.max);
|
||||
print(curriedmax(8)(4),curryrestedmax(8)(4)(),curryrestedmax(8)(4)(9,7,2));
|
||||
// 8,8,9
|
||||
(function () {
|
||||
|
||||
// (arbitrary arity to fully curried)
|
||||
// extraCurry :: Function -> Function
|
||||
function extraCurry(f) {
|
||||
|
||||
// Recursive currying
|
||||
function _curry(xs) {
|
||||
return xs.length >= intArgs ? (
|
||||
f.apply(null, xs)
|
||||
) : function () {
|
||||
return _curry(xs.concat([].slice.apply(arguments)));
|
||||
};
|
||||
}
|
||||
|
||||
var intArgs = f.length;
|
||||
|
||||
return _curry([].slice.call(arguments, 1));
|
||||
}
|
||||
|
||||
|
||||
// TEST
|
||||
|
||||
// product3:: Num -> Num -> Num -> Num
|
||||
function product3(a, b, c) {
|
||||
return a * b * c;
|
||||
}
|
||||
|
||||
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
.map(extraCurry(product3)(7)(2))
|
||||
|
||||
// [14, 28, 42, 56, 70, 84, 98, 112, 126, 140]
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue