Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Sequenceの話 ~kotlin.sequences.Sequence~
Search
RyotaMurohoshi
July 01, 2016
Programming
3.4k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Sequenceの話 ~kotlin.sequences.Sequence~
2016/07/01(金) 第3回 Kotlin 勉強会 @Sansanの発表した資料です
RyotaMurohoshi
July 01, 2016
More Decks by RyotaMurohoshi
See All by RyotaMurohoshi
Tilemapのアップデートについて
ryotamurohoshi
0
100
Unityの合同同人誌や合同商業誌を書いてる僕は感想やレビューや評価が欲しい
ryotamurohoshi
0
640
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
930
Odin Validationはいいぞ!
ryotamurohoshi
2
1.1k
Tilemapはいいぞ!2020 〜すごいぞ、プロジェクト専用拡張Brush〜
ryotamurohoshi
0
2.6k
Unityでも、新しいC#
ryotamurohoshi
0
1.6k
Riderはいいぞ!
ryotamurohoshi
1
3.9k
Riderのススメ〜俺はRiderここが好き〜
ryotamurohoshi
1
2.8k
Unity開発者に伝えたい.NETのこと
ryotamurohoshi
4
38k
Other Decks in Programming
See All in Programming
地方こそサーバーレス — 10年続けた、顔が見えるチームで確かな基盤を / Serverless Matters Most in Regional Areas — 10 Years of Building Reliable Foundations with Close-Knit Teams
seike460
PRO
0
100
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
200
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3.1k
AmebaブログはKMPから卒業します
kmars0605
0
330
우리의 Nitro Modules는 당신의 Turbo Modules보다 아름답다
l2hyunwoo
0
250
動画プレイヤーはなぜカクつくのか 〜Flutterのレンダリングとネイティブの境界を解剖する〜
naruogram
0
330
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
120
Railsバージョンを4倍にした話
optim
0
940
Seven New Principles of Software Engineering
yegor256
0
450
DroidKnights 2026 Android A2UI Jetpack Compose 기반의 안전한 AI UI 렌더링 - 박제창
itsmedreamwalker
1
190
LLM 101
dalinaum
0
470
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
4
3.3k
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Scaling GitHub
holman
464
140k
Code Review Best Practice
trishagee
74
20k
So, you think you're a good person
axbom
PRO
2
2.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Testing 201, or: Great Expectations
jmmastey
46
8.4k
My Coaching Mixtape
mlcsv
0
380
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.7k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
480
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.3k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.4k
Automating Front-end Workflow
addyosmani
1370
210k
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!!!?
໊લม͑·͢ (΄ͱΜͲ࡞ͬͯͳ͍Ͱ͕͢)