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

SequenceとCoroutineの話

 SequenceとCoroutineの話

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

RyotaMurohoshi

April 04, 2017
Tweet

More Decks by RyotaMurohoshi

Other Decks in Technology

Transcript

  1. 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")
  2. 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")
  3. 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>ΑΓ Πϯσϯτ͸ม͍͑ͯ·͢
  4. 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
  5. 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 } } }
  6. 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) } } }
  7. 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) } } }
  8. 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") }
  9. 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())
  10. map

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

    buildSequence { for (element in iterator()) { yield(selector(element)) } }
  12. 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
  13. 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]]
  14. 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) }