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

SequenceとCoroutineの話

 SequenceとCoroutineの話

2017/04/04 Kotlin Sansan #5 の @RyotaMurohoshiの資料です。

F46c97bb74758d481531990654933050?s=128

RyotaMurohoshi

April 04, 2017
Tweet

Transcript

  1. SequenceͱCorou*neͷ࿩ @RyotaMurohoshi 2017/04/04'ୈ5ճ'Kotlinษڧձ'@SanSan

  2. ࣗݾ঺հ @RyotaMurohoshi Kotlin͕޷͖Ͱ͢ɻC#΋޷͖Ͱ͢ɻ

  3. Sequence<T>ͬͯԿʁ

  4. Sequence<T> ίϨΫγϣϯΠϯλʔϑΣʔε public interface Sequence<out T> { public operator fun

    iterator(): Iterator<T> }
  5. Sequence<T>ʹ͸ɺList<T>΍Array<T>ͱಉ͡Α͏ʹ map΍filterͱ͍֦ͬͨுؔ਺͕͋Γ·͢

  6. Ͱ΋ڍಈ͕େ͖͘ҧ͍·͢

  7. List<T>ͷmapɾfilterͷڍಈ val list : List<Int> = listOf(0, 1, 2, 3)

    val converted = list .filter { println("filter $it") it.rem(2) == 0 } .map { println("map $it") it } println("--------") for(it in converted) println("for $it")
  8. List<T>ͷmapɾfilterͷڍಈ filter 0 filter 1 filter 2 filter 3 map

    0 map 2 -------- for 0 for 2
  9. List<T>ͷmap΍filter͸ɺ શཁૉʹରͯؔ͠਺Ϧςϥϧ͕ଈ࠲ʹద༻͞Ε·͢

  10. ࣍͸Sequence<T>

  11. Sequence<T>ͷmapɾfilterͷڍಈ val sequence : Sequence<Int> = sequenceOf(0, 1, 2, 3)

    val converted = sequence .filter { println("filter $it") it.rem(2) == 0 } .map { println("map $it") it } println("--------") for(it in converted) println("for $it")
  12. -------- filter 0 map 0 for 0 filter 1 filter

    2 map 2 for 2 filter 3
  13. Sequence<T>ͷmapɾfilter͸ ͦͷཁૉ͕ඞཁʹͳͬͨλΠϛϯάͰ ؔ਺Ϧςϥϧ͕ద༻͞Ε·͢

  14. JavaͷStream)API C#ͷLINQ ʹࣅͯ·͢Ͷ

  15. ʮͰɺ࣮ࡍ࢖͏৔໘͋Δͷʁʯ

  16. ͋ΔΜͰ͢ʂ

  17. ࣮ࡍʹSequence<T>ͷ׆༂͢Δ৔໘ • ϑΝΠϧͷ1ߦͣͭಡΈࠐΈ#File.useLines • σΟϨΫτϦͷϑΝΠϧ୳ࡧ#FireTreeWalk

  18. ͢΂ͯ࠷ޙ·ͰҰؾʹ΍Δඞཁ͕ͳ͍ ͦ͏͍͏࣌ʹ׆༂

  19. Sequence<T>͕ͲΜͳ΋ͷ͔ɺ ͲΜͳ৔໘Ͱ׆༂͢Δ͔Θ͔Γ·ͨ͠Ͷʁ

  20. ࣍͸ɺ͖ͬ͞ͷfilterͷ࣮૷ΛݟͯΈ·͠ΐ͏

  21. List<T>ͷfilterͷ࣮૷ ࣮ࡍʹ͸Iterable<T>ͷfilter

  22. ୯७ɻͦͷ·Μ· ৽ͨͳArrayListΛ࡞ͬͯɺforͯ͠ɺifͯ͠ɺaddͯ͠

  23. List<T>(Iterable<T>)ͷfilterͷ࣮૷ public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T>

    { return filterTo(ArrayList<T>(), predicate) } public inline fun <T, C : MutableCollection<in T>> Iterable<T> .filterTo(destination: C, predicate: (T) -> Boolean): C { for (element in this) if (predicate(element)) destination.add(element) return destination } h"ps:/ /github.com/JetBrains/kotlin/blob/v1.1.1/libraries/stdlib/src/generated/_Collec=ons.kt>ΑΓ Πϯσϯτ͸ม͍͑ͯ·͢
  24. ୯७ɻͦͷ·Μ· ৽ͨͳArrayListΛ࡞ͬͯɺforͯ͠ɺifͯ͠ɺaddͯ͠

  25. ͡Ό͊ɺSequence<T>͸ʁ

  26. Sequence<T>ͷfilterͷ࣮૷͸ʁ public fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T> {

    return FilteringSequence(this, true, predicate) } h"ps:/ /github.com/JetBrains/kotlin/blob/v1.1.1/libraries/stdlib/src/generated/_Sequences.kt#L307
  27. ;Ή;ΉɻͳΔ΄Ͳɻ FilteringSequenceͷ࣮૷͸ʁ

  28. FilteringSequence<T>ͷ࣮૷ 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 } } }
  29. ௕͍ʂ

  30. Iterator<T>Λ࣮૷͠ͳ͍ͱ͍͚ͳ͍ ௕͘ͳͬͯ͠·͏

  31. େม

  32. ࠓ೔ͷςʔϚ͸Kotlin'1.1 ͓଴ͨͤ͠·ͨ͠

  33. Kotlin1.1ͰCorou,neಋೖ (·ͩexperimentalͬͯ෇͍͍ͯΔ͚Ͳʂ)

  34. filterͱಉ༷ͷॲཧ͸ɺ΋͏௕ʑॻ͔ͳͯ͘΋࣮૷Ͱ͖Δ

  35. Corou%neΛ࢖ͬͯ Sequence<T>ͷfilterΛॻ͘ͱʁ

  36. Kotlin'1.1ͰSequence<T>ͷfilter fun <T> Sequence<T>.where(predicate: (T) -> Boolean): Sequence<T> = buildSequence

    { for (element in iterator()) { if (predicate(element)) { yield(element) } } }
  37. buildSequenceʂ ͦͯ͠yieldʂ

  38. forͯ͠ifͯ͠ ௚ײతͰ͠ΐʁ

  39. Kotlin'1.1ͰSequence<T>ͷfilter fun <T> Sequence<T>.where(predicate: (T) -> Boolean): Sequence<T> = buildSequence

    { for (element in iterator()) { if (predicate(element)) { yield(element) } } }
  40. ͜ΕͰSequence<T>Λ؆୯ʹ࡞ΕΔ

  41. Generator ʢͱ͔ݴΘΕͯΔΒ͍͠ʣ

  42. buildSequenceͷڍಈΛprintlnͰ

  43. val sequence: Sequence<String> = buildSequence { println("before A") yield("A") println("before

    B") yield("B") println("last") } println("-----------") for (element in sequence) { println("for $element") }
  44. ----------- before A for A before B for B last

  45. ϙΠϯτ͸!தஅ!ͱ!࠶։

  46. val sequence: Sequence<String> = buildSequence { println("before A") yield("A") println("before

    B") yield("B") println("last") } println("-----------") val iterator = sequence.iterator() println(iterator.hasNext()) println(iterator.next()) println(iterator.hasNext()) println(iterator.next()) println(iterator.hasNext())
  47. ----------- before A true A before B true B lastfalse

  48. GeneratorΛ࢖ͬͯ ͍Ζ͍Ζ࡞ͬͯΈ·͠ΐ͏

  49. ແݶUnit

  50. ແݶUnit fun infiniteUnitSequence() = buildSequence { while (true) { yield(Unit)

    } }
  51. map

  52. map fun <T, R> Sequence<T>.map(selector: (T) -> R): Sequence<R> =

    buildSequence { for (element in iterator()) { yield(selector(element)) } }
  53. File.useLinesͬΆ͍΍ͭ fun sequenceOfLines(fileName: String) = buildSequence<String> { BufferedReader(FileReader(fileName)).use { while

    (true) { yield(it.readLine() ?: break) } } } h"ps:/ /github.com/Kotlin/kotlin4corou6nes/blob/master/examples/sequence/sequenceOfLines.kt
  54. buffer(count,,skip)

  55. val sequence = sequenceOf(0, 1, 2, 3, 4, 5) val

    buffered = sequence.buffer(3, 2) println(buffered.toList()) // දࣔͷͨΊtoList //[[0, 1, 2],[2, 3, 4], [4, 5]]
  56. buffer(count,,skip) fun <T> Sequence<T>.buffer(count: Int, skip: Int): Sequence<List<T>> = buildSequence

    { val buffers = LinkedList<MutableList<T>>() var i = 0 forEach { element -> if (i.rem(skip) == 0) buffers.add(mutableListOf()) for (buffer in buffers) buffer.add(element) if (buffers.size > 0 && buffers.peek().count() == count) yield(buffers.poll().toList()) i++ } yieldAll(buffers) }
  57. Ͷʁ buildSequenceؔ਺Λ࢖͑͹؆୯ʹ Sequenceͷؔ਺Λ࡞ΕΔͰ͠ΐʁ

  58. ·ͱΊ • Sequence<T>ศར • Ͱ΋Kotlin01.0Ͱ͸Sequenceͳϝιουͷ࣮૷໘౗ • Kotlin01.1ͰCorou6neՃΘͬͨ • Corou6ne͸async0/0await͚ͩ͡Όͳ͍ •

    buildSequenceͰSequenceͳϝιουͷ࣮૷ָʹͰ͖Δ
  59. ͕࣌ؒڐ͢ݶΓখωλΛ

  60. QʮbuildSequenceʁgenerate͡Όͳ͍ͷʁʯ

  61. A"ͳΜ͔มΘͬͨͬΆ͍Ͱ͢Αɻ

  62. Kotlin/kotlin)corou-nesͷυΩϡϝϯτ͸ 2017/01/17ͰมΘͬͯ·ͨ͠ h"ps:/ /github.com/Kotlin/kotlin4corou6nes/commit/c8232f0cbff664d232fe46c1a554219c482fe10d

  63. Q"ʮͲ͏΍ͬͨΒࢼͤΔͷʁʯ

  64. A"ʮGenerator(buildSequence)͸ ௥ՃϥΠϒϥϦ͍Βͳ͍Ͱ͢Αʯ ͨͩexperimentalͳcorou.ne͸ܯࠂग़Δ͔Βɺ཈੍ϑϥάΛཱ͍ͯͯͩ͘͞Ͷ h"p:/ /kotlinlang.org/docs/diagnos3cs/experimental7corou3nes.html

  65. Q"ʮExperimentalͳͷ?ʯ

  66. A"ʮExperimentalͩͦ͏Ͱ͢ʯ

  67. Ͳ͏͍͏͜ͱʁ

  68. KotlinͷExperimentalͷఆٛʹΑΔͱɺ • 1.1.xͷؒ͸ޙํޓ׵ੑͷอূ͋Γ • 1.2Ͱ͸อূͳ͠ h"ps:/ /kotlinlang.org/docs/reference/compa6bility.html#experimental;features

  69. experimental͔ͩΒύοέʔδ͸ kotlin.corou+nes.experimental API͕֬ఆͨ͠Βɺkotlin.corou.nesʹ (ޙํޓ׵ੑͷͨΊkotlin.corou,nes.experimental΋࢒Δ) h"ps:/ /kotlinlang.org/docs/reference/corou6nes.html#experimental:status:of:corou6nes

  70. Kotlin'1.1ͷϒϩάΑΓ An#important#note.#With#all#the#benefits#that#they#bring,#Kotlin# corou9nes#are#a#fairly#new#design#that#needs#extensive#ba?le@ tes9ng#before#we#can#be#sure#it’s#100%#right#and#complete.#This#is# why#we#will#release#it#under#an#“experimental”#opt@in#flag.#We#do# not#expect#the#language#rules#to#change,#but#APIs#may#require# some#adjustments#in#Kotlin#1.2.

  71. Kotlin'1.1ͷϒϩάΑΓ We#do#not#expect#the#language#rules#to#change,#but#APIs#may# require#some#adjustments#in#Kotlin#1.2.

  72. ͳΔ΄ͲɺมΘΔՄೳੑ͸͋Δ

  73. ·ͱΊ • Sequence<T>ศར • Ͱ΋Kotlin01.0Ͱ͸Sequenceͳϝιουͷ࣮૷໘౗ • Kotlin01.1ͰCorou6neՃΘͬͨ • Corou6ne͸async0/0await͚ͩ͡Όͳ͍ •

    buildSequenceͰSequenceͳϝιουͷ࣮૷ָʹͰ͖Δ • experimental
  74. ๻͸·ͣݸਓϥΠϒϥϦ͔ΒΨϯΨϯ࢖͍·͢