Created
February 29, 2012 01:00
-
-
Save feross/1936676 to your computer and use it in GitHub Desktop.
memoizer.js
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 count = 0; | |
var memoizer = function(memo, formula) { | |
var recur = function(n) { | |
var result = memo[n]; | |
if (typeof result !== "number") { | |
result = formula(n); | |
memo[n] = result; | |
} | |
return result; | |
}; | |
return recur; | |
}; | |
var fibonacci = memoizer([0, 1], function(n) { | |
return fibonacci(n - 1) + fibonacci(n - 2); | |
}); | |
console.log(fibonacci(10)); | |
console.log("count: " + count); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment