Skip to content

Instantly share code, notes, and snippets.

@everaldo
Created April 21, 2013 00:31
Show Gist options
  • Save everaldo/5427977 to your computer and use it in GitHub Desktop.
Save everaldo/5427977 to your computer and use it in GitHub Desktop.
Javascript memoizer
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