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 Sequence and Collection Supplement
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
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
150
SwiftUIで二重スクロール作ってみた / When I tried to make a dual-scroll-ish view in SwiftUI
lovee
1
190
Observation のあれこれ / A brief introduction about Observation
lovee
3
310
ChatGPT 時代の勉強 / Learning under ChatGPT era
lovee
22
6.6k
属人化しない為の勉強会作り / To make tech meetups with less personal dependencies
lovee
0
210
偏見と妄想で語るスクリプト言語としての Swift / Swift as a Scripting Language
lovee
2
650
danger-swift-kantoku
lovee
1
410
Decimal、正しく使ってる? / Are you using Decimal correctly?
lovee
4
580
A story about me trying to make a router that manages when, how and which view to transit in a SwiftUI app
lovee
1
330
Other Decks in Programming
See All in Programming
dbtのドメイン分割による データ基盤の改善とDigdagとの連携
sakama
0
350
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
8
4.1k
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
1
190
スクラムガイドのスプリントレトロスペクティブを改めて読みかえしてみた / Re-reading the Sprint Retrospective Section in the Scrum Guide
mackey0225
3
430
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
650
PostmanでAPIの動作確認が楽になった話
h455h1
0
170
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
340
Rubyでたのしむクリエイティブコーディング/Enjoy Creative coding with Ruby
chobishiba
1
180
Git Rebase
bkuhlmann
11
1.6k
FigmaとPHPで作る1ミリたりとも表示崩れしない最強の帳票印刷ソリューション
ttskch
43
19k
2 週間で Twitter Bot を作ってみた
contour_gara
0
550
Random\Randomizer クラスで日常のあれこれを解決しよう! / Random\Randomizer class solves familiar trouble
cocoeyes02
0
250
Featured
See All Featured
The Mythical Team-Month
searls
216
42k
For a Future-Friendly Web
brad_frost
172
9k
Ruby is Unlike a Banana
tanoku
96
10k
Making Projects Easy
brettharned
108
5.5k
What's new in Ruby 2.0
geeforr
337
31k
GraphQLとの向き合い方2022年版
quramy
32
12k
Robots, Beer and Maslow
schacon
PRO
155
7.9k
How to train your dragon (web standard)
notwaldorf
73
5.2k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
6.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
6
1.5k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
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"