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
Swift flatMap 2
Search
USAMI Kosuke
April 12, 2017
Programming
0
73
Swift flatMap 2
Workshop in Fenrir Inc.
USAMI Kosuke
April 12, 2017
Tweet
Share
More Decks by USAMI Kosuke
See All by USAMI Kosuke
Onsager代数とその周辺 / Onsager algebra tsudoi
usamik26
0
600
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
160
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
310
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
460
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
720
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
650
UICollectionView Compositional Layout
usamik26
0
750
Coding Swift with Visual Studio Code and Docker
usamik26
0
490
Swift Extension for Visual Studio Code
usamik26
2
1k
Other Decks in Programming
See All in Programming
Going Structural with Named Tuples
bishabosha
0
150
AIエージェントを活用したアプリ開発手法の模索
kumamotone
1
730
20250326_生成AIによる_レビュー承認システムの実現.pdf
takahiromatsui
15
4.6k
いまさら聞けない生成AI入門: 「生成AIを高速キャッチアップ」
soh9834
11
3.3k
2025/3/18 サービスの成長で生じる幅広いパフォーマンスの問題を、 AIで手軽に解決する
shirahama_x
0
160
小さく段階的リリースすることで深夜メンテを回避する
mkmk884
2
120
Devinのメモリ活用の学びを自社サービスにどう組み込むか?
itarutomy
0
1.4k
List とは何か? / PHPerKaigi 2025
meihei3
0
310
Denoでフロントエンド開発 2025年春版 / Frontend Development with Deno (Spring 2025)
petamoriken
1
1.3k
AI時代のプログラミング教育 / programming education in ai era
kishida
22
20k
goにおける コネクションプールの仕組み を軽く掘って見た
aronokuyama
0
110
remix + cloudflare workers (DO) docker上でいい感じに開発する
yoshidatomoaki
0
110
Featured
See All Featured
Docker and Python
trallard
44
3.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
460
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Optimising Largest Contentful Paint
csswizardry
34
3.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
8
690
Designing Experiences People Love
moore
140
23k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
How STYLIGHT went responsive
nonsquared
99
5.4k
Building Adaptive Systems
keathley
40
2.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
134
33k
Thoughts on Productivity
jonyablonski
69
4.5k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
Transcript
flatMap ͷ USAMI Kosuke Fenrir Inc.
͡Ίʹ • Swi% ͷ flatMap ʹ͍ͭͯ͠·͢ • flatMap ͦͷͷ͍Ζ͍Ζͳݴޠʹ͋Γ·͢ •
Op-onal ͷ flatMap • Array ͷ flatMap • Observable ͷ flatMap
flatMap ͷҹɾɾɾ • Α͘Θ͔Βͳ͍ͷͱ͍͏ҹ͕͋Δʁ • ίʔυ͕ಡΈͮΒ͘ͳΔʁ • ͳΜ͔ map ͯ͠
fla'en ͢Δͭʁ • ؔܕʁ Ϟφυʁ
flatMap • ݸਓతʹɺؾָʹ͍͍͑ͱࢥ͏ • ͬͯ׳Εͯ͠·͑ɺ͍͠ͷͰͳ͍
Op#onal ͷ flatMap
Op#onal ͷ flatMap • มલ : Optional<T> • มॲཧ :
T -> Optional<U> • flatMap : Optional<T> -> Optional<U> • มલ͕ non-nil ͳΒมॲཧΛద༻͢Δ • มલ͕ nil ͳΒมॲཧΛద༻͠ͳ͍
ྫ • มલ : Optional<String> • มॲཧ : String ->
Optional<Int> : { Int($0) } • string.flatMap { Int($0) } • "123" -> Int("123") -> 123 • "abc" -> Int("abc") -> nil • nil -> nil
nil ͷೖྗΛҙࣝ͠ͳ͍͍ͯ͘ // string Optional ͚ͩͲɾɾɾ let string: String?
= "123" let integer: Int? = string.flatMap { Int($0) // $0 unwrap ͞Ε͍ͯΔ }
if let Ͱॻ͘ͱʹͳΔ let string: String? = "123" let integer:
Int? if let string = string { integer = Int(string) } else { integer = nil }
Op#onal chaining ෩ʹ͏ // Optional chaining string?.isEmpty // ಉ༷ʹ͜Μͳͷ͕ཉ͘͠ͳΔʁ string?.toInt
// ΘΓʹ͜͏͢Δ string.flatMap { Int($0) }
Array ͷ flatMap
Array ͷ flatMap • 2 छྨͷ flatMap ͕ଘࡏ͢Δ
Array ͷ flatMap (1) • มલ : Array<T> • มॲཧ
: T -> Array<U> • flatMap : Array<T> -> Array<U>
ྫ • มલ : Array<Int> • มॲཧ : Int ->
Array<Int> : { [$0, $0 * 10] } • [1, 2, 3].flatMap { [$0, $0 * 10] } • 1 -> [1, 10], 2 -> [2, 20], 3 -> [3, 30] • [1, 10, 2, 20, 3, 30]
Op#onal ͱ Array ͷ flatMap ൺֱ • มલ : Optional<T>
• มॲཧ : T -> Optional<U> • flatMap : Optional<T> -> Optional<U> • มલ : Array<T> • มॲཧ : T -> Array<U> • flatMap : Array<T> -> Array<U>
flatMap ͷجຊܗ • ܕ T U ͕ೖͬͨԿΒ͔ͷശʢBoxʣ͕͋Δͱ͖ʹ • มલ
: Box<T> • มॲཧ : T -> Box<U> • flatMap : Box<T> -> Box<U>
Array ͷ flatMap (2) • มલ : Array<T> • มॲཧ
: T -> Optional<U> • flatMap : Array<T> -> Array<U> • มॲཧͷ݁Ռ͕ non-nil ͷͷ͕ Array ͷཁૉʹͳΔ • มॲཧͷ݁Ռ͕ nil ͷͷࣺͯΒΕΔ
ྫ • มલ : Array<String> • มॲཧ : String ->
Optional<Int> : { Int($0) } • ["123", "abc"].flatMap { Int($0) } • "123" -> 123, "abc" -> nil • [123]
Array ͷ 2 ͭͷ flatMap ͷؔ • flatMap (2)
flatMap (1) Ͱ࣮ݱͰ͖Δ • non-nil Λฦ͢ΘΓʹ1ཁૉͷ Array Λฦ͢ • nil Λฦ͢ΘΓʹۭͷ Array Λฦ͢ • flatMap (2) ͷํ͕ίʔυ͕γϯϓϧʹͳΔ • flatMap (1) ࣮͋Μ·ΓΘͳ͍
׆༻ྫ • ["A", "B", nil, "C"] : [String?] • ͔͜͜Β
nil ΛऔΓআ͍ͨ [String] ͕ཉ͍͠ // flatMap ͳ͠ͳΒɺ͜Μͳײ͡Ͱॻ͘ʁ ["A", "B", nil, "C"] .filter({ $0 != nil }) as! [String]
׆༻ྫ // ୯ʹ͜͏ॻ͚ྑ͍ ["A", "B", nil, "C"].flatMap { $0 }
• มલ : Array<Optional<String>> • มॲཧ : Optional<String> -> Optional<String> • flatMap : Array<Optional<String>> -> Array<String>
flatMap Λ׆༻͢Δ໘ • Op$onal ͱͷ૬ੑ͕ྑ͍ • Op$onal Λѻ͏ॲཧ͕͋Δͱ͖ʹ͑ͳ͍͔ߟ͑Δͱྑ͍
ࢀߟɿଞʹ flatMap ͕͑Δͷ • Result ܕʢan+typical/Resultʣ • Op+onal ͱ΄΅ಉ͡ʢnil ͷΘΓʹ۩ମతͳ
Error Λฦ͢ʣ • Observable ܕʢRxSwi:ʣ • ඇಉظετϦʔϜɺ్தͰΤϥʔʹͳͬͨ߹ͷରԠ͕༰қ
Observable ͷ flatMap
Observable ͷ flatMap • มલ : Observable<T> • มॲཧ :
T -> Observable<U> • flatMap : Observable<T> -> Observable<U> • มલ͕ .next ͳΒมॲཧΛద༻͢Δ • มલ͕ .error / .complete ͳΒมॲཧΛద༻͠ͳ͍
ྫ Observable.of("123", "abc") .flatMap { string -> Observable<Int> in if
let integer = Int(string) { return Observable.just(integer) } else { return Observable.empty() } } // -> [.next(123), .completed] • ิɿ্هͷ if let Op*onal ͷ flatMap Ͱॻ͘ͱεοΩϦ͢ Δ
Observable ͷ flatMap ͷҙ • มॲཧ : T -> Observable<U>
• flatMap : Observable<T> -> Observable<U> • มॲཧʹΑͬͯੜ͞Εͨ Observable<U> ΛɺflatMap Ͳ͏݁߹ͯ͠࠷ऴతͳ Observable<U> ʹ͢Δ͔ʁ • Op*onal Array ͷ߹ʹฆΕͳ͔ͬͨ • Observable ݁߹ͷํ͕ෳ͋Δ
flatMap Ͱͷ݁߹ॲཧ • flatMap Observable Λ merge Ͱ݁߹͢Δʢconcat Ͱͳ
͍ʣ
ଞͷ݁߹Λߦ͏ flatMap • flatMapLatest switchLatest Ͱ݁߹͢Δ
flatMap Λ্खʹ׆༻͠Α͏