Skip to content

Instantly share code, notes, and snippets.

@aoi0308
Created April 10, 2012 02:12
Show Gist options
  • Save aoi0308/2347920 to your computer and use it in GitHub Desktop.
Save aoi0308/2347920 to your computer and use it in GitHub Desktop.
アキュムレータ(クラス使用)
/**
* https://gist.github.com/2344525 の別バージョン
* 高階関数じゃなくてクラスを使用してみる。
*/
class Accum(private var n: Int) {
def apply(i: Int): Int = { n += i; n }
}
object Accum {
def apply(n: Int): Accum = new Accum(n)
}
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