Created
September 4, 2013 10:55
-
-
Save yoko/6435498 to your computer and use it in GitHub Desktop.
前回の返り値を引数に渡す関数
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 counter = pond(function(count) { | |
return ++count; | |
}, 0); | |
counter(); // 1 | |
counter(); // 2 | |
var getValue = pond(function(value) { | |
return value ? value : calc(); | |
}); | |
getValue(); // ちゃんと計算する | |
getValue(); // キャッシュを利用する |
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 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