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
2.9k
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
370
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
630
Odin Validationはいいぞ!
ryotamurohoshi
2
860
Tilemapはいいぞ!2020 〜すごいぞ、プロジェクト専用拡張Brush〜
ryotamurohoshi
0
2.3k
Unityでも、新しいC#
ryotamurohoshi
0
1.2k
Riderはいいぞ!
ryotamurohoshi
1
3.2k
Riderのススメ〜俺はRiderここが好き〜
ryotamurohoshi
1
2.2k
Unity開発者に伝えたい.NETのこと
ryotamurohoshi
4
36k
ImportedLinqのススメ
ryotamurohoshi
0
1.3k
Other Decks in Programming
See All in Programming
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
Remix on Hono on Cloudflare Workers
yusukebe
1
290
Quine, Polyglot, 良いコード
qnighy
4
640
役立つログに取り組もう
irof
28
9.6k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
290
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
cmp.Or に感動した
otakakot
3
170
Featured
See All Featured
A designer walks into a library…
pauljervisheath
204
24k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Six Lessons from altMBA
skipperchong
27
3.5k
How STYLIGHT went responsive
nonsquared
95
5.2k
Thoughts on Productivity
jonyablonski
67
4.3k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Faster Mobile Websites
deanohume
305
30k
Statistics for Hackers
jakevdp
796
220k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
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!!!?
໊લม͑·͢ (΄ͱΜͲ࡞ͬͯͳ͍Ͱ͕͢)