Swift flatMap
by
USAMI Kosuke
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
flatMap ͷ USAMI Kosuke Fenrir Inc.
Slide 2
Slide 2 text
͡Ίʹ • Swi% ͷ flatMap ʹ͍ͭͯ͠·͢ • flatMap ͦͷͷ͍Ζ͍Ζͳݴޠʹ͋Γ·͢
Slide 3
Slide 3 text
flatMap • Α͘Θ͔Βͳ͍Πϝʔδ͕͋Δʁ • ίʔυ͕ಡΈͮΒ͍ʁ • map ͯ͠ fla'en ͢Δͭʁ • ؔܕʁ Ϟφυʁ
Slide 4
Slide 4 text
flatMap • ݸਓతʹɺؾָʹ͍͍͑ͱࢥ͏
Slide 5
Slide 5 text
Op#onal ͷ flatMap
Slide 6
Slide 6 text
Op#onal ͷ flatMap • มલ : Optional • มॲཧ : T -> Optional • flatMap : Optional -> Optional • มલ͕ non-nil ͳΒมॲཧΛద༻͢Δ • มલ͕ nil ͳΒมॲཧΛద༻͠ͳ͍
Slide 7
Slide 7 text
ྫ • มલ : Optional • มॲཧ : String -> Optional : { Int($0) } • string.flatMap { Int($0) } • "123" -> Int("123") -> 123 • "abc" -> Int("abc") -> nil • nil -> nil
Slide 8
Slide 8 text
nil ͷೖྗΛҙࣝ͠ͳ͍͍ͯ͘ // string Optional ͚ͩͲɾɾɾ let string: String? = "123" let integer: Int? = string.flatMap { Int($0) // $0 unwrap ͞Ε͍ͯΔ }
Slide 9
Slide 9 text
if let Ͱॻ͘ͱʹͳΔ let string: String? = "123" let integer: Int? if let string = string { integer = Int(string) } else { integer = nil }
Slide 10
Slide 10 text
Op#onal chaining ෩ʹ͏ // Optional chaining string?.isEmpty // ಉ༷ʹ͜Μͳͷ͕ཉ͘͠ͳΔʁ string?.toInt // ΘΓʹ͜͏͢Δ string.flatMap { Int($0) }
Slide 11
Slide 11 text
Array ͷ flatMap
Slide 12
Slide 12 text
Array ͷ flatMap • 2 छྨͷ flatMap ͕ଘࡏ͢Δ
Slide 13
Slide 13 text
Array ͷ flatMap (1) • มલ : Array • มॲཧ : T -> Array • flatMap : Array -> Array
Slide 14
Slide 14 text
ྫ • มલ : Array • มॲཧ : Int -> Array : { [$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]
Slide 15
Slide 15 text
Op#onal ͱ Array ͷ flatMap ൺֱ • มલ : Optional • มॲཧ : T -> Optional • flatMap : Optional -> Optional • มલ : Array • มॲཧ : T -> Array • flatMap : Array -> Array
Slide 16
Slide 16 text
flatMap ͷجຊܗ • ܕ T U ͕ೖͬͨԿΒ͔ͷശʢBoxʣ͕͋Δͱ͖ʹ • มલ : Box • มॲཧ : T -> Box • flatMap : Box -> Box
Slide 17
Slide 17 text
Array ͷ flatMap (2) • มલ : Array • มॲཧ : T -> Optional • flatMap : Array -> Array • มॲཧͷ݁Ռ͕ non-nil ͷͷ͕ Array ͷཁૉʹͳΔ • มॲཧͷ݁Ռ͕ nil ͷͷࣺͯΒΕΔ
Slide 18
Slide 18 text
ྫ • มલ : Array • มॲཧ : String -> Optional : { Int($0) } • ["123", "abc"].flatMap { Int($0) } • "123" -> 123, "abc" -> nil • [123]
Slide 19
Slide 19 text
Array ͷ 2 ͭͷ flatMap ͷؔ • flatMap (2) flatMap (1) Ͱ࣮ݱͰ͖Δ • non-nil Λฦ͢ΘΓʹ1ཁૉͷ Array Λฦ͢ • nil Λฦ͢ΘΓʹۭͷ Array Λฦ͢ • flatMap (2) ͷํ͕ίʔυ͕γϯϓϧʹͳΔ • flatMap (1) ࣮͋Μ·ΓΘͳ͍
Slide 20
Slide 20 text
׆༻ྫ • ["A", "B", nil, "C"] : [String?] • ͔͜͜Β nil ΛऔΓআ͍ͨ [String] ͕ཉ͍͠ // flatMap ͳ͠ͳΒɺ͜Μͳײ͡Ͱॻ͘ʁ ["A", "B", nil, "C"] .filter({ $0 != nil }) as! [String]
Slide 21
Slide 21 text
׆༻ྫ // ୯ʹ͜͏ॻ͚ྑ͍ ["A", "B", nil, "C"].flatMap { $0 } • มલ : Array> • มॲཧ : Optional -> Optional • flatMap : Array> -> Array
Slide 22
Slide 22 text
flatMap Λ׆༻͢Δ໘ • Op$onal ͱͷ૬ੑ͕ྑ͍ • Op$onal Λѻ͏ॲཧ͕͋Δͱ͖ʹ͑ͳ͍͔ߟ͑Δͱྑ͍
Slide 23
Slide 23 text
ࢀߟɿଞʹ flatMap ͕͑Δͷ • Result ܕʢan+typical/Resultʣ • Op+onal ͱ΄΅ಉ͡ʢnil ͷΘΓʹ۩ମతͳ Error Λฦ͢ʣ • Observable ܕʢRxSwi:ʣ • ඇಉظετϦʔϜɺ్தͰΤϥʔʹͳͬͨ߹ͷରԠ͕༰қ
Slide 24
Slide 24 text
flatMap Λ্खʹ׆༻͠Α͏