fun toJSON(collection: Collection): String { val sb = StringBuilder() sb.append("[") val iterator = collection.iterator() while (iterator.hasNext()) { val element = iterator.next() sb.append(element) if (iterator.hasNext()) { sb.append(", ") } } sb.append("]") return sb.toString() }
είʔϓؔୡ(ൈਮ) public inline fun T.run(block: T.() -> R): R = block() public inline fun with(receiver: T, block: T.() -> R): R = receiver.block() public inline fun T.apply(block: T.() -> Unit): T { block(); return this } public inline fun T.also(block: (T) -> Unit): T { block(this); return this } public inline fun T.let(block: (T) -> R): R = block(this)
είʔϓؔ(run) public inline fun T.run(block: T.() -> R): R = block() val s = "hoge".run { toUpperCase() } println(s) //=> HOGE ϨγʔόΦϒδΣΫτʹthisͰΞΫηεՄೳ ʢলུ͍ͯ͠Δʣ
είʔϓؔ(with) public inline fun with(receiver: T, block: T.() -> R): R = receiver.block() val s = with("hoge") { this.toUpperCase() } println(s) //=> HOGE
είʔϓؔ(with) public inline fun with(receiver: T, block: T.() -> R): R = receiver.block() val s = with("hoge") { toUpperCase() } println(s) //=> HOGE ֦ுؔͰͳ͍
είʔϓؔ(with) public inline fun with(receiver: T, block: T.() -> R): R = receiver.block() val s = with("hoge") { toUpperCase() } println(s) //=> HOGE ϨγʔόΦϒδΣΫτʹthisͰΞΫηεՄೳ ʢলུ͍ͯ͠Δʣ
είʔϓؔ(apply) public inline fun T.apply(block: T.() -> Unit): T { block(); return this } val s = "hoge".apply { toUpperCase() } println(s) //=> hoge Γthis ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ
είʔϓؔ(apply) public inline fun T.apply(block: T.() -> Unit): T { block(); return this } val s = "hoge".apply { toUpperCase() } println(s) //=> hoge toUpperCase()Λ͍ͯ͠Δ͕ɺ “hoge” ͕ฦ͞ΕΔͷͰҙ
είʔϓؔ(also) public inline fun T.also(block: (T) -> Unit): T { block(this); return this } val s = "hoge".also { it.toUpperCase() } println(s) //=> hoge Γthis ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ
είʔϓؔ(also) public inline fun T.also(block: (T) -> Unit): T { block(this); return this } val s = "hoge".also { it.toUpperCase() } println(s) //=> hoge Ҿͱͯ͠T͕ͬͯ͘ΔͨΊɺ itͰΞΫηεՄೳ
fun toJSON(collection: Collection): String { val sb = StringBuilder() sb.append("[") val iterator = collection.iterator() while (iterator.hasNext()) { val element = iterator.next() sb.append(element) if (iterator.hasNext()) { sb.append(", ") } } sb.append("]") return sb.toString() }