Skip to content

Instantly share code, notes, and snippets.

@jasonlvhit
Created March 1, 2014 09:46
Show Gist options
  • Save jasonlvhit/9287651 to your computer and use it in GitHub Desktop.
Save jasonlvhit/9287651 to your computer and use it in GitHub Desktop.
//JavaScript : The Good Parts
//闭包,存储数据
var memoizer = function (memo, formula) {
var recur = function(n){
var result = memo[n];
if (typeof result !== 'number') {
result = formula(recur, n);
memo[n] = result;
};
return result;
}
return recur;
};
//费波纳契数列
var fibonacci = memoizer([0,1], function(recur, n){
return recur(n - 1) + recur(n -2);
});
//阶乘
var factorial = memoizer([1,1], function(recur, n){
return n * recur(n - 1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment