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

Sequenceの話 ~kotlin.sequences.Sequence~

 Sequenceの話 ~kotlin.sequences.Sequence~

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

RyotaMurohoshi

July 01, 2016
Tweet

More Decks by RyotaMurohoshi

Other Decks in Programming

Transcript

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

    { println("map $it") it } println("--------") for(it in converted) println("for $it")
  2. 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")
  3. map 1 map 2 /* தུ */ map 9 filter

    1 filter 2 /* தུ */ filter 9 -------- for 2 for 4 for 6
  4. 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) }
  5. 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
  6. 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")
  7. // 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")
  8. -------- map 1 filter 1 map 2 filter 2 for

    2 map 3 /*தུ*/ map 5 filter 5 map 6 filter 6 for 6
  9. 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) }
  10. 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 } } }
  11. 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; }
  12. େࣄͳ෦෼͸͜͜ʂ͜Μͳʹ୹͍ʂ 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͸࠷దԽͷͨΊʹ΋ͬͱ৭ʑ΍ͬͯ·͢