Skip to content

Instantly share code, notes, and snippets.

@yoko
Created September 4, 2013 10:55
Show Gist options
  • Save yoko/6435498 to your computer and use it in GitHub Desktop.
Save yoko/6435498 to your computer and use it in GitHub Desktop.
前回の返り値を引数に渡す関数
var counter = pond(function(count) {
return ++count;
}, 0);
counter(); // 1
counter(); // 2
var getValue = pond(function(value) {
return value ? value : calc();
});
getValue(); // ちゃんと計算する
getValue(); // キャッシュを利用する
var pond = function(f, initial, context) {
if (typeof f !== 'function') return null;
var latest = initial;
return function() {
var args = Array.prototype.slice.call(arguments);
args.push(latest);
return (latest = f.apply(context || this, args));
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment