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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
630
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
920
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
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
160
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.1k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
610
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
360
すこし踏み込む CancellationToken
htkym
2
1.7k
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
22
22k
contenteditable と日本語入力に向き合う
colorful12
0
190
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
540
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
370
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
130
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
350
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Making Projects Easy
brettharned
120
6.8k
Six Lessons from altMBA
skipperchong
29
4.5k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
370
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
360
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
480
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
58k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
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!!!?
໊લม͑·͢ (΄ͱΜͲ࡞ͬͯͳ͍Ͱ͕͢)