Sequenceの話 ~kotlin.sequences.Sequence~

 Sequenceの話 ~kotlin.sequences.Sequence~

2016/07/01(金) 第3回 Kotlin 勉強会 @Sansanの発表した資料です

F46c97bb74758d481531990654933050?s=128

RyotaMurohoshi

July 01, 2016
Tweet

Transcript

  1. Sequenceͷ࿩ @RyotaMurohoshi 2016/07/01'ୈ3ճ'Kotlinษڧձ'@SanSan

  2. ໰୊ ࣍ͷίʔυΛ࣮ߦ͢ΔͱɺͲ͏දࣔ͞ΕΔͰ͠ΐ͏͔ʁ ೋͭͷબ୒ࢶ͔ΒબΜͰ͍ͩ͘͞

  3. val list = listOf(1, 2, 3) val converted = list.map

    { println("map $it") it } println("--------") for(it in converted) println("for $it")
  4. બ୒ࢶ1 map 1 map 2 map 3 -------- for 1

    for 2 for 3
  5. બ୒ࢶ2 -------- map 1 for 1 map 2 for 2

    map 3 for 3
  6. ͲͬͪͰ͠ΐ͏ʁ

  7. ౴͑͸ બ୒ࢶ1

  8. kotlin.collections.List<T>ͷmapϝιου͸ શཁૉΛଈ࠲ʹࣹӨ͠·͢

  9. ͍ͭͮͯͷ໰୊ ࣍ͷίʔυΛ࣮ߦ͢ΔͱɺͲ͏දࣔ͞ΕΔͰ͠ΐ͏͔ʁ

  10. val list = listOf(1, 2, 3, 4, 5, 6, 7,

    8, 9) val result = list .map { println("map $it") it } .filter { println("filter $it") it % 2 == 0 } .take(3) println("--------") for(it in result) println("for $it")
  11. kotlin.collections.List<T>ͷfilterϝιου΋ mapϝιουͱಉ༷ʹଈ࠲ʹબ୒ॲཧ͕ߦΘΕ·͢ ɹ ͱ͍͏͜ͱ͸...

  12. map 1 map 2 /* தུ */ map 9 filter

    1 filter 2 /* தུ */ filter 9 -------- for 2 for 4 for 6
  13. kotlin.collections.List<T>ͷ֤ϝιουͱ Java8ͷStream*API΍C#ͷLINQͷҧ͍ʹ஫ҙ

  14. ࠷ޙͷ໰୊Ͱ͢

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

  16. 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) }
  17. ໰୊ ɹ Կ͕Α͘ͳ͍Ͱ͔͢ʁ

  18. ౴͑ 10݅ͷσʔλ͕ཉ͍͠ͷʹɺ 100000ߦͷσʔλɺ શͯಡΈࠐΜͰ͠·͍ͬͯΔ

  19. ͦ͜ͰSequenceͰ͢Α

  20. package kotlin.sequences public interface Sequence<out T> { public operator fun

    iterator(): Iterator<T> } ίϝϯτ͸ൈ͍ͯ·͕͢ɺҎԼΑΓ h"ps:/ /github.com/JetBrains/kotlin/blob/1.0.2/libraries/stdlib/src/kotlin/collec<ons/Sequence.kt
  21. Sequences.ktʹ͸ map΍filterɺcountɺtakeͳͲ ଟ͘ͷ֦ுؔ਺͕ఆٛ͞Ε͍ͯ·͢

  22. val list = listOf(1, 2, 3) val sequence = list.asSequence()

    // ஫໨ʂ val result = sequence .map { println("map $it") it } println("--------") for (it in result) println("for $it")
  23. -------- map 1 for 1 map 2 for 2 map

    3 for 3
  24. ཁૉͷࣹӨ͸ forͰΠςϨʔτ͞ΕඞཁʹͳͬͨλΠϛϯάͰ ߦΘΕ·͢

  25. // listOfͰͳ͘ɺsequenceOfʂ val sequence = sequenceOf(1, 2, 3, 4, 5,

    6, 7, 8, 9) val converted = sequence .map { println("map $it") it } .filter { println("filter $it") it % 2 == 0 } .take(3) println("--------") for(it in converted) println("for $it")
  26. -------- map 1 filter 1 map 2 filter 2 for

    2 map 3 /*தུ*/ map 5 filter 5 map 6 filter 6 for 6
  27. ϑΝΠϧͷྫ΋ݟͯΈ·͠ΐ͏ʂ

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

  29. ໰୊఺͸ඞཁͳ͍ͷʹ ϑΝΠϧΛશ෦ಡΜͰ͠·͏͜ͱ

  30. 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) }
  31. ϙΠϯτʂʂʂ val result: List<PersonData> = File("data.jsonlines") .useLines { lineSequences: Sequence<String>

    -> /*ུ*/ } ͜ΕͰϑΝΠϧશ෦ΛಡΈࠐ·ͳ͍ʂʂʂ
  32. ͱ͍͏͜ͱͰҰճ·ͱΊ

  33. ৔໘ʹԠͯ͡ద੾ʹ࢖͓͏ʂ • List<T>ͷmap΍filterͱ͔͸ͦͷ৔ͰશཁૉΛॲཧ • Sequence<T>ͷmap΍filterͱ͔͸ඞཁʹͳͬͨཁૉΛॲཧ

  34. ͱ͜ΖͰ...

  35. Sequenceͷfilterͷ࣮૷͸...

  36. public fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T> { return

    FilteringSequence(this, true, predicate) }
  37. ;Ή;ΉɺͳΔ΄Ͳ FilteringSequenceͷ࣮૷͸ʁ

  38. internal class FilteringSequence<T>(private val sequence: Sequence<T>, private val sendWhen: Boolean

    = true, private val predicate: (T) -> Boolean ) : Sequence<T> { override fun iterator(): Iterator<T> = object : Iterator<T> { val iterator = sequence.iterator(); var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue var nextItem: T? = null private fun calcNext() { while (iterator.hasNext()) { val item = iterator.next() if (predicate(item) == sendWhen) { nextItem = item nextState = 1 return } } nextState = 0 } override fun next(): T { if (nextState == -1) calcNext() if (nextState == 0) throw NoSuchElementException() val result = nextItem nextItem = null nextState = -1 return result as T } override fun hasNext(): Boolean { if (nextState == -1) calcNext() return nextState == 1 } } }
  39. ͳ͛͐ʂʂʂ

  40. IteratorΛ࣮૷ͨ͠ΦϒδΣΫτΛ࡞Βͳ͚Ε͹͍͚ͳ͍ ͍ͨ΁Μʂʂʂʂ

  41. ΋͠Sequenceͷࣗ࡞֦ுؔ਺Λ࡞Δͱͨ͠Βɺ໘౗͍͘͞ʂʂʂ ɹ ͱ͜Ζ͕...

  42. Kotlin'1.1Ͱ SequenceΛͭ͘Δϝιουָ͕ʹ ࡞Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳΓͦ͏ʂʂʂ

  43. yield/generator 1.0ϦϦʔε࣌఺Ͱɺgenerator͸༧໿ޠʹͳΓ·ͨ͠Ͷ

  44. yieldɺC#Ͱେ׆༂ʂʂʂ LINQ͸͜ͷyield͕׆༻͞Ε͍ͯΔʂ

  45. ࣗݾ঺հ

  46. • @RyotaMurohoshi • C#ɾUnity2(Unity෦2ӡӦ) • Unity༻ͷLINQϥΠϒϥϦ2UniLinq࡞੒ • KotlinͷLINQϥΠϒϥϦ2KotLinq࡞੒த

  47. C#erͰ͢! LINQ͕޷͖Ͱ͢ʂ (Sequence.mapͱ͔ʹ͍ۙίϨΫγϣϯؔ਺܈)

  48. C#ͰyieldΛ࢖ͬͯ filterʹ૬౰͢ΔॲཧΛߦ͏ʹ͸ʁ

  49. C#Ͱfilterʹ૬౰͢ΔॲཧΛߦ͏ʹ͸ʁ public static IEnumerable<TSource> Filter<TSource> ( this IEnumerable<TSource> source, Func<TSource,

    bool> predicate) { if (source == null) throw new ArgumentNullException (nameOf(source)); if (selector == null) throw new ArgumentNullException (nameOf(predicate)); return source.Filter_ (predicate); } public static IEnumerable<TSource> Filter_<TSource> ( this IEnumerable<TSource> source, Func<TSource,bool> predicate) { foreach (TSource element in source) if(predicate(element)) yield return element; }
  50. େࣄͳ෦෼͸͜͜ʂ͜Μͳʹ୹͍ʂ public static IEnumerable<TSource> Filter_<TSource> ( this IEnumerable<TSource> source, Func<TSource,bool>

    predicate) { foreach (TSource element in source) if(predicate(element)) yield return element; } ࣮ࡍʹC#Ͱ࢖ΘΕΔɺWhere͸࠷దԽͷͨΊʹ΋ͬͱ৭ʑ΍ͬͯ·͢
  51. yield࠷ߴʂ

  52. Kotlinͷyield/generatorͨͷ͠ΈͰ͢Ͷʂ h"ps:/ /github.com/Kotlin/kotlin4corou6nes

  53. ΋͔ͨ͠͠Β͜Μͳ෩ʹ୹͘ͳΔ͔ͳʁ public fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T> =

    generate { for(it in this){ if(predicate(it)){ yield(it) } } }
  54. ࣗ࡞Sequence֦ுؔ਺΋ॻ͖΍͘͢ͳΔ͔ͳʁ

  55. એ఻ʂ

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

  57. KotLinq • (΄΅)LINQޓ׵ • IterableɺMapɺArrayɺSequence΁ͷ֦ு਺܈ • ࣹӨɺબ୒ͱ͔தؒૢ࡞ͷϝιουͰ͸SequenceΛฦ͢

  58. ·ͱΊ • Sequenceɺ࢖͍Ͳ͜Ζ͕͋ͬͨΒͥͻʂ • yield/generator2ָ͠Έʂ • KotLinqΛ͓ٓ͘͠ئ͍͠·͢ʂ

  59. ͓·͚

  60. h"ps:/ /github.com/RyotaMurohoshi/Kollec8on

  61. Μʁ

  62. Kollec&on?

  63. Kollec&on!!!?

  64. ໊લม͑·͢ (΄ͱΜͲ࡞ͬͯͳ͍Ͱ͕͢)