Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Sequenceの話 ~kotlin.sequences.Sequence~
Search
RyotaMurohoshi
July 01, 2016
Programming
2
3k
Sequenceの話 ~kotlin.sequences.Sequence~
2016/07/01(金) 第3回 Kotlin 勉強会 @Sansanの発表した資料です
RyotaMurohoshi
July 01, 2016
Tweet
Share
More Decks by RyotaMurohoshi
See All by RyotaMurohoshi
Unityの合同同人誌や合同商業誌を書いてる僕は感想やレビューや評価が欲しい
ryotamurohoshi
0
420
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
650
Odin Validationはいいぞ!
ryotamurohoshi
2
880
Tilemapはいいぞ!2020 〜すごいぞ、プロジェクト専用拡張Brush〜
ryotamurohoshi
0
2.3k
Unityでも、新しいC#
ryotamurohoshi
0
1.2k
Riderはいいぞ!
ryotamurohoshi
1
3.3k
Riderのススメ〜俺はRiderここが好き〜
ryotamurohoshi
1
2.3k
Unity開発者に伝えたい.NETのこと
ryotamurohoshi
4
36k
ImportedLinqのススメ
ryotamurohoshi
0
1.3k
Other Decks in Programming
See All in Programming
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
Online-Dokumentation, die hilft: Strukturen, Prozesse, Tools
ahus1
0
100
良いユニットテストを書こう
mototakatsu
8
3.1k
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
150
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
350
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
200
Recoilを剥がしている話
kirik
5
7.2k
AppRouterを用いた大規模サービス開発におけるディレクトリ構成の変遷と問題点
eiganken
1
150
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
180
テストコード書いてみませんか?
onopon
2
200
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
340
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
820
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
4 Signs Your Business is Dying
shpigford
182
21k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Testing 201, or: Great Expectations
jmmastey
41
7.1k
Navigating Team Friction
lara
183
15k
Designing for Performance
lara
604
68k
The Pragmatic Product Professional
lauravandoore
32
6.3k
What's in a price? How to price your products and services
michaelherold
244
12k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Practical Orchestrator
shlominoach
186
10k
Transcript
Sequenceͷ @RyotaMurohoshi 2016/07/01'ୈ3ճ'Kotlinษڧձ'@SanSan
࣍ͷίʔυΛ࣮ߦ͢ΔͱɺͲ͏දࣔ͞ΕΔͰ͠ΐ͏͔ʁ ೋͭͷબࢶ͔ΒબΜͰ͍ͩ͘͞
val list = listOf(1, 2, 3) val converted = list.map
{ println("map $it") it } println("--------") for(it in converted) println("for $it")
બࢶ1 map 1 map 2 map 3 -------- for 1
for 2 for 3
બࢶ2 -------- map 1 for 1 map 2 for 2
map 3 for 3
ͲͬͪͰ͠ΐ͏ʁ
͑ બࢶ1
kotlin.collections.List<T>ͷmapϝιου શཁૉΛଈ࠲ʹࣹӨ͠·͢
͍ͭͮͯͷ ࣍ͷίʔυΛ࣮ߦ͢ΔͱɺͲ͏දࣔ͞ΕΔͰ͠ΐ͏͔ʁ
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")
kotlin.collections.List<T>ͷfilterϝιου mapϝιουͱಉ༷ʹଈ࠲ʹબॲཧ͕ߦΘΕ·͢ ɹ ͱ͍͏͜ͱ...
map 1 map 2 /* தུ */ map 9 filter
1 filter 2 /* தུ */ filter 9 -------- for 2 for 4 for 6
kotlin.collections.List<T>ͷ֤ϝιουͱ Java8ͷStream*APIC#ͷLINQͷҧ͍ʹҙ
࠷ޙͷͰ͢
100000ߦͷJSON'LinesϑΝΠϧ ͜ΕΛ1ߦͣͭdataΫϥεͷΦϒδΣΫτʹม͠ ͋Δ݅Λຬͨ͢ΦϒδΣΫτΛ ࠷ॳ͔Β10͚݅ͩ֬ೝද͍ࣔͨ͠
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) }
ɹ Կ͕Α͘ͳ͍Ͱ͔͢ʁ
͑ 10݅ͷσʔλ͕ཉ͍͠ͷʹɺ 100000ߦͷσʔλɺ શͯಡΈࠐΜͰ͠·͍ͬͯΔ
ͦ͜ͰSequenceͰ͢Α
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
Sequences.ktʹ mapfilterɺcountɺtakeͳͲ ଟ͘ͷ֦ு͕ؔఆٛ͞Ε͍ͯ·͢
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")
-------- map 1 for 1 map 2 for 2 map
3 for 3
ཁૉͷࣹӨ forͰΠςϨʔτ͞ΕඞཁʹͳͬͨλΠϛϯάͰ ߦΘΕ·͢
// 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")
-------- map 1 filter 1 map 2 filter 2 for
2 map 3 /*தུ*/ map 5 filter 5 map 6 filter 6 for 6
ϑΝΠϧͷྫݟͯΈ·͠ΐ͏ʂ
͓͞Β͍ 100000ߦͷJSON'LinesϑΝΠϧ ͜ΕΛ1ߦͣͭdataΫϥεͷΦϒδΣΫτʹม͠ ͋Δ݅Λຬͨ͢ΦϒδΣΫτΛ ࠷ॳ͔Β10͚݅ͩ֬ೝද͍ࣔͨ͠
ඞཁͳ͍ͷʹ ϑΝΠϧΛશ෦ಡΜͰ͠·͏͜ͱ
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) }
ϙΠϯτʂʂʂ val result: List<PersonData> = File("data.jsonlines") .useLines { lineSequences: Sequence<String>
-> /*ུ*/ } ͜ΕͰϑΝΠϧશ෦ΛಡΈࠐ·ͳ͍ʂʂʂ
ͱ͍͏͜ͱͰҰճ·ͱΊ
໘ʹԠͯ͡దʹ͓͏ʂ • List<T>ͷmapfilterͱ͔ͦͷͰશཁૉΛॲཧ • Sequence<T>ͷmapfilterͱ͔ඞཁʹͳͬͨཁૉΛॲཧ
ͱ͜ΖͰ...
Sequenceͷfilterͷ࣮...
public fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T> { return
FilteringSequence(this, true, predicate) }
;Ή;ΉɺͳΔ΄Ͳ FilteringSequenceͷ࣮ʁ
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 } } }
ͳ͛͐ʂʂʂ
IteratorΛ࣮ͨ͠ΦϒδΣΫτΛ࡞Βͳ͚Ε͍͚ͳ͍ ͍ͨΜʂʂʂʂ
͠Sequenceͷࣗ࡞֦ுؔΛ࡞Δͱͨ͠Βɺ໘͍͘͞ʂʂʂ ɹ ͱ͜Ζ͕...
Kotlin'1.1Ͱ SequenceΛͭ͘Δϝιουָ͕ʹ ࡞Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳΓͦ͏ʂʂʂ
yield/generator 1.0ϦϦʔε࣌Ͱɺgenerator༧ޠʹͳΓ·ͨ͠Ͷ
yieldɺC#Ͱେ׆༂ʂʂʂ LINQ͜ͷyield͕׆༻͞Ε͍ͯΔʂ
ࣗݾհ
• @RyotaMurohoshi • C#ɾUnity2(Unity෦2ӡӦ) • Unity༻ͷLINQϥΠϒϥϦ2UniLinq࡞ • KotlinͷLINQϥΠϒϥϦ2KotLinq࡞த
C#erͰ͢! LINQ͕͖Ͱ͢ʂ (Sequence.mapͱ͔ʹ͍ۙίϨΫγϣϯؔ܈)
C#ͰyieldΛͬͯ filterʹ૬͢ΔॲཧΛߦ͏ʹʁ
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; }
େࣄͳ෦͜͜ʂ͜Μͳʹ͍ʂ 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࠷దԽͷͨΊʹͬͱ৭ʑͬͯ·͢
yield࠷ߴʂ
Kotlinͷyield/generatorͨͷ͠ΈͰ͢Ͷʂ h"ps:/ /github.com/Kotlin/kotlin4corou6nes
͔ͨ͠͠Β͜Μͳ෩ʹ͘ͳΔ͔ͳʁ public fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T> =
generate { for(it in this){ if(predicate(it)){ yield(it) } } }
ࣗ࡞Sequence֦ுؔॻ͖͘͢ͳΔ͔ͳʁ
એʂ
C#ͷLINQޓͷϥΠϒϥϦ KotLinq࡞͍ͬͯ·͢ʂʂʂ h"ps:/ /github.com/RyotaMurohoshi/KotLinq
KotLinq • (΄΅)LINQޓ • IterableɺMapɺArrayɺSequenceͷ֦ு܈ • ࣹӨɺબͱ͔தؒૢ࡞ͷϝιουͰSequenceΛฦ͢
·ͱΊ • Sequenceɺ͍Ͳ͜Ζ͕͋ͬͨΒͥͻʂ • yield/generator2ָ͠Έʂ • KotLinqΛ͓ٓ͘͠ئ͍͠·͢ʂ
͓·͚
h"ps:/ /github.com/RyotaMurohoshi/Kollec8on
Μʁ
Kollec&on?
Kollec&on!!!?
໊લม͑·͢ (΄ͱΜͲ࡞ͬͯͳ͍Ͱ͕͢)