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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
RyotaMurohoshi
July 01, 2016
Programming
3.3k
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
96
Unityの合同同人誌や合同商業誌を書いてる僕は感想やレビューや評価が欲しい
ryotamurohoshi
0
630
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
900
Odin Validationはいいぞ!
ryotamurohoshi
2
1.1k
Tilemapはいいぞ!2020 〜すごいぞ、プロジェクト専用拡張Brush〜
ryotamurohoshi
0
2.6k
Unityでも、新しいC#
ryotamurohoshi
0
1.5k
Riderはいいぞ!
ryotamurohoshi
1
3.8k
Riderのススメ〜俺はRiderここが好き〜
ryotamurohoshi
1
2.8k
Unity開発者に伝えたい.NETのこと
ryotamurohoshi
4
38k
Other Decks in Programming
See All in Programming
Claude Codeを組織的に動かして月400PRを実現した話
happy_ryo
0
270
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
270
Java 27新機能 / Java 27 new features
kishida
2
130
アクセシビリティから考える情報設計
high_g_engineer
0
370
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
6
2.2k
Heart of Swift Concurrency
koher
0
700
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
350
巨大モノリシックアプリ モダン化大作戦
ktcryomm
0
1k
Jetpack Compose メカニズム
skydoves
0
410
Building an Out-of-Order CPU
latte72
1
770
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
110
cdk deploy JawsSonic #MARATHONしながらAWSリソースをデプロイしてみよう
akihisaikeda
2
130
Featured
See All Featured
What does AI have to do with Human Rights?
axbom
PRO
1
2.4k
It's Worth the Effort
3n
188
29k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
540
Between Models and Reality
mayunak
4
460
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.8k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Automating Front-end Workflow
addyosmani
1369
210k
Into the Great Unknown - MozCon
thekraken
41
2.7k
Color Theory Basics | Prateek | Gurzu
gurzu
1
460
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
430
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!!!?
໊લม͑·͢ (΄ͱΜͲ࡞ͬͯͳ͍Ͱ͕͢)