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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
USAMI Kosuke
April 12, 2017
Programming
0
89
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
680
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
230
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
370
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
530
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
790
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
710
UICollectionView Compositional Layout
usamik26
0
850
Coding Swift with Visual Studio Code and Docker
usamik26
0
550
Swift Extension for Visual Studio Code
usamik26
2
1.1k
Other Decks in Programming
See All in Programming
技術検証結果の整理と解析をAIに任せよう!
keisukeikeda
0
120
CSC307 Lecture 13
javiergs
PRO
0
320
Codex の「自走力」を高める
yorifuji
0
1.2k
DSPy入門 Pythonで実現する自動プロンプト最適化 〜人手によるプロンプト調整からの卒業〜
seaturt1e
1
690
ベクトル検索のフィルタを用いた機械学習モデルとの統合 / python-meetup-fukuoka-06-vector-attr
monochromegane
2
390
AIコーディングの理想と現実 2026 | AI Coding: Expectations vs. Reality 2026
tomohisa
0
1.2k
Ruby and LLM Ecosystem 2nd
koic
1
620
猫の手も借りたい!ので AIエージェント猫を作って社内に放した話 Claude Code × Container Lambda の Slack Bot "DevNeko"
naramomi7
0
260
How to stabilize UI tests using XCTest
akkeylab
0
120
Windows on Ryzen and I
seosoft
0
260
S3ストレージクラスの「見える」「ある」「使える」は全部違う ─ 体験から見た、仕様の深淵を覗く
ya_ma23
0
400
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
230
Featured
See All Featured
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
480
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.3k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
220
How to train your dragon (web standard)
notwaldorf
97
6.6k
YesSQL, Process and Tooling at Scale
rocio
174
15k
The Language of Interfaces
destraynor
162
26k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
980
Mobile First: as difficult as doing things right
swwweet
225
10k
How Software Deployment tools have changed in the past 20 years
geshan
0
32k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
630
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
280
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 Λ্खʹ׆༻͠Α͏