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
210
Swift Open Source
greg3z
0
73
Swift 2.0
greg3z
0
100
Other Decks in Programming
See All in Programming
ErdMap: Thinking about a map for Rails applications
makicamel
1
700
HTML/CSS超絶浅い説明
yuki0329
0
190
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
420
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
14
2.4k
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
410
functionalなアプローチで動的要素を排除する
ryopeko
1
240
Flatt Security XSS Challenge 解答・解説
flatt_security
0
740
令和7年版 あなたが使ってよいフロントエンド機能とは
mugi_uno
11
5.3k
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
3
590
Fixstars高速化コンテスト2024準優勝解法
eijirou
0
190
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
1.3k
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
1.9k
Featured
See All Featured
Designing Experiences People Love
moore
139
23k
Building Adaptive Systems
keathley
38
2.4k
The Invisible Side of Design
smashingmag
299
50k
A designer walks into a library…
pauljervisheath
205
24k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Code Reviewing Like a Champion
maltzj
521
39k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.5k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.7k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.6k
Designing for humans not robots
tammielis
250
25k
Designing for Performance
lara
604
68k
How to Ace a Technical Interview
jacobian
276
23k
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