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
Swift Sequences & Collections
Search
greg3z
December 10, 2015
Programming
0
48
Swift Sequences & Collections
How sequences & collections work in Swift
greg3z
December 10, 2015
Tweet
Share
More Decks by greg3z
See All by greg3z
How to turn an onion into a snake?
greg3z
0
1.5k
The Inheritance Curse
greg3z
0
1.1k
MVC-RS
greg3z
0
220
Swift Open Source
greg3z
0
75
Swift 2.0
greg3z
0
100
Other Decks in Programming
See All in Programming
Use Perl as Better Shell Script
karupanerura
0
690
Go1.25からのGOMAXPROCS
kuro_kurorrr
0
130
Cline指示通りに動かない? AI小説エージェントで学ぶ指示書の書き方と自動アップデートの仕組み
kamomeashizawa
1
430
AIエージェントによるテストフレームワーク Arbigent
takahirom
0
370
Haskell でアルゴリズムを抽象化する / 関数型言語で競技プログラミング
naoya
16
4k
GoのGenericsによるslice操作との付き合い方
syumai
2
450
TypeScript LSP の今までとこれから
quramy
1
490
エラーって何種類あるの?
kajitack
5
140
プロダクト開発でも使おう 関数のオーバーロード
yoiwamoto
0
140
Perlで痩せる
yuukis
1
680
Cloudflare Realtime と Workers でつくるサーバーレス WebRTC
nekoya3
0
390
セキュリティマネジャー廃止とクラウドネイティブ型サンドボックス活用
kazumura
1
170
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
650
Facilitating Awesome Meetings
lara
54
6.4k
Faster Mobile Websites
deanohume
307
31k
Why Our Code Smells
bkeepers
PRO
337
57k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
34k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Raft: Consensus for Rubyists
vanstee
139
7k
GitHub's CSS Performance
jonrohan
1031
460k
Building an army of robots
kneath
306
45k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.8k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Transcript
Swift Sequences & Collections @greg3z
let array = [1, 2, 3]
let array = [1, 2, 3] array[7]
let dic = ["a": 1, "b": 2]
let dic = ["a": 1, "b": 2] dic["z"]
[] -> subscript
struct Array<Element> { subscript(index: Int) -> Element }
struct Dictionary<Key: Hashable, Value> { subscript(key: Key) -> Value? }
subscript(index: Int) -> Element?
subscript(safe index: Int) -> Element?
subscript(safe index: Int) -> Element? array[safe: 2]
extension Array { subscript(safe i: Int) -> Element? { return
i >= 0 && i < count ? self[i] : nil } }
let array = [1, 2, 3]
let array = [1, 2, 3] array[safe: 7]
Custom collection? A type that I did myself
struct Section<T> { let title: String let elements: [T] }
struct Section<T> { let title: String let elements: [T] subscript(index:
Int) -> T? { return elements[safe: index] } }
let cars = ["911", "Cayman", "Cayenne"] let section = Section(title:
"Porsche", elements: cars)
let cars = ["911", "Cayman", "Cayenne"] let section = Section(title:
"Porsche", elements: cars) section[1] // Optional("Cayman")
Sequence A type that can be iterated with a `for`...`in`
loop
protocol SequenceType { func generate() -> GeneratorType }
protocol GeneratorType { func next() -> Element? }
struct ArrayGenerator<T>: GeneratorType { func next() -> T? { return
something } }
struct ArrayGenerator<T>: GeneratorType { let array: [T] var currentIndex =
0 init(_ array: [T]) { self.array = array } mutating func next() -> T? { return array[safe: currentIndex++] } }
struct Section<T>: SequenceType { let title: String let elements: [T]
func generate() -> ArrayGenerator<T> { return ArrayGenerator(elements) } }
var generator = section.generate() while let element = generator.next() {
}
for element in section { }
var generator = section.generate() while let element = generator.next() {
} for element in section { }
let cars = ["911", "Cayman", "Cayenne"] let section = Section(title:
"Porsche", elements: cars)
for car in section { } // 911 // Cayman
// Cayenne
for (index, car) in section.enumerate() { } // 0 911
// 1 Cayman // 2 Cayenne
section.minElement() // 911 section.maxElement() // Cayman section.sort() // ["911", "Cayenne",
"Cayman"]
section.contains("911") // true
section.filter { $0.characters.count > 3 } // ["Cayman", "Cayenne"]
section.map { $0.characters.count } // [3, 6, 7]
section.reduce(0) { $0 + $1.characters.count } // 16
Collection A multi-pass *sequence* with addressable positions
protocol CollectionType : Indexable, SequenceType { }
protocol Indexable { var startIndex: Index { get } var
endIndex: Index { get } }
struct Section<T>: Indexable { let title: String var elements: [T]
var startIndex: Int { return 0 } var endIndex: Int { return elements.count } subscript(index: Int) -> T? { … } func generate() -> ArrayGenerator<T> { … } }
protocol Indexable { var startIndex: Index { get } var
endIndex: Index { get } subscript(position: Index) -> Element { get } }
struct Section<T>: CollectionType { let title: String var elements: [T]
var startIndex: Int { return 0 } var endIndex: Int { return elements.count } subscript(index: Int) -> T? { … } func generate() -> ArrayGenerator<T> { … } }
struct Section<T>: CollectionType { let title: String var elements: [T]
var startIndex: Int { return 0 } var endIndex: Int { return elements.count } subscript(index: Int) -> T { return elements[index] } func generate() -> ArrayGenerator<T> { … } }
struct Section<T>: CollectionType { let title: String let elements: [T]
var startIndex: Int { return 0 } var endIndex: Int { return elements.count } subscript(index: Int) -> T { return elements[index] } subscript(safe index: Int) -> T? { return elements[safe: index] } func generate() -> ArrayGenerator<T> { … } }
let cars = ["911", "Cayman", "Cayenne"] let section = Section(title:
"Porsche", elements: cars) section.count // 3 section.first // Optional("911") section.isEmpty // false section.indexOf("Cayman") // 1
Epilogue So dictionaries aren’t Collections?
struct Dictionary<K : Hashable, V> { subscript(key: K) -> V?
subscript(position: DictionaryIndex<K, V>) -> (K, V) }
let dic = ["a": "audi", "b": "bmw", "c": "citroen"] let
index = dic.startIndex // DictionaryIndex<String, String> dic[index] // ("a", "audi") dic[index.advancedBy(1)] // ("b", "bmw") dic[index.advancedBy(3)] // Fatal error
Thank you! @greg3z medium.com/swift-programming