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

いつか役にたつかもしれないコレクション周りの5つのこと

 いつか役にたつかもしれないコレクション周りの5つのこと

2016//07/05(火)に開催された「Kotlin 勉強会 #1 - 初めての Kotlin」で発表した資料です

http://connpass.com/event/33584/

RyotaMurohoshi

July 05, 2016
Tweet

More Decks by RyotaMurohoshi

Other Decks in Programming

Transcript

  1. ผʹܶతʹ͸มΘΒͳ͍͚ΕͲ
    ͍͔ͭ໾ʹ͔ͨͭ΋͠Εͳ͍
    ίϨΫγϣϯपΓͷ5ͭͷ͜ͱ
    @RyotaMurohoshi
    2016/07/05(Ր)*͸͡ΊͯͷKotlin

    View Slide

  2. ͦͷ1
    ঢ়گʹԠͯ͡SequenceΛ࢖͏΂͖ͳ݅

    View Slide

  3. 100000ߦͷJSON'LinesϑΝΠϧ
    ͜ΕΛ1ߦͣͭdataΫϥεͷΦϒδΣΫτʹม׵͠
    ͋Δ৚݅Λຬͨ͢ΦϒδΣΫτΛ
    ࠷ॳ͔Β10͚݅ͩ֬ೝද͍ࣔͨ͠

    View Slide

  4. Listͷ৔߹

    View Slide

  5. val lineList: List =
    File("data.jsonlines").readLines()
    val result = lineList
    .map { convertToData(it) }
    .filter { filterData(it) }
    .take(10)
    for (it in result) {
    println(it)
    }

    View Slide

  6. ࠷ޙ·ͰϑΝΠϧΛಡΜͰ͠·͏

    View Slide

  7. Sequenceͷ৔߹

    View Slide

  8. val result: List = File("data.jsonlines")
    .useLines { lineSequences: Sequence ->
    lineSequences
    .map { convertToData(it) }
    .filter { filterData(it) }
    .take(10)
    .toList()
    }
    for (it in result) {
    println(it)
    }

    View Slide

  9. ϑΝΠϧΛ࠷ޙ·Ͱಡ·ͳ͍

    View Slide

  10. ৄ͘͠͸͜ͷࢿྉͰʂ
    h"ps:/
    /speakerdeck.com/ryotamurohoshi/sequencefalsehua8kotlin8dot8sequences8dot8sequence

    View Slide

  11. ͦͷ2
    ཁૉ͕ۭͷ࣌ͷaverageϝιου͕
    0.0Λฦͯ͠͠·͏݅

    View Slide

  12. averageϝιουͱ͸ʁ
    Listͱ͔ͷฏۉܭࢉϝιου

    View Slide

  13. ͡Ό͋ɺཁૉ͕ۭͷ࣌͸ʁ

    View Slide

  14. ·͔͞ͷ0.0!

    View Slide

  15. ͳͥͩʂʁ

    View Slide

  16. ͜Ε͡Όܭࢉ݁Ռ͕0.0ͩͬͨͷ͔ɺ
    ཁૉ͕ۭ͔ͩΒ0.0ͬͨͷ͔
    ۠ผԽ͔ͭͳ͍ʂʂʂ

    View Slide

  17. ͦ΋ͦ΋ͳͥDouble?ܕʹ͠ͳ͔ͬͨͷ͔ʁ
    max΍min͸Int?Λฦ͢ͷʹʂ
    ΋͘͠͸ྫ֎Λ౤͛ͯཉ͍͠

    View Slide

  18. ͦͷ3
    Ωʔ͕ॏෳͨ͠ࡍͷassociateܥϝιου͕ɺ
    ྫ֎Λ౤͛ͳ͍݅

    View Slide

  19. associateܥͷϝιουͱ͸ʁ
    Listͱ͔͔ΒMapΛͭ͘Δϝιου

    View Slide

  20. data class Person(
    val id: Long,ɹval name: String, val teamId: Long)
    // ॏෳidͷPerson͸ଘࡏ͠ͳ͍
    // ಉ͡teamIdΛ࣋ͭPerson͸ଘࡏ͠͏Δ
    val personList : Listɹ= loadPersonList()
    // id͕ॏෳ͸͠ͳ͍ͷͰ໰୊ແ͠
    val personMap : Map = personList.associateBy { it.id }
    // ʲ஫໨ʂʂʂʳಉ͡teamIdΛ΋ͭཁૉ͕personList಺ʹଘࡏ͠͏Δʂʂʂʂʂ
    val teamMap : Map = personList.associateBy { it.teamId }

    View Slide

  21. ΋͠Key͕ॏෳͨ͠Βʁ

    View Slide

  22. ޙউʂ
    ͋ͱ͕ͪ:ޙΖͷཁૉͰ্ॻ͖

    View Slide

  23. ݸਓతʹ͸ྫ֎Λ౤͛ͯཉ͍͠

    View Slide

  24. ͦ΋ͦ΋MapΛͭ͘ͷͰ͋Ε͹ɺKey͸Ұҙͳ΋ͷͳ͸ͣ
    Key͕ॏෳ͢Δ΋ͷΛ૝ఆ͢ΔͳΒ͹ɺgroupByΛ࢖͏΂͖

    View Slide

  25. ಥવͷએ఻

    View Slide

  26. C#ͷLINQޓ׵ͷϥΠϒϥϦ
    KotLinq࡞͍ͬͯ·͢ʂʂʂ
    h"ps:/
    /github.com/RyotaMurohoshi/KotLinq

    View Slide

  27. ࠓճͷTips͸
    KotLinqΛ࡞͍ͬͯΔࡍͷ஌ݟͰ͢

    View Slide

  28. ͦͷ4
    σϑΥϧτΠϯϙʔτ֦ுؔ਺Λ
    ্ॻ͖Ͱ͖Δ݅

    View Slide

  29. ࣗ࡞ϥΠϒϥϦKotLinqͷ಺ʹ͸
    takeͬͯϝιου͕͋Γ·͢ʂ

    View Slide

  30. KotLinq಺ͷtakeϝιου
    fun Iterable.take(count: Int): Sequence =
    asSequence().take(count)

    View Slide

  31. Μʁ

    View Slide

  32. ඪ४ؔ਺ʹ΋takeͬͯ͋ͬͨΑ͏ͳ?
    fun Iterable.take(n: Int): List

    View Slide

  33. ඪ४ͰΠϯϙʔτͰ͖Δ֦ுؔ਺
    Λ্ॻ͖Ͱ͖Δ݅ʂ

    View Slide

  34. ͦͷ5
    ϝιουͷΦʔόʔϩʔυͱit͕
    ૬ੑѱ͍݅
    (͜Ε͋Μ·ΓίϨΫγϣϯؔ܎ͳ͍)

    View Slide

  35. ࣹӨϝιου!ͱ!ΠϯσοΫε෇͖ࣹӨϝιουೋͭ࡞Γ͍ͨ
    // ࣹӨϝιου
    fun Sequence
    .select(selector: (TSource) -> TResult): Sequence
    // ΠϯσοΫε෇͖ࣹӨϝιου
    fun Sequence
    .select(selector: (TSource, Int) -> TResult): Sequence

    View Slide

  36. ࣹӨϝιου!ͱ!ΠϯσοΫε෇͖ࣹӨϝιου
    ҰҙʹܾΊΒΕͳ͍͔ΒίϯύΠϧΤϥʔ
    sequenceOf(1, 2, 3).select { it * 2 }

    View Slide

  37. KotLinq(Ͱ͸ɺΠϯσοΫε෇͖ࣹӨϝιουΛ
    ผ໊ʹ͠·ͨ͠
    sequence.select { it * it }!ͱ͔͍ͨ͠͠

    View Slide

  38. ·ͱΊ
    • ঢ়گʹԠͯ͡SequenceΛ࢖͏΂͖ͳ݅
    • ཁૉ͕ۭͷ࣌ͷaverageϝιου͕0.0Λฦͯ͠͠·͏݅
    • Ωʔ͕ॏෳͨ͠ࡍͷassociateܥϝιου͕ྫ֎Λ౤͛ͳ͍݅
    • σϑΥϧτΠϯϙʔτ֦ுؔ਺Λ্ॻ͖Ͱ͖Δ݅
    • ϝιουͷΦʔόʔϩʔυͱit͕૬ੑѱ͍݅

    View Slide

  39. ·ͱΊ
    KotLinqʹ੕Λ

    View Slide