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
1.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
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
350
How did I build an Open-Source SwiftUI Toast Library
lovee
1
72
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
810
SwiftUIで二重スクロール作ってみた / When I tried to make a dual-scroll-ish view in SwiftUI
lovee
1
280
Observation のあれこれ / A brief introduction about Observation
lovee
3
360
ChatGPT 時代の勉強 / Learning under ChatGPT era
lovee
27
8.6k
属人化しない為の勉強会作り / To make tech meetups with less personal dependencies
lovee
0
300
偏見と妄想で語るスクリプト言語としての Swift / Swift as a Scripting Language
lovee
2
830
danger-swift-kantoku
lovee
1
520
Other Decks in Programming
See All in Programming
5分で理解する SOLID 原則 #phpcon_nagoya
shogogg
1
270
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2.2k
Amazon ECS とマイクロサービスから考えるシステム構成
hiyanger
2
580
『品質』という言葉が嫌いな理由
korimu
0
180
楽しく向き合う例外対応
okutsu
0
540
pylint custom ruleで始めるレビュー自動化
shogoujiie
0
130
CSS Linter による Baseline サポートの仕組み
ryo_manba
1
140
CloudNativePGを布教したい
nnaka2992
0
100
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
150
Formの複雑さに立ち向かう
bmthd
1
900
Unity Android XR入門
sakutama_11
0
170
AIプログラミング雑キャッチアップ
yuheinakasaka
6
910
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
67
4.6k
Fireside Chat
paigeccino
34
3.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
Building an army of robots
kneath
303
45k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.4k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.5k
Navigating Team Friction
lara
183
15k
Become a Pro
speakerdeck
PRO
26
5.1k
How to Ace a Technical Interview
jacobian
276
23k
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"