Kotlin 1.1 Standard Libraryの改善

Kotlin 1.1 Standard Libraryの改善

2017/06/29(木)に開催された 「第6回Kotlin勉強会 @ Sansan」の発表資料です。

F46c97bb74758d481531990654933050?s=128

RyotaMurohoshi

June 29, 2017
Tweet

Transcript

  1. 14.
  2. 16.

    forEach val unit: Unit = listOf(1, 2, 3) .map {

    it * it } .forEach { println(it) } ࣮ߦ݁Ռ͸ 1 4 9
  3. 17.

    onEach val list: List<Int> = listOf(1, 2, 3) .map {

    it * it } .onEach { println(it) } ࣮ߦ݁Ռ͸ 1 4 9
  4. 19.

    val unit: Unit = listOf(1, 2, 3) .map { it

    * it } .forEach { println(it) } val list: List<Int> = listOf(1, 2, 3) .map { it * it } .onEach { println(it) }
  5. 24.

    val sequence: Sequence<Int> =sequenceOf(1, 2, 3) .onEach { println(it) }

    .map { it * it } .onEach { println(it) } println("============") for (e in sequence){ println("$e in for") }
  6. 25.
  7. 26.

    ͿͬͪΌ͚ίʔυݟͨํ͕ૣ͍Ͱ͢Ͷ @SinceKotlin("1.1") public inline fun <T, C : Iterable<T>> C.onEach(action:

    (T) -> Unit): C { return apply { for (element in this) action(element) } }
  8. 28.

    ίʔυݟͨํ͕Θ͔Γ΍͍͢ʂ 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> 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> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null
  9. 29.

    val evenOrNull: Int? = 2.takeIf { it % 2 ==

    0 } Assert.assertEquals(2, evenOrNull) val oddOrNull: Int? = 2.takeUnless { it % 2 == 0 } Assert.assertEquals(null, oddOrNull)
  10. 31.
  11. 32.

    val bundleA = Bundle().also { // ͜͜Ͱthis͸Bundleͷ֎ଆ it.putString("KEY1", "VALUE1") //

    ίϯύΠϧΤϥʔ //putString("KEY2", "VALUE2") // ίϯύΠϧΤϥʔ //this.putString("KEY3", "VALUE3") }
  12. 33.

    ίʔυݟͨํ͕Θ͔Γ΍͍͢ʂ applyͱalso /** * Calls the specified function [block] with

    `this` value as its receiver and returns `this` value. */ @kotlin.internal.InlineOnly public inline fun <T> 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> T.also(block: (T) -> Unit): T { block(this); return this }
  13. 38.

    groupBy͍ͬͯ͏ϝιου͋Γ·ͨ͠ΑͶʁ data class ScoreRecord( val stageId: Int, val score: Int

    ) val scoreRecordList = listOf( ScoreRecord(stageId = 0, score = 1), ScoreRecord(stageId = 0, score = 3), ScoreRecord(stageId = 1, score = 2), ScoreRecord(stageId = 0, score = 1), ScoreRecord(stageId = 2, score = 4) ) val map: Map<Int, List<ScoreRecord>> = scoreRecordList.groupBy { it.stageId }
  14. 40.

    val sumScores: Map<Int, Int> = scoreRecordList .groupingBy { it.stageId }

    .fold(0, { acc, elm -> acc + elm.score }) val maxScores: Map<Int, Int> = scoreRecordList .groupingBy { it.stageId } .fold(0, { acc, elm -> maxOf(acc, elm.score) })
  15. 42.

    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)
  16. 44.

    Kotlin'1.1ͷલʹ΋ɺ+ΦϖϨʔλ͸͋ͬͨ val map = mapOf(1 to "A", 2 to "B")

    // Kotlin 1.0Ͱ΋ // +ΦϖϨʔλʔͰཁૉΛ௥Ճͨ͠৽ͨͳMapΛੜ੒Ͱ͖ͨ val added = map + (3 to "C")
  17. 45.

    Kotlin'1.1͔Βɺ*ΦϖϨʔλ͕ՃΘͬͨ val map = mapOf(1 to "A", 2 to "B")

    // Kotlin 1.1͔Β // -ΦϖϨʔλʔͰࢦఆͨ͠ΩʔͷཁૉΛ࡟আͨ͠ // ৽ͨͳMapΛੜ੒Ͱ͖ΔΑ͏ʹ val added = map - 1
  18. 50.

    ൺֱ৚݅ΛҾ਺ͱͯ͠ࢦఆ΋Ͱ͖Δ val alphabetical = minOf( "Java", "Kotlin", "Android" ) val

    length = minOf( "Java", "Kotlin", "Android", Comparator { a, b -> a.length - b.length } )
  19. 56.

    val map: Map<Int, String> = mapOf(1 to "A", 2 to

    "B") // 0ͱ͍͏Ωʔ͸ͳ͍͔ΒɺnullΛฦ͢ val valueOrNull : String? = map[0] // 0ͱ͍͏Ωʔ͸ͳ͍͔ΒɺNoSuchElementExceptionΛ౤͛Δ val value: String = map.getValue(0)
  20. 71.
  21. 72.
  22. 73.