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
いつか役にたつかもしれないコレクション周りの5つのこと
Search
RyotaMurohoshi
July 05, 2016
Programming
2
750
いつか役にたつかもしれないコレクション周りの5つのこと
2016//07/05(火)に開催された「Kotlin 勉強会 #1 - 初めての Kotlin」で発表した資料です
http://connpass.com/event/33584/
RyotaMurohoshi
July 05, 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
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
330
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
subpath importsで始めるモック生活
10tera
0
300
Arm移行タイムアタック
qnighy
0
320
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Jakarta EE meets AI
ivargrimstad
0
630
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Thoughts on Productivity
jonyablonski
67
4.3k
The Cult of Friendly URLs
andyhume
78
6k
Statistics for Hackers
jakevdp
796
220k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
A Tale of Four Properties
chriscoyier
156
23k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Transcript
ผʹܶతʹมΘΒͳ͍͚ΕͲ ͍͔ͭʹ͔ͨͭ͠Εͳ͍ ίϨΫγϣϯपΓͷ5ͭͷ͜ͱ @RyotaMurohoshi 2016/07/05(Ր)*͡ΊͯͷKotlin
ͦͷ1 ঢ়گʹԠͯ͡SequenceΛ͏͖ͳ݅
100000ߦͷJSON'LinesϑΝΠϧ ͜ΕΛ1ߦͣͭdataΫϥεͷΦϒδΣΫτʹม͠ ͋Δ݅Λຬͨ͢ΦϒδΣΫτΛ ࠷ॳ͔Β10͚݅ͩ֬ೝද͍ࣔͨ͠
List<T>ͷ߹
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) }
࠷ޙ·ͰϑΝΠϧΛಡΜͰ͠·͏
Sequence<T>ͷ߹
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) }
ϑΝΠϧΛ࠷ޙ·Ͱಡ·ͳ͍
ৄ͘͜͠ͷࢿྉͰʂ h"ps:/ /speakerdeck.com/ryotamurohoshi/sequencefalsehua8kotlin8dot8sequences8dot8sequence
ͦͷ2 ཁૉ͕ۭͷ࣌ͷaverageϝιου͕ 0.0Λฦͯ͠͠·͏݅
averageϝιουͱʁ List<Int>ͱ͔ͷฏۉܭࢉϝιου
͡Ό͋ɺཁૉ͕ۭͷ࣌ʁ
·͔͞ͷ0.0!
ͳͥͩʂʁ
͜Ε͡Όܭࢉ݁Ռ͕0.0ͩͬͨͷ͔ɺ ཁૉ͕ۭ͔ͩΒ0.0ͬͨͷ͔ ۠ผԽ͔ͭͳ͍ʂʂʂ
ͦͦͳͥDouble?ܕʹ͠ͳ͔ͬͨͷ͔ʁ maxminInt?Λฦ͢ͷʹʂ ͘͠ྫ֎Λ͛ͯཉ͍͠
ͦͷ3 Ωʔ͕ॏෳͨ͠ࡍͷassociateܥϝιου͕ɺ ྫ֎Λ͛ͳ͍݅
associateܥͷϝιουͱʁ Listͱ͔͔ΒMapΛͭ͘Δϝιου
data class Person( val id: Long,ɹval name: String, val teamId:
Long) // ॏෳidͷPersonଘࡏ͠ͳ͍ // ಉ͡teamIdΛ࣋ͭPersonଘࡏ͠͏Δ val personList : List<Person>ɹ= loadPersonList() // id͕ॏෳ͠ͳ͍ͷͰແ͠ val personMap : Map<Long, Person> = personList.associateBy { it.id } // ʲʂʂʂʳಉ͡teamIdΛͭཁૉ͕personListʹଘࡏ͠͏Δʂʂʂʂʂ val teamMap : Map<Long, Person> = personList.associateBy { it.teamId }
͠Key͕ॏෳͨ͠Βʁ
ޙউʂ ͋ͱ͕ͪ:ޙΖͷཁૉͰ্ॻ͖
ݸਓతʹྫ֎Λ͛ͯཉ͍͠
ͦͦMapΛͭ͘ͷͰ͋ΕɺKeyҰҙͳͷͳͣ Key͕ॏෳ͢ΔͷΛఆ͢ΔͳΒɺgroupByΛ͏͖
ಥવͷએ
C#ͷLINQޓͷϥΠϒϥϦ KotLinq࡞͍ͬͯ·͢ʂʂʂ h"ps:/ /github.com/RyotaMurohoshi/KotLinq
ࠓճͷTips KotLinqΛ࡞͍ͬͯΔࡍͷݟͰ͢
ͦͷ4 σϑΥϧτΠϯϙʔτ֦ுؔΛ ্ॻ͖Ͱ͖Δ݅
ࣗ࡞ϥΠϒϥϦKotLinqͷʹ takeͬͯϝιου͕͋Γ·͢ʂ
KotLinqͷtakeϝιου fun <TSource> Iterable<TSource>.take(count: Int): Sequence<TSource> = asSequence().take(count)
Μʁ
ඪ४ؔʹtakeͬͯ͋ͬͨΑ͏ͳ? fun <T> Iterable<T>.take(n: Int): List<T>
ඪ४ͰΠϯϙʔτͰ͖Δ֦ுؔ Λ্ॻ͖Ͱ͖Δ݅ʂ
ͦͷ5 ϝιουͷΦʔόʔϩʔυͱit͕ ૬ੑѱ͍݅ (͜Ε͋Μ·ΓίϨΫγϣϯؔͳ͍)
ࣹӨϝιου!ͱ!ΠϯσοΫε͖ࣹӨϝιουೋͭ࡞Γ͍ͨ // ࣹӨϝιου fun <TSource, TResult> Sequence<TSource> .select(selector: (TSource) ->
TResult): Sequence<TResult> // ΠϯσοΫε͖ࣹӨϝιου fun <TSource, TResult> Sequence<TSource> .select(selector: (TSource, Int) -> TResult): Sequence<TResult>
ࣹӨϝιου!ͱ!ΠϯσοΫε͖ࣹӨϝιου ҰҙʹܾΊΒΕͳ͍͔ΒίϯύΠϧΤϥʔ sequenceOf(1, 2, 3).select { it * 2 }
KotLinq(ͰɺΠϯσοΫε͖ࣹӨϝιουΛ ผ໊ʹ͠·ͨ͠ sequence.select { it * it }!ͱ͔͍ͨ͠͠
·ͱΊ • ঢ়گʹԠͯ͡SequenceΛ͏͖ͳ݅ • ཁૉ͕ۭͷ࣌ͷaverageϝιου͕0.0Λฦͯ͠͠·͏݅ • Ωʔ͕ॏෳͨ͠ࡍͷassociateܥϝιου͕ྫ֎Λ͛ͳ͍݅ • σϑΥϧτΠϯϙʔτ֦ுؔΛ্ॻ͖Ͱ͖Δ݅ •
ϝιουͷΦʔόʔϩʔυͱit͕૬ੑѱ͍݅
·ͱΊ KotLinqʹΛ