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
970
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
580
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
150
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
300
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
450
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
710
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
640
UICollectionView Compositional Layout
usamik26
0
720
Coding Swift with Visual Studio Code and Docker
usamik26
0
480
Swift Extension for Visual Studio Code
usamik26
2
970
Other Decks in Programming
See All in Programming
お前もAI鬼にならないか?👹Bolt & Cursor & Supabase & Vercelで人間をやめるぞ、ジョジョー!👺
taishiyade
5
3.9k
[JAWS-UG横浜 #80] うわっ…今年のServerless アップデート、少なすぎ…?
maroon1st
1
180
ASP. NET CoreにおけるWebAPIの最新情報
tomokusaba
0
360
Linux && Docker 研修/Linux && Docker training
forrep
24
4.5k
sappoRo.R #12 初心者セッション
kosugitti
0
240
DROBEの生成AI活用事例 with AWS
ippey
0
130
[JAWS-UG横浜 #79] re:Invent 2024 の DB アップデートは Multi-Region!
maroon1st
1
140
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
870
定理証明プラットフォーム lapisla.net
abap34
1
1.8k
ISUCON14公式反省会LT: 社内ISUCONの話
astj
PRO
0
190
AWS Lambda functions with C# 用の Dev Container Template を作ってみた件
mappie_kochi
0
240
Amazon S3 TablesとAmazon S3 Metadataを触ってみた / 20250201-jawsug-tochigi-s3tables-s3metadata
kasacchiful
0
120
Featured
See All Featured
Become a Pro
speakerdeck
PRO
26
5.1k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
Making Projects Easy
brettharned
116
6k
Designing Experiences People Love
moore
139
23k
Building Your Own Lightsaber
phodgson
104
6.2k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
BBQ
matthewcrist
86
9.5k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
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 Λ্खʹ׆༻͠Α͏