Skip to content

Instantly share code, notes, and snippets.

@aoi0308
Created April 9, 2012 16:21
Show Gist options
  • Save aoi0308/2344525 to your computer and use it in GitHub Desktop.
Save aoi0308/2344525 to your computer and use it in GitHub Desktop.
アキュムレータ
/**
* ハッカーと画家の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