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
71
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
350
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
Honoとフロントエンドの 型安全性について
yodaka
7
1.4k
1年目の私に伝えたい!テストコードを怖がらなくなるためのヒント/Tips for not being afraid of test code
push_gawa
1
390
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
5
390
責務と認知負荷を整える! 抽象レベルを意識した関心の分離
yahiru
8
1.1k
React 19アップデートのために必要なこと
uhyo
3
710
Lottieアニメーションをカスタマイズしてみた
tahia910
0
130
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
40
15k
Rubyで始める関数型ドメインモデリング
shogo_tksk
0
120
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
490
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
160
dbt Pythonモデルで実現するSnowflake活用術
trsnium
0
210
『品質』という言葉が嫌いな理由
korimu
0
180
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
980
Building a Scalable Design System with Sketch
lauravandoore
461
33k
Bootstrapping a Software Product
garrettdimon
PRO
306
110k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Optimizing for Happiness
mojombo
376
70k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Writing Fast Ruby
sferik
628
61k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.2k
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"