Kotlin in Action - Chapter5 #4
ukhyun2402
started this conversation in
General
Replies: 1 comment
-
val r = Random(1)
val colors = mutableListOf<Color>().run {
for (i in 1..100) {
this.add(Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)))
}
this
}
println(colors) val a = colors.groupBy { it.red + it.blue + it.green }
val maxKey = a.values.maxBy { it.size }[0].run {
this.blue + this.red + this.green
}
val b = colors.groupBy { it.red + it.blue + it.green }.values.maxBy { it.size }[0].run {
this.blue + this.red + this.green
}
println(b)
println(a[maxKey])
//with
val file = File("./hello.txt")
with(file) {
val reader = BufferedReader(FileReader(file, Charset.defaultCharset()))
reader.lines().forEach { println(it) }
}
// run
val file = File("./hello.txt").run {
val reader = BufferedReader(FileReader(this, Charset.defaultCharset()))
reader.lines().forEach { println(it) }
} 따라서 위의 예제와 같이 특정 파일의 내용을 읽을 때에만 필요한 객체들을 scope내에서 실행시켜 scope가 종료될때 자동으로 객체를 없애주는 방향으로도 사용할 수 있다. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Beta Was this translation helpful? Give feedback.
All reactions