Pro Yearly is on sale from $80 to $50! »

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

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

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

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

F46c97bb74758d481531990654933050?s=128

RyotaMurohoshi

July 05, 2016
Tweet

Transcript

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

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

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

  4. List<T>ͷ৔߹

  5. val lineList: List<String> = File("data.jsonlines").readLines() val result = lineList .map

    { convertToData(it) } .filter { filterData(it) } .take(10) for (it in result) { println(it) }
  6. ࠷ޙ·ͰϑΝΠϧΛಡΜͰ͠·͏

  7. Sequence<T>ͷ৔߹

  8. val result: List<PersonData> = File("data.jsonlines") .useLines { lineSequences: Sequence<String> ->

    lineSequences .map { convertToData(it) } .filter { filterData(it) } .take(10) .toList() } for (it in result) { println(it) }
  9. ϑΝΠϧΛ࠷ޙ·Ͱಡ·ͳ͍

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

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

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

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

  14. ·͔͞ͷ0.0!

  15. ͳͥͩʂʁ

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

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

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

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

  20. data class Person( val id: Long,ɹval name: String, val teamId:

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

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

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

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

  25. ಥવͷએ఻

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

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

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

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

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

  31. Μʁ

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

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

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

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

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

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

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

    ϝιουͷΦʔόʔϩʔυͱit͕૬ੑѱ͍݅
  39. ·ͱΊ KotLinqʹ੕Λ