Skip to content

Instantly share code, notes, and snippets.

@tanishiking
Last active December 24, 2015 18:50
Show Gist options
  • Save tanishiking/dcfdc8e08596fc99e4d1 to your computer and use it in GitHub Desktop.
Save tanishiking/dcfdc8e08596fc99e4d1 to your computer and use it in GitHub Desktop.
object MyGenerator {
def main(args: Array[String]) = {
val g = myGenerator()
println(g()) // 1
println(g()) // 2
println(g()) // 3
}
private[this] def myGenerator(): (() => Int) = {
var num = 0 // 局所変数
() => { // 評価される関数
num += 1
num
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment