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
Collectionの罠
Search
andfactory, inc.
October 25, 2018
Programming
0
200
Collectionの罠
417_72kiさんが10.25and factory beer bash #4 若手エンジニアLT - スマホアプリ開発あるある編で登壇した資料です。
andfactory, inc.
October 25, 2018
Tweet
Share
More Decks by andfactory, inc.
See All by andfactory, inc.
UIViewのレイアウト更新メソッドを整理する
andfactory
0
440
Android開発初心者にありがちなこと
andfactory
0
250
Codableあるある
andfactory
0
210
Androidエンジニアあるある
andfactory
0
250
Cocoa Touch Frameworkあるある
andfactory
0
350
iOS開発に慣れてきた時にやりがちあるある
andfactory
0
210
社内勉強会資料 - スクラム・アジャイル開発
andfactory
0
620
Other Decks in Programming
See All in Programming
Serena MCPのすすめ
wadakatu
4
1k
What's new in Spring Modulith?
olivergierke
1
150
monorepo の Go テストをはやくした〜い!~最小の依存解決への道のり~ / faster-testing-of-monorepos
convto
2
500
Le côté obscur des IA génératives
pascallemerrer
0
150
Go言語はstack overflowの夢を見るか?
logica0419
0
340
Railsだからできる 例外業務に禍根を残さない 設定設計パターン
ei_ei_eiichi
0
910
あなたとKaigi on Rails / Kaigi on Rails + You
shimoju
0
160
After go func(): Goroutines Through a Beginner’s Eye
97vaibhav
0
400
NixOS + Kubernetesで構築する自宅サーバーのすべて
ichi_h3
0
910
Swift Concurrency - 状態監視の罠
objectiveaudio
2
520
TFLintカスタムプラグインで始める Terraformコード品質管理
bells17
2
180
詳しくない分野でのVibe Codingで困ったことと学び/vibe-coding-in-unfamiliar-area
shibayu36
3
5.1k
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.9k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
How to Think Like a Performance Engineer
csswizardry
27
2k
Embracing the Ebb and Flow
colly
88
4.8k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.7k
What's in a price? How to price your products and services
michaelherold
246
12k
We Have a Design System, Now What?
morganepeng
53
7.8k
Rails Girls Zürich Keynote
gr2m
95
14k
Scaling GitHub
holman
463
140k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Transcript
COLLECTION操作の罠 Navigate : Space / Arrow Keys | - Menu
| - Fullscreen | - Overview | - Blackout | - Speaker | - Help M F O B S ? 1 / 28
Profile Takuhiro Muta iOS Enginner@iRidge inc. Twitter/Qiita: @417_72ki GitHub: 417-72KI
[ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 2 / 28
本題 [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 3 / 28
Objective-CからSwift JavaからKotlin(またはJava8) になるに従って コレクション操作が大幅に楽になりました [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ]
4 / 28
昔 Objective-C Java(~7) NSArray *array = ... NSArray *evenArray =
[NSMutableArray new]; for (NSNumber *num in array) { if(num.integerValue % 2 == 0) { [evenArray addObject:num]; } } List<Integer> list = ... List<Integer> evenList = new ArrayList<>(); for(int num: list) { if(num % 2 == 0) { evenList.add(num); } } [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 5 / 28
今 Swift Kotlin let list = [1,2,3,4,...] let evenList =
list.filter { $0 % 2 == 0 } val array = arrayOf(1,2,3,4,...) val evenList = array.filter { it % 2 == 0 }.toTypedArray() [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 6 / 28
ちょっと複雑な例 [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 7 / 28
let range = 1...10 let newList = range.filter { $0
% 2 == 0 } .map { $0 / 2 } .map { $0 * 5 } .filter { $0 > 10 } [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 8 / 28
map と filter の定義 public func map<T>(_ transform: (Element) throws
-> T) rethrows -> public func filter(_ isIncluded: (Element) throws -> Bool) rethrow [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 9 / 28
どちらも配列を返している... [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 10 / 28
分かりやすいように画面出力してみましょう func multiply(_ i: Int) -> (Int) -> Int {
return { print($0, "*" , i, "=", $0 * i) return $0 * i } } func divide(_ i: Int) -> (Int) -> Int { return { print($0, "/" , i, "=", $0 / i) return $0 / i } } func isOdd() -> (Int) -> Bool { return { print($0, "odd?" , $0 % 2 == 0) return $0 % 2 == 0 [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 11 / 28
実行結果 1 odd? false 2 odd? true 3 odd? false
4 odd? true 5 odd? false 6 odd? true 7 odd? false 8 odd? true 9 odd? false 10 odd? true 2 / 2 = 1 4 / 2 = 2 6 / 2 = 3 8 / 2 = 4 10 / 2 = 5 1 * 5 = 5 [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 12 / 28
イメージ List<Integer> l = new ArrayList<>(); for (int n: array)
if (isOdd(n)) l.add(n); List<Integer> l2 = new ArrayList<>(); for (int n: l) l2.add(divide(n, 2)); List<Integer> l3 = new ArrayList<>(); for (int n: l2) l3.add(multiply(n, 5)); List<Integer> result = new ArrayList<>(); for (int n: l3) if (isMoreThan(n, 10)) result.add(n); [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 13 / 28
ループ回しすぎぃ! [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 14 / 28
これが要素数100万とかの配列になったら... [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 15 / 28
((((;゚Д゚)))) [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 16 / 28
そこで [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 17 / 28
Swift → LazyCollection Kotlin → Sequence [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections
] 18 / 28
どちらも遅延評価で実行してくれる [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 19 / 28
さっきの例 これを var list = 1...10 let newList = list.filter(isOdd())
.map(divide(2)) .map(multiply(5)) .filter(isMoreThan(10)) [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 20 / 28
こうじゃ var list = 1...10 let newList = list.lazy .filter(isOdd())
.map(divide(2)) .map(multiply(5)) .filter(isMoreThan(10)) .reduce(into: [Int] ()) { (array: inout [Int], i: Int) in arr [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 21 / 28
実行結果 1 odd? false 2 odd? true 2 / 2
= 1 1 * 5 = 5 5 > 10 : false 3 odd? false 4 odd? true 4 / 2 = 2 2 * 5 = 10 10 > 10 : false 5 odd? false 6 odd? true 6 / 2 = 3 3 * 5 = 15 15 > 10 : true 7 odd? false [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 22 / 28
イメージ 1ループで回す感じに List<Integer> result = new ArrayList<>(); for (int n:
array) { if (!isOdd(n)) continue; int n2 = divide(n, 2); int n3 = multiply(n2, 5) if (isMoreThan(n3, 10)) result.add(n3); } [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 23 / 28
ちなみにKotlinの場合は array.asSequence() をmapとかfilterとかの前に 呼ぶだけ [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ]
24 / 28
どういう時に有効? 大きいデータから複数の条件に合致する数件だけを 抽出する [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 25
/ 28
Before 実行時間: 0.7900744676589966 let range = 1...1000 let result1 =
range .filter(isOdd()) .map(divide(2)) .map(multiply(5)) .filter(isMoreThan(10)) .prefix(5) [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 26 / 28
After 実行時間: 0.00704646110534668 let range = 1...1000 let result2 =
range.lazy .filter(isOdd()) .map(divide(2)) .map(multiply(5)) .filter(isMoreThan(10)) .prefix(5) .reduce(into: [Int] ()) { (array: inout [Int], i: Int) in arr [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 27 / 28
まとめ 膨大なコレクションを操作する際は遅延評価の検討 を! [ GitPitch @ github/417-72KI/Slides4LT/trap-of-collections ] 28
/ 28