Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin 1.1 Standard Libraryの改善

Kotlin 1.1 Standard Libraryの改善

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

RyotaMurohoshi

June 29, 2017
Tweet

More Decks by RyotaMurohoshi

Other Decks in Technology

Transcript

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

    it * it } .forEach { println(it) } ࣮ߦ݁Ռ͸ 1 4 9
  2. onEach val list: List<Int> = listOf(1, 2, 3) .map {

    it * it } .onEach { println(it) } ࣮ߦ݁Ռ͸ 1 4 9
  3. 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) }
  4. 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") }
  5. ͿͬͪΌ͚ίʔυݟͨํ͕ૣ͍Ͱ͢Ͷ @SinceKotlin("1.1") public inline fun <T, C : Iterable<T>> C.onEach(action:

    (T) -> Unit): C { return apply { for (element in this) action(element) } }
  6. ίʔυݟͨํ͕Θ͔Γ΍͍͢ʂ 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
  7. val evenOrNull: Int? = 2.takeIf { it % 2 ==

    0 } Assert.assertEquals(2, evenOrNull) val oddOrNull: Int? = 2.takeUnless { it % 2 == 0 } Assert.assertEquals(null, oddOrNull)
  8. val bundleA = Bundle().also { // ͜͜Ͱthis͸Bundleͷ֎ଆ it.putString("KEY1", "VALUE1") //

    ίϯύΠϧΤϥʔ //putString("KEY2", "VALUE2") // ίϯύΠϧΤϥʔ //this.putString("KEY3", "VALUE3") }
  9. ίʔυݟͨํ͕Θ͔Γ΍͍͢ʂ 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 }
  10. 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 }
  11. 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) })
  12. 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)
  13. Kotlin'1.1ͷલʹ΋ɺ+ΦϖϨʔλ͸͋ͬͨ val map = mapOf(1 to "A", 2 to "B")

    // Kotlin 1.0Ͱ΋ // +ΦϖϨʔλʔͰཁૉΛ௥Ճͨ͠৽ͨͳMapΛੜ੒Ͱ͖ͨ val added = map + (3 to "C")
  14. Kotlin'1.1͔Βɺ*ΦϖϨʔλ͕ՃΘͬͨ val map = mapOf(1 to "A", 2 to "B")

    // Kotlin 1.1͔Β // -ΦϖϨʔλʔͰࢦఆͨ͠ΩʔͷཁૉΛ࡟আͨ͠ // ৽ͨͳMapΛੜ੒Ͱ͖ΔΑ͏ʹ val added = map - 1
  15. ൺֱ৚݅ΛҾ਺ͱͯ͠ࢦఆ΋Ͱ͖Δ val alphabetical = minOf( "Java", "Kotlin", "Android" ) val

    length = minOf( "Java", "Kotlin", "Android", Comparator { a, b -> a.length - b.length } )
  16. 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)