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
Almost Everything You Ever Wanted To Know About...
Search
Elvis Shi
March 06, 2017
Programming
0
1k
Almost Everything You Ever Wanted To Know About Sequence and Collection Supplement
Swift の Sequence と Collection についての簡単な説明 & try! Swift Tokyo 2017 の同名の発表についての追記
Elvis Shi
March 06, 2017
Tweet
Share
More Decks by Elvis Shi
See All by Elvis Shi
How did I build an Open-Source SwiftUI Toast Library
lovee
1
48
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
660
SwiftUIで二重スクロール作ってみた / When I tried to make a dual-scroll-ish view in SwiftUI
lovee
1
240
Observation のあれこれ / A brief introduction about Observation
lovee
3
330
ChatGPT 時代の勉強 / Learning under ChatGPT era
lovee
26
8.2k
属人化しない為の勉強会作り / To make tech meetups with less personal dependencies
lovee
0
270
偏見と妄想で語るスクリプト言語としての Swift / Swift as a Scripting Language
lovee
2
790
danger-swift-kantoku
lovee
1
490
Decimal、正しく使ってる? / Are you using Decimal correctly?
lovee
4
690
Other Decks in Programming
See All in Programming
Realtime API 入門
riofujimon
0
150
最新TCAキャッチアップ
0si43
0
140
Arm移行タイムアタック
qnighy
0
320
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
役立つログに取り組もう
irof
28
9.6k
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
100
Jakarta EE meets AI
ivargrimstad
0
610
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
100
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
受け取る人から提供する人になるということ
little_rubyist
0
230
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
33
2.9k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Code Reviewing Like a Champion
maltzj
520
39k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Music & Morning Musume
bryan
46
6.2k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Why Our Code Smells
bkeepers
PRO
334
57k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
The Invisible Side of Design
smashingmag
298
50k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Transcript
Everything You Ever Wanted To Know About Sequence and Collection
for AKIBA.swift×SwiftѪձ vol2 Alm ost supplem ent
override init() { super.init() emplyedBy = "MAGES. 5pb. Game div"
job = "iOS Developer" twitter = "@lovee" github = "el-hoshino" additionalInfo = "NotAutoLayout 0.8.0 released!" } class Speaker: Human { }
Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection
Sequence • ͷϦετ • ༗ݶΞϦɺແݶΞϦ • ΠςϨʔλʔ܁Γฦͯ͑͠ΔͱݶΒͳ͍
Sequence protocol Sequence { associatedtype Iterator: IteratorProtocol func makeIterator() ->
Iterator }
Collection • Sequence ͔Βܧঝ • ৗʹ༗ݶ • ΠςϨʔλʔ܁Γฦͯ͑͠Δ
Collection protocol Collection: Sequence { associatedtype Index: Comparable var startIndex:
Index var endIndex: Index subscript(position: Index) -> Iterator.Element { get } func index(after index: Index) -> Index }
BidirectionalCollection • Collection ͔Βܧঝ • ਖ਼ํ͚ͩͰͳ͘ٯํ͔Β͑ΒΕΔ
protocol BidirectionalCollection: Collection { func index(before index: Index) -> Index
} BidirectionalCollection protocol Collection { func index(after index: Index) -> Index }
RandomAccessCollection • BidirectionalCollection ͔Βܧঝ • ҙͷΠϯσοΫεʹૉૣ͘ΞΫηεͰ͖Δ • ҙͷ 2 ΠϯσοΫεؒͷڑ͕ૉૣ͘ٻΊΒΕΔ
protocol RandomAccessCollection: BidirectionalCollection { /// - Complexity: O(1) func index(_
i: Index, offsetBy n: IndexDistance) -> Index /// - Complexity: O(1) func distance(from start: Index, to end: Index) -> IndexDistance } RandomAccessCollection
Complexity: O(1) ͱ • Φʔμʔه๏ʮO(1)ʯ • σʔλྔʢཁૉʣʹࠨӈͤͣɺৗʹҰఆ࣌ؒͰߦ͏ • ΞϧΰϦζϜͷ͞ΛධՁ͢ΔࢦඪͷҰͭ •
ଞʹʮO(n)ʯʮO(log n)ʯͳͲ
protocol RandomAccessCollection: BidirectionalCollection { /// - Complexity: O(1) func index(_
i: Index, offsetBy n: IndexDistance) -> Index /// - Complexity: O(1) func distance(from start: Index, to end: Index) -> IndexDistance } RandomAccessCollection
// RandomAccessCollection ʹ४ڌ͠ͳ͍ྫ protocol String.CharacterView: BidirectionalCollection { /// - Complexity:
O(n) func index(_ i: String.Index, offsetBy n: String.IndexDistance) -> String.Index }
ʮจࣈʯͬͯͳ͊ʹʁ ʙString, NSString, Unicodeͷجຊʙ by takasek 2016/11/16 ୈ5ճελʔτΞοϓiOSษڧձ 1 https://speakerdeck.com/takasek/wen-zi-shu-tutenaani-string-
nsstring-unicodefalseji-ben
ࠓ࢈ۀ RandomAccessCollection ͱ ू߹ͷ ϥϯμϜͳཁૉʹ ૉૣ͘ΞΫηεͰ͖Δ ू߹ͷ͜ͱ
MutableCollection • Collection ͔Βܧঝ • ಛఆཁૉʹର͠ήολʔͱηολʔ྆ํ༻ҙ͞Ε͍ͯΔ
protocol MutableCollection: Collection { subscript(position: Index) -> Iterator.Element { get
set } } MutableCollection protocol Collection { subscript(position: Index) -> Iterator.Element { get } }
// MutableCollection ʹ४ڌ͠ͳ͍ྫ protocol String.CharacterView: BidirectionalCollection var str = "Hello,
Swift" str.characters[str.startIndex] = "H" // error: cannot assign through subscript: subscript is get- only
ʮจࣈʯͬͯͳ͊ʹʁ ʙString, NSString, Unicodeͷجຊʙ by takasek 2016/11/16 ୈ5ճελʔτΞοϓiOSษڧձ 1 https://speakerdeck.com/takasek/wen-zi-shu-tutenaani-string-
nsstring-unicodefalseji-ben
ࠓ࢈ۀ MutableCollection ͱ ू߹ͷ ಛఆͳཁૉΛ ஔ͖͑ΒΕΔ ू߹ͷ͜ͱ
RangeReplaceableCollection
Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection
RangeReplaceableCollection • RandomAccessCollection ͔Βܧঝ !
RangeReplaceableCollection • Collection ͔Βܧঝ • ू߹ͷཁૉΛมߋ͢ΔΠϯλʔϑΣΠεΛఏڙ
protocol RangeReplaceableCollection: Collection { mutating func replaceSubrange(_ subrange: Range<Index>, with
newElements: Collection) mutating func append(_ newElement: Iterator.Element) mutating func insert(_ newElement: Iterator.Element, at i: Index) mutating func remove(at i: Index) -> Iterator.Element // etc... } RangeReplaceableCollection protocol MutableCollection: Collection { subscript(position: Index) -> Iterator.Element { get set } }
ࠓ࢈ۀ RangeReplaceableCollection ͱ ू߹ͷ ཁૉΛ ఴͰ͖Δ ू߹ͷ͜ͱ
Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection
Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection
Θ͔ͬͨΘ͔ͬͨɻ ͰɺԿͷʹཱͭͷʁ "
࣮ҙ֎ͱ୯७ͩͬͨ Sequence#map ͷ࣮ݱ extension Sequence { // ࣮ࡍͷ࣮ͱগʑҧ͏ɺڵຯ͋ΔํԼهͷϦϯΫ͔Β͍ͯΈͯͶ // https://github.com/apple/swift/blob/master/stdlib/public/
core/Sequence.swift func map<T>(_ transform: (Iterator.Element) -> T) -> [T] { var result: [T] = [] var iterator = self.makeIterator() while let element = iterator.next() { result.append(transform(element)) } return result } }
Γ͕ࣙॻܕͷ Dictionary#map ࡞ͬͯΈΑ͏ extension Dictionary { func map<T>(_ transform: (Value)
-> T) -> [Key: T] { var result: [Key: T] = [:] var iterator = self.makeIterator() while let element = iterator.next() { result[element.key] = transform(element.value) } return result } } [1: 1, 2: 2].map { $0.description } // [1: "1", 2: "2"]
ॳظͷཁΒͳ͍ Sequence#reduce ࡞ͬͯΈΑ͏ extension Sequence { func reduce(_ nextPartialResult: (_
result: Iterator.Element, _ next: Iterator.Element) -> Iterator.Element) -> Iterator.Element? { var iterator = self.makeIterator() guard var result = iterator.next() else { return nil } while let next = iterator.next() { result = nextPartialResult(result, next) } return result } } [1, 2, 3, 4, 5].reduce(*) // 120
࡞ͬͯΈΔͱ໘നͦ͏ͳͷ • ྻ͔ΒάϧʔϓͰ·ͱΊͨೋ࣍ݩྻΛ࡞Δؔ • ԿΛ݅ʹͯ͠άϧʔϓΛ·ͱΊΔ͔ • ͲͷϓϩτίϧʢSequence ͔ Collection ͔Կ͔ʣΛ֦ு͖͔͢
• ແݶʹ͕Δ Sequence • Ͳ͏͢Ε Sequence ͕ແݶʹͳΔͷ͔ • MutableCollection ʹରԠ͢Δ͚Ͳ RangeReplaceableCollection ʹର Ԡ͠ͳ͍ू߹ • etc...
⺄⇾ْطٳ
None
None
!ank y"