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
Search
USAMI Kosuke
February 28, 2017
Programming
4
1k
Swift flatMap
https://kanmoba.connpass.com/event/51628/
USAMI Kosuke
February 28, 2017
Tweet
Share
More Decks by USAMI Kosuke
See All by USAMI Kosuke
Onsager代数とその周辺 / Onsager algebra tsudoi
usamik26
0
630
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
180
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
330
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
480
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
740
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
670
UICollectionView Compositional Layout
usamik26
0
780
Coding Swift with Visual Studio Code and Docker
usamik26
0
510
Swift Extension for Visual Studio Code
usamik26
2
1k
Other Decks in Programming
See All in Programming
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
170
来たるべき 8.0 に備えて React 19 新機能と React Router 固有機能の取捨選択とすり合わせを考える
oukayuka
2
830
すべてのコンテキストを、 ユーザー価値に変える
applism118
2
520
GoのWebAssembly活用パターン紹介
syumai
3
10k
A comprehensive view of refactoring
marabesi
0
970
20250628_非エンジニアがバイブコーディングしてみた
ponponmikankan
0
280
AWS CDKの推しポイント 〜CloudFormationと比較してみた〜
akihisaikeda
3
300
「ElixirでIoT!!」のこれまでとこれから
takasehideki
0
370
Blazing Fast UI Development with Compose Hot Reload (droidcon New York 2025)
zsmb
1
170
Cline指示通りに動かない? AI小説エージェントで学ぶ指示書の書き方と自動アップデートの仕組み
kamomeashizawa
1
570
Claude Codeの使い方
ttnyt8701
1
130
Team operations that are not burdened by SRE
kazatohiei
1
100
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
69
11k
The Straight Up "How To Draw Better" Workshop
denniskardys
233
140k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
16
940
Done Done
chrislema
184
16k
Facilitating Awesome Meetings
lara
54
6.4k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
34k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
The Cult of Friendly URLs
andyhume
79
6.4k
Side Projects
sachag
455
42k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
Transcript
flatMap ͷ USAMI Kosuke Fenrir Inc.
͡Ίʹ • Swi% ͷ flatMap ʹ͍ͭͯ͠·͢ • 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:ʣ • ඇಉظετϦʔϜɺ్தͰΤϥʔʹͳͬͨ߹ͷରԠ͕༰қ
flatMap Λ্खʹ׆༻͠Α͏