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
940
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
520
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
110
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
280
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
420
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
680
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
610
UICollectionView Compositional Layout
usamik26
0
670
Coding Swift with Visual Studio Code and Docker
usamik26
0
440
Swift Extension for Visual Studio Code
usamik26
2
890
Other Decks in Programming
See All in Programming
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
370
Kubernetes for Data Engineers: Building Scalable, Reliable Data Pipelines
sucitw
1
200
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
240
僕がつくった48個のWebサービス達
yusukebe
18
17k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
440
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
220
飲食業界向けマルチプロダクトを実現させる開発体制とリアルな現状
hiroya0601
1
400
Vitest Browser Mode への期待 / Vitest Browser Mode
odanado
PRO
2
1.7k
外部システム連携先が10を超えるシステムでのアーキテクチャ設計・実装事例
kiwasaki
1
230
qmuntal/stateless のススメ
sgash708
0
120
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
1.7k
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
167
14k
Music & Morning Musume
bryan
46
6.1k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
27
1.9k
Optimizing for Happiness
mojombo
376
69k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Product Roadmaps are Hard
iamctodd
PRO
48
10k
Speed Design
sergeychernyshev
24
570
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
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 Λ্खʹ׆༻͠Α͏