Created
April 10, 2012 02:12
-
-
Save aoi0308/2347920 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
/** | |
* 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