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」の発表資料です。

F46c97bb74758d481531990654933050?s=128

RyotaMurohoshi

June 29, 2017
Tweet

Transcript

  1. Kotlin'1.1'Standard'Libraryͷվળ @RyotaMurohoshi 2017/06/29(ୈ6ճ(Kotlinษڧձ(@SanSan

  2. ॕ!Early!access!program!for!Kotlin!1.2!։࢝ʂ h"ps:/ /blog.jetbrains.com/kotlin/2017/06/early<access<program<for<kotlin<1<2<has<been<started/

  3. ͚ΕͲࠓ೔͸ɺKotlin'1.1ͷ࿩Λ͠·͢

  4. Kotlin'1.1ͱݴ͑͹ɽɽɽ

  5. Kotlin'1.1ͱݴ͑͹ • Corou&nes(experimental) • Type5aliases • Bound5callable5references • Sealed5and5data5classes ͳͲͳͲɽɽɽ

  6. ͦ͏͍͏ݴޠػೳ͚ͩͰͳ͘ɺ Standard'Libraryʹ΋͍Ζ͍Ζ௥Ճ͞Ε͍ͯ·͢

  7. What's'New'in'Kotlin'1.1 Standard'library'ΑΓ h"ps:/ /kotlinlang.org/docs/reference/whatsnew11.html#standard9library

  8. String'to'number'conversions

  9. val numString : String = loadString() // ΋͔ͨ͠͠ΒParseࣦഊͯ͠ // NumberFormatException͕౤͛ΒΕΔ͔΋

    val num: Int = numString.toInt()
  10. ͦ͜ͰɺtoIntOrNull

  11. val numString : String = loadString() // Parseࣦഊͨ͠Βnull val num:Int?

    = numString.toIntOrNull()
  12. // ؀ڥม਺Λύʔε͢Δ͚Ͳɺ // ࣦഊͨ͠Β80൪ʹ val port = System.getenv("PORT")?.toIntOrNull() ?: 80

  13. • toDoubleOrNull • toFloatOrNull • toShortOrNull • toLongOrNull • toByteOrNull

    ੔਺͸ج਺Λࢦఆ͢ΔΦʔόʔϩʔυ΋͋ΔΑ
  14. onEach()

  15. onEach() ֤छίϨΫγϣϯͷ֦ுؔ਺ forEach͡Όͳͯ͘ɺonEachͰ͢ʂ

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

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

    it * it } .onEach { println(it) } ࣮ߦ݁Ռ͸ 1 4 9
  18. ࣅ͍ͯ·͢Ͷ

  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) }
  20. ͓Θ͔Γ͍͚ͨͩ·͔ͨ͠ʁ

  21. forEach͸Unit onEach͸List<Int>

  22. ίϨΫγϣϯͷϝιουνΣʔϯͷ్தͰɺ֤ཁૉʹॲཧΛߦ͏ϝιουɻ ฦΓ஋͸ݩͷίϨΫγϣϯɻ ίϨΫγϣϯࣗମͷ஋͸มΘΒͳ͍ɻ ෭࡞༻͕͋Δϝιουͱ͔΋ɻRxͷdoΈ͍ͨ

  23. Sequenceͷ৔߹ɺ ཁૉ΁ͷద༻ɾධՁ͸ଈ࣌Ͱ͸ͳ͍Ͱ͢ʂ

  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") }
  25. ============ 1 1 1 in for 2 4 4 in

    for 3 9 9 in for
  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) } }
  27. also/takeIf/takeUnless

  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
  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)
  30. also͸applyʹࣅ͍ͯΔ ͚ΕͲ ϥϜμࣜͷதͷthisͷҙຯ͕ҧ͏

  31. val bundleB = Bundle().apply { // ͜͜Ͱthis͸Bundle putString("KEY1", "VALUE1") this.putString("KEY2",

    "VALUE2") // ίϯύΠϧΤϥʔ //it.putString("KEY3", "VALUE3") }
  32. val bundleA = Bundle().also { // ͜͜Ͱthis͸Bundleͷ֎ଆ it.putString("KEY1", "VALUE1") //

    ίϯύΠϧΤϥʔ //putString("KEY2", "VALUE2") // ίϯύΠϧΤϥʔ //this.putString("KEY3", "VALUE3") }
  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 }
  34. groupingBy()

  35. ฦΓ஋ܕ͸৽ΠϯλʔϑΣʔε!Grouping interface Grouping<T, out K>

  36. Grouping͸άϧʔϓԽͯ͠ ͝ʹΐ͝ʹΐॲཧΛ͢ΔͨΊͷ தؒͷσʔλߏ଄Λ࢘ΔΠϯλʔϑΣʔε

  37. • aggregate • fold • reduce • eachCount ͳͲͳͲ

  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 }
  39. groupingByͱfoldͳͲͰɺ ΋ͬͱॊೈʹ

  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) })
  41. toMap()(ͱ(toMutableMap()

  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)
  43. Map.minus(key)

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

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

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

  47. ໊લͷ௨Γʂ minOf&:&Ҿ਺ʹ౉ͨ͠΋ͷ͔Β࠷খ஋Λฦ͢ maxOf&:&Ҿ਺ʹ౉ͨ͠΋ͷ͔Β࠷େ஋Λฦ͢ 2Ҿ਺όʔδϣϯͱ3Ҿ਺όʔδϣϯ͕͋Δ

  48. val min = minOf(1, 2) val max = maxOf(2.3, 5.1,

    4.7)
  49. ComparableΛ࣮૷ͨ͠΋ͷ΋Ͱ͖Δ val min = minOf( LocalDate.parse("2017-07-21"), LocalDate.parse("2017-07-20") )

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

    length = minOf( "Java", "Kotlin", "Android", Comparator { a, b -> a.length - b.length } )
  51. Array%like*List*instan/a/on* func/ons

  52. Kotlin'1.0͔Β ࣮͸഑ྻͷॳظԽɺ͜Μͳײ͡Ͱ͔͚ͨ val zeroFilledZize10 = Array(size=10) { 0 } val

    indexedFilled = Array(size=3) {index -> index }
  53. ͜ΕʹࣅͨListͱMutableListΛੜ੒͢Δ ʮؔ਺ʯ͕Kotlin'1.1͔ΒՃΘͬͨ val zeroFilledSize10 = MutableList(size=10) { 0 } val

    squaredIndexList = List(size = 4) { it * it }
  54. େจࣈͰ࢝·Δ͚Ͳ Ϋϥε໊͡Όͳͯ͘ɺؔ਺ͷ໊લʂ

  55. Map.getValue()

  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)
  57. Abstract(collec,ons

  58. Abstract(collec,ons ίϨΫγϣϯͷந৅Ϋϥε • AbstractCollec-on/ • AbstractList • AbstractSet • AbstractMap

  59. Abstract(collec,ons ίϨΫγϣϯͷந৅Ϋϥε • AbstractMutableCollec/on • AbstractMutableList • AbstractMutableSet • AbstractMutableMap

  60. java.u&l.AbstractCollec&onͬͯ͋Γ·ͨ͠ΑͶʁ

  61. JVMͰ͸ɺ kotlin.collec*ons.MutableܥͷίϨΫγϣϯ͸ɺ java.utl.AbstractܥͷίϨΫγϣϯΛܧঝ࣮ͯ͠૷͞Ε͍ͯΔ kotlin.collec*ons.AbstractMutableCollec*on͸ɺjava.u*l.AbstractCollec*onΛܧঝ

  62. JavaScriptͰ͸ɺ ͜ΕΒͷίϨΫγϣϯΛܧঝ֤ͯ͠छίϨΫγϣϯΛ࡞͍ͬͯΔ kotlin.collec*ons.ArrayList͸ɺkotlin.collec*ons.AbstractMutableListΛܧঝ͍ͯ͠Δ

  63. Kotlin'1.1͔Βొ৔ kotlin.collec*ons.ArrayList<E> JVMͰ͸ɺjava.u)l.ArrayList<E>ͷtypealias JavaScriptͰ͸ɺkotlin.collec0ons.AbstractMutableListΛܧঝͨ͠Ϋϥε

  64. ίϨΫγϣϯ͚ͩ͡Όͳͯ͘ɺExcep&onܥ΋ JVMͰ͸typealiasɺJavaScriptͰ͸ಠࣗఆٛ ClassCastExcep+on,IllegalStateExcep+on

  65. Array%manipula,on%func,ons

  66. ഑ྻͷཁૉ͝ͱ͝ʹΐ͝ʹΐ͢Δϝιου͕ Standard'Libraryʹ

  67. ഑ྻͷtoString() println(arrayOf(1, 2, 3).toString()) ࣮ߦ݁Ռ Ljava.lang.Integer;@46ee7fe8

  68. ͍΍ɺ͜Ε͕ݟ͍ͨΜ͡Όͳͯ͘orz

  69. ͦ͜ͰcontentToStringͰ͢Α

  70. ഑ྻͷcontentToString() println(arrayOf(1, 2, 3).contentToString()) ࣮ߦ݁Ռ [1, 2, 3]

  71. equalsͱcontentEquals΋ // false println(arrayOf(1, 2, 3).equals(arrayOf(1, 2, 3))) // true

    println(arrayOf(1, 2, 3).contentEquals(arrayOf(1, 2, 3)))
  72. ͍͍Ͷʂ

  73. ·ͱΊ

  74. Standard'Library ஍ຯʹ͔ͬ͠ΓਐԽ͍ͯͨͥ͠ʂ

  75. ͓·͚ kotlin'stdlib'jre8ͰStream2APIͱ͔΋