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
72
Swift 2.0
greg3z
0
100
Other Decks in Programming
See All in Programming
CSC305 Lecture 26
javiergs
PRO
0
140
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
270
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
270
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
250
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
testcontainers のススメ
sgash708
1
120
Go の GC の不得意な部分を克服したい
taiyow
2
770
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
As an Engineers, let's build the CRM system via LINE Official Account 2.0
clonn
1
670
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
Featured
See All Featured
Unsuck your backbone
ammeep
669
57k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
For a Future-Friendly Web
brad_frost
175
9.4k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Agile that works and the tools we love
rasmusluckow
328
21k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
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