Created
April 9, 2012 16:21
-
-
Save aoi0308/2344525 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
/** | |
* ハッカーと画家のP.198に載ってるアキュムレータをScalaで実装。 | |
* 元ネタは Common Lisp | |
* (defun foo (n) | |
* (lambda (i) (incf n i))) | |
* | |
* Int固定じゃなくてもっと汎用的にするにはどうするんだろ。 | |
*/ | |
def accum(n: Int): (Int => Int) = { | |
var m = n | |
(i: Int) => { | |
m += i | |
m | |
} | |
} | |
val a = accum(5) | |
println(a(1)) //=> 6 | |
println(a(3)) //=> 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment