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
43
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.4k
The Inheritance Curse
greg3z
0
1.1k
MVC-RS
greg3z
0
190
Swift Open Source
greg3z
0
67
Swift 2.0
greg3z
0
93
Other Decks in Programming
See All in Programming
OpenTelemetry のサービスという概念について
azukiazusa1
1
410
App Router への移行は「改善」となり得るのか?/ Can migration to App Router be an improvement
takefumiyoshii
1
140
DDDはなぜ難しいのか / 良いコードの定義と設計能力の壁
pospome
25
7.6k
ここ1~2年くらいで 使えるようになった(主要ブラウザーの最新版 がすべて対応した ) ウェブの新機能について ランダムに喋る!
myzkyy
9
6.5k
WinUI 3デモ - "CommunityToolkit.Mvvm"NuGetパッケージ編
andrewkeepcoding
0
140
Material 3で Material 2ぽい見た目にする
numeroanddev
2
250
Migrating to Signals: A Practical Workshop
manfredsteyer
PRO
0
290
TCA魔法学入門🪄
dazy
0
280
チームでモデリングを育てるうえで 考えたこと・気づいたこと / Cultivating Modeling in Teams: Thoughts and Insights
mackey0225
5
2.4k
【KMC春合宿2024】実装視点で見るNeural Radiance Fields
runningoutrate
0
150
LPIXEL×CADDi_kaerururu
kaerururu
3
300
品質とスピードを両立: TypeScriptの柔軟な型システムをバックエンドで活用する
kosui
5
1.2k
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
288
19k
Gamification - CAS2011
davidbonilla
76
4.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
397
65k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
5
1.4k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
101
6.6k
10 Git Anti Patterns You Should be Aware of
lemiorhan
644
57k
How GitHub Uses GitHub to Build GitHub
holman
467
290k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
6
950
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
319
20k
Build your cross-platform service in a week with App Engine
jlugia
223
17k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
242
20k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
153
14k
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