val sequence: Sequence =sequenceOf(1, 2, 3) .onEach { println(it) } .map { it * it } .onEach { println(it) } println("============") for (e in sequence){ println("$e in for") }
ίʔυݟͨํ͕Θ͔Γ͍͢ʂ takeIfͱtakeUnless /** * Returns `this` value if it satisfies the given [predicate] or `null`, if it doesn't. */ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null /** * Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does. */ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null
ίʔυݟͨํ͕Θ͔Γ͍͢ʂ applyͱalso /** * Calls the specified function [block] with `this` value as its receiver and returns `this` value. */ @kotlin.internal.InlineOnly public inline fun T.apply(block: T.() -> Unit): T { block(); return this } /** * Calls the specified function [block] with `this` value as its argument and returns `this` value. */ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.also(block: (T) -> Unit): T { block(this); return this }
toMap()(ͱ(toMutableMap() MapͷίϐʔΠϯελϯεੜϝιου val original = mutableMapOf(1 to "A", 2 to "B") val copied = original.toMap() original[3] = "C" Assert.assertEquals(3, original.size) Assert.assertEquals(2, copied.size)
val map: Map = mapOf(1 to "A", 2 to "B") // 0ͱ͍͏Ωʔͳ͍͔ΒɺnullΛฦ͢ val valueOrNull : String? = map[0] // 0ͱ͍͏Ωʔͳ͍͔ΒɺNoSuchElementExceptionΛ͛Δ val value: String = map.getValue(0)