Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -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;
};
});

View file

@ -0,0 +1,5 @@
function Y(f) {
return function() {
return f(arguments.callee).apply(this, arguments);
};
}

View 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);

View 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));