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のmapからその次へ
Search
oyuk
August 04, 2016
Programming
1
1.3k
Swiftのmapからその次へ
oyuk
August 04, 2016
Tweet
Share
More Decks by oyuk
See All by oyuk
Shorebird について
oyuk
0
350
Material designのWindow size classについて
oyuk
0
970
Swiftでなんで[weak self]するのか?
oyuk
2
4.7k
LLVMについて調べた
oyuk
0
170
Other Decks in Programming
See All in Programming
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.7k
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
RubyLSPのマルチバイト文字対応
notfounds
0
120
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
250
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
Macとオーディオ再生 2024/11/02
yusukeito
0
370
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
CSC509 Lecture 09
javiergs
PRO
0
140
Featured
See All Featured
Designing for humans not robots
tammielis
250
25k
Building an army of robots
kneath
302
43k
Documentation Writing (for coders)
carmenintech
65
4.4k
Fireside Chat
paigeccino
34
3k
Producing Creativity
orderedlist
PRO
341
39k
Automating Front-end Workflow
addyosmani
1366
200k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Facilitating Awesome Meetings
lara
50
6.1k
Transcript
Swiftͷmap͔Βͦͷ࣍ 2016/08/04 oyuk(@oydku)
ࣗݾհ • oyuk(@oydku) • iosΞϓϦΤϯδχΞ!
Swiftͷmap map͕͑ΔͭΒ͍͔ͭ͋͘ΔΑͶ • Optional (Enum) • Array (CollectionType) • ObservableType
(RxSwift) ଞʹ͔͋ͬͨͶɻ͋ͬͨΒڭ͑ͯ
mapͷఆٛΛݟΔ Optional func map<U>(@noescape f: (Wrapped) throws -> U) rethrows
-> U? Array func map<T>(@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T] ObservableType public func map<R>(selector: Self.E throws -> R) -> RxSwift.Observable<R>
ڞ௨͋ΔΑͶ!
͍͍ͩͨ͜Μͳײ͡ func map<U>(f:T -> U) -> ͋Δ<U>
͍͍ͩͨ͜Μͳײ͡ func map<U>(f:T -> U) -> ͋Δ<U> Optional func map<U>(@noescape
f: (Wrapped) throws -> U) rethrows -> U? • Wrapped->UͷؔΛWrappedʹద༻ͯ͠U?Λฦ͢ • ͋Δ<U>͜͜ͰU?
͍͍ͩͨ͜Μͳײ͡ func map<U>(f:T -> U) -> ͋Δ<U> Array func map<T>(@noescape
transform: (Self.Generator.Element) throws -> T) rethrows -> [T] • Self.Generator.Element->TͷؔΛ Self.Generator.Elementʹద༻ͯ͠[T]Λฦ͢ • ͋Δ<U>͜͜Ͱ[U]
͍͍ͩͨ͜Μͳײ͡ func map<U>(f:T -> U) -> ͋Δ<U> ObservableType public func
map<R>(selector: Self.E throws -> R) -> RxSwift.Observable<R> • Self.E->RͷؔΛSelf.Eʹద༻ͯ͠ RxSwift.Observable<R>Λฦ͢ • ͋Δ<U>͜͜ͰRxSwift.Observable<R>
Μʁ
Ͳͷmapಉ͡ʢ͖ͬ͞ݴͬͨʣ func map<U>(f:T -> U) -> ͋Δ<U> • ͋Δ<T>ʹؔ(T->U)Λద༻ͯ͋͠Δ<U>Λฦ͢
۩ମతʹ func map<String>(f:Int -> String) -> Optional<String> Optional let o:Optional<String>
= Optional<Int>(1).map{String($0)}
۩ମతʹ func map<String>(f:Int -> String) -> Array<String> Array let a:Array<String>
= Array(arrayLiteral: 1,2,3).map {String($0)}
۩ମతʹ func map<String>(f:Int -> String) -> Observable<String> ObservableType let o:Observable<String>
= Observable.just(1).map{String($0)}
ΈΜͳಉͩ͡Ͷʢ͖ͬ͞ݴͬͨʣ let a:͋Δ<U> = ͋Δ<T>.map{U($0)} • ͋Δ͕ • Optional •
Array • Observable ʹͳΔΘ͚Ͱ͢ͳ
͋ΔͬͯԿʁ
Functor • શମΛҠͤΔͷΛද͢ • ؔखͱ͍͏Β͍͠ • ശʹྫ͑ΒΕΔࣄ͕͋Δ Πϝʔδ let a:Functor<U>
= Functor<T>.map{U($0)}
Functor let o:Optional<String> = Optional<Int>(1).map{String($0)} map͕͍ͯ͠Δ͜ͱ 1. Optional<Int>ͱ͍͏ശͷதΛऔΓग़͠ɺ͜ΕΛ$0ͱ͢ Δɻ 2.
$0ΛStringʹΩϟετ 3. $0ΛOptional<String>ͱ͍͏ശʹೖΕͯฦ͢
Functor let a:Array<String> = Array(arrayLiteral: 1,2,3).map {String($0)} map͕͍ͯ͠Δ͜ͱ 1. Array<Int>ͱ͍͏ശͷதΛऔΓग़͠ɺ͜ΕΛ$0ͱ͢Δɻ
2. $0ʢ֤ཁૉʣΛStringʹΩϟετ 3. $0ΛArray<String>ͱ͍͏ശʹೖΕͯฦ͢
Functor let o:Observable<String> = Observable.just(1).map{String($0)} map͕͍ͯ͠Δ͜ͱ 1. Observable<Int>ͱ͍͏ശͷதΛऔΓग़͠ɺ͜ΕΛ$0ͱ͢ Δɻ 2.
$0ΛStringʹΩϟετ 3. $0ΛObservable<String>ͱ͍͏ശʹೖΕͯฦ͢
·ͱΊ • mapͷഎޙʹFunctorͱ͍͏֓೦͕͋Δ • FunctorશମΛҠͤΔΛ͋ΒΘ͢ • ࠓճͷྫͰOptional,Array,Observable • શମΛҠ͢ಈ࡞͕mapͰ͋Δ