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
360
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
CDK開発におけるコーディング規約の運用
yamanashi_ren01
2
250
コードを読んで理解するko build
bells17
1
110
データの整合性を保つ非同期処理アーキテクチャパターン / Async Architecture Patterns
mokuo
53
18k
ML.NETで始める機械学習
ymd65536
0
230
バッチを作らなきゃとなったときに考えること
irof
2
520
Introduction to kotlinx.rpc
arawn
0
750
Go 1.24でジェネリックになった型エイリアスの紹介
syumai
2
280
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
160
データベースのオペレーターであるCloudNativePGがStatefulSetを使わない理由に迫る
nnaka2992
0
230
Open source software: how to live long and go far
gaelvaroquaux
0
660
たのしいSocketのしくみ / Socket Under a Microscope
coe401_
8
1.2k
Bedrock Agentsレスポンス解析によるAgentのOps
licux
3
910
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
511
110k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
4 Signs Your Business is Dying
shpigford
182
22k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Producing Creativity
orderedlist
PRO
344
40k
Bash Introduction
62gerente
611
210k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Side Projects
sachag
452
42k
Designing Experiences People Love
moore
140
23k
Code Reviewing Like a Champion
maltzj
521
39k
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"