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
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Macとオーディオ再生 2024/11/02
yusukeito
0
370
C++でシェーダを書く
fadis
6
4.1k
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
CSC509 Lecture 12
javiergs
PRO
0
160
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
ヤプリ新卒SREの オンボーディング
masaki12
0
130
みんなでプロポーザルを書いてみた
yuriko1211
0
280
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
900
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Site-Speed That Sticks
csswizardry
0
28
Fireside Chat
paigeccino
34
3k
A Philosophy of Restraint
colly
203
16k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Thoughts on Productivity
jonyablonski
67
4.3k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
The Pragmatic Product Professional
lauravandoore
31
6.3k
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Ͱ͋Δ