Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -12,3 +12,15 @@ function Y(f) {
|
|||
}));
|
||||
return g;
|
||||
}
|
||||
|
||||
var fac = Y(function(f) {
|
||||
return function (n) {
|
||||
return n > 1 ? n * f(n - 1) : 1;
|
||||
};
|
||||
});
|
||||
|
||||
var fib = Y(function(f) {
|
||||
return function(n) {
|
||||
return n > 1 ? f(n - 1) + f(n - 2) : n;
|
||||
};
|
||||
});
|
||||
|
|
|
|||
5
Task/Y-combinator/JavaScript/y-combinator-5.js
Normal file
5
Task/Y-combinator/JavaScript/y-combinator-5.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
function Y(f) {
|
||||
return function() {
|
||||
return f(arguments.callee).apply(this, arguments);
|
||||
};
|
||||
}
|
||||
20
Task/Y-combinator/JavaScript/y-combinator-6.js
Normal file
20
Task/Y-combinator/JavaScript/y-combinator-6.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
let
|
||||
Y= // Except for the η-abstraction necessary for applicative order languages, this is the formal Y combinator.
|
||||
f=>((g=>(f((...x)=>g(g)(...x))))
|
||||
(g=>(f((...x)=>g(g)(...x))))),
|
||||
Y2= // Using β-abstraction to eliminate code repetition.
|
||||
f=>((f=>f(f))
|
||||
(g=>(f((...x)=>g(g)(...x))))),
|
||||
Y3= // Using β-abstraction to separate out the self application combinator δ.
|
||||
((δ=>f=>δ(g=>(f((...x)=>g(g)(...x)))))
|
||||
((f=>f(f)))),
|
||||
fix= // β/η-equivalent fix point combinator. Easier to convert to memoise than the Y combinator.
|
||||
(((f)=>(g)=>(h)=>(f(h)(g(h)))) // The Substitute combinator out of SKI calculus
|
||||
((f)=>(g)=>(...x)=>(f(g(g)))(...x)) // S((S(KS)K)S(S(KS)K))(KI)
|
||||
((f)=>(g)=>(...x)=>(f(g(g)))(...x))),
|
||||
fix2= // β/η-converted form of fix above into a more compact form
|
||||
f=>(f=>f(f))(g=>(...x)=>f(g(g))(...x)),
|
||||
opentailfact= // Open version of the tail call variant of the factorial function
|
||||
fact=>(n,m=1)=>n<2?m:fact(n-1,n*m);
|
||||
tailfact= // Tail call version of factorial function
|
||||
Y(parttailfact);
|
||||
9
Task/Y-combinator/JavaScript/y-combinator-7.js
Normal file
9
Task/Y-combinator/JavaScript/y-combinator-7.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
let
|
||||
polyfix= // A version that takes an array instead of multiple arguments would simply use l instead of (...l) for parameter
|
||||
(...l)=>(
|
||||
(f=>f(f))
|
||||
(g=>l.map(f=>(...x)=>f(...g(g))(...x)))),
|
||||
[even,odd]= // The new destructive assignment syntax for arrays
|
||||
polyfix(
|
||||
(even,odd)=>n=>(n===0)||odd(n-1),
|
||||
(even,odd)=>n=>(n!==0)&&even(n-1));
|
||||
Loading…
Add table
Add a link
Reference in a new issue