Created
April 21, 2013 00:31
-
-
Save everaldo/5427977 to your computer and use it in GitHub Desktop.
Javascript memoizer
This file contains 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 characters
var memoizer = function(memo, fundamental){ | |
var shell = function (n) { | |
var result = memo[n]; | |
if (typeof result !== 'number'){ | |
result = fundamental(shell, n); | |
memo[n] = result; | |
} | |
return result; | |
}; | |
return shell; | |
}; | |
var fibonacci = | |
memoizer([0,1], function(recur, n){ | |
return recur(n - 1) + recur(n - 2); | |
}); | |
fibonacci(40); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment