Last active
June 18, 2021 01:46
-
-
Save logicmason/0722b5b159a45f7a81b6 to your computer and use it in GitHub Desktop.
Revisions
-
logicmason renamed this gist
Dec 6, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
logicmason revised this gist
Dec 6, 2014 . 2 changed files with 43 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,19 @@ var factgen = function(fact) { }; }; var fibgen = function(fib) { // this naive solution has exponential runtime complexity return function(n) { return (n <= 2) ? 1 : fib(n-1) + fib(n-2); }; }; console.log( Y(factgen)(5) ); // returns 120, i.e., 1 * 2 * 3 * 4 * 5 console.log( Y(fibgen)(7) ); // returns 13, i.e., the 7th fibonacci number var factorial = Y(factgen); // built entirely with anonymous functions var fibonacci = Y(fibgen); console.log( factorial(6) ); // 120 console.log([1,2,3,4,5,6,7,8,9,10].map( fibonacci) ); // the first 10 fibonacci numbers console.log( fibonacci(35) ); // uh oh, already getting kind of slow due to poor algorithm This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ var fibgen = function(fib) { // this naive solution has exponential runtime complexity return function(n) { return (n <= 2) ? 1 : fib(n-1) + fib(n-2); }; }; var Ymem = function(proc) { // this Y combinator-like function caches the results of earlier calculations var results = {}; return (function(x) { return proc(function(y) { if (results[y] === undefined) { results[y] = (x(x))(y); } return results[y]; }); })(function(x) { return proc(function(y) { if (results[y] === undefined) { results[y] = (x(x))(y); } return results[y]; }); }); } // Using Ymem to cache results, fibgen no longer has exponential runtime complexity var fibonacci = Ymem(fibgen); console.log([1,2,3,4,5,6,7,8,9,10].map( fibonacci) ); // the first 10 fibonacci numbers console.log( fibonacci(35) ); // quickly calculates solution of 9227465 console.log( fibonacci(120) ); // still calculates solution almost instantly -
logicmason revised this gist
Dec 6, 2014 . 1 changed file with 7 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,17 @@ var Y = function(proc) { return (function(x) { return proc(function(y) { return (x(x))(y);}); })(function(x) { return proc(function(y) { return (x(x))(y);}); }); }; var factgen = function(fact) { return function(n) { return (n === 0) ? 1 : n * fact(n-1); // calls argument, not self }; }; console.log( Y(factgen)(5) ); // returns 120, i.e. 1 * 2 * 3 * 4 * 5 var factorial = Y(factgen); // built entirely with anonymous functions -
logicmason created this gist
Dec 4, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ var Y = function (proc) { return (function (x) { return proc(function (y) { return (x(x))(y);}); })(function (x) { return proc(function (y) { return (x(x))(y);}); }); }; var factgen = function (fact) { return function(n) { return (n === 0) ? 1 : n * fact(n-1); // calls argument, not self }; }; console.log( Y(factgen)(4) ); console.log( Y(factgen)(6) ); var factorial = Y(factgen); // built entirely with anonymous functions