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
76
Swift 2.0
greg3z
0
100
Other Decks in Programming
See All in Programming
PHPカンファレンス関西2025 基調講演
sugimotokei
5
930
バイブスあるコーディングで ~PHP~ 便利ツールをつくるプラクティス
uzulla
1
260
Yes, You Can Work on Rails & any other Gem
kaspth
0
110
はじめてのWeb API体験 ー 飲食店検索アプリを作ろうー
akinko_0915
0
160
Jakarta EE Meets AI
ivargrimstad
0
270
The Niche of CDK Grant オブジェクトって何者?/the-niche-of-cdk-what-isgrant-object
hassaku63
1
690
マッチングアプリにおけるフリックUIで苦労したこと
yuheiito
0
240
NEWT Backend Evolution
xpromx
1
150
QA x AIエコシステム段階構築作戦
osu
0
130
TypeScriptでDXを上げろ! Hono編
yusukebe
3
860
Hack Claude Code with Claude Code
choplin
8
2.8k
ご注文の差分はこちらですか? 〜 AWS CDK のいろいろな差分検出と安全なデプロイ
konokenj
4
670
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.4k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
5.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.9k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Optimizing for Happiness
mojombo
379
70k
KATA
mclloyd
30
14k
Producing Creativity
orderedlist
PRO
346
40k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.5k
Automating Front-end Workflow
addyosmani
1370
200k
Testing 201, or: Great Expectations
jmmastey
43
7.6k
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