swift sequences & collections

49
Swift Sequences & Collections @greg3z

Upload: cocoaheads-france

Post on 15-Apr-2017

5.485 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Swift Sequences & Collections

Swift Sequences & Collections

@greg3z

Page 2: Swift Sequences & Collections

let array = [1, 2, 3]

Page 3: Swift Sequences & Collections

let array = [1, 2, 3]

array[7] 😭

Page 4: Swift Sequences & Collections

let dic = ["a": 1, "b": 2]

Page 5: Swift Sequences & Collections

let dic = ["a": 1, "b": 2]

dic["z"] 😎

Page 6: Swift Sequences & Collections

[] -> subscript

Page 7: Swift Sequences & Collections

struct Array<Element> { subscript(index: Int) -> Element }

Page 8: Swift Sequences & Collections

struct Dictionary<Key: Hashable, Value> { subscript(key: Key) -> Value? }

Page 9: Swift Sequences & Collections

subscript(index: Int) -> Element?

Page 10: Swift Sequences & Collections

subscript(safe index: Int) -> Element?

Page 11: Swift Sequences & Collections

subscript(safe index: Int) -> Element?

array[safe: 2]

Page 12: Swift Sequences & Collections

extension Array { subscript(safe i: Int) -> Element? { return i >= 0 && i < count ? self[i] : nil } }

Page 13: Swift Sequences & Collections

let array = [1, 2, 3]

Page 14: Swift Sequences & Collections

let array = [1, 2, 3]

array[safe: 7] 😎

Page 15: Swift Sequences & Collections

Custom collection? A type that I did myself

Page 16: Swift Sequences & Collections

struct Section<T> { let title: String let elements: [T] }

Page 17: Swift Sequences & Collections

struct Section<T> { let title: String let elements: [T] subscript(index: Int) -> T? { return elements[safe: index] } }

Page 18: Swift Sequences & Collections

let cars = ["911", "Cayman", "Cayenne"] let section = Section(title: "Porsche", elements: cars)

Page 19: Swift Sequences & Collections

let cars = ["911", "Cayman", "Cayenne"] let section = Section(title: "Porsche", elements: cars)

section[1] // Optional("Cayman")

Page 20: Swift Sequences & Collections

Sequence A type that can be iterated with a `for`...`in` loop

Page 21: Swift Sequences & Collections

protocol SequenceType { func generate() -> GeneratorType }

Page 22: Swift Sequences & Collections

protocol GeneratorType {

func next() -> Element? }

Page 23: Swift Sequences & Collections

struct ArrayGenerator<T>: GeneratorType { func next() -> T? { return something } }

Page 24: Swift Sequences & Collections

struct ArrayGenerator<T>: GeneratorType { let array: [T] var currentIndex = 0 init(_ array: [T]) { self.array = array } mutating func next() -> T? { return array[safe: currentIndex++] } }

Page 25: Swift Sequences & Collections

struct Section<T>: SequenceType { let title: String let elements: [T] func generate() -> ArrayGenerator<T> { return ArrayGenerator(elements) } }

Page 26: Swift Sequences & Collections

var generator = section.generate() while let element = generator.next() { }

Page 27: Swift Sequences & Collections

for element in section { }

Page 28: Swift Sequences & Collections

var generator = section.generate() while let element = generator.next() { }

for element in section { }

Page 29: Swift Sequences & Collections

let cars = ["911", "Cayman", "Cayenne"] let section = Section(title: "Porsche", elements: cars)

Page 30: Swift Sequences & Collections

for car in section {

} // 911 // Cayman // Cayenne

Page 31: Swift Sequences & Collections

for (index, car) in section.enumerate() {

} // 0 911 // 1 Cayman // 2 Cayenne

Page 32: Swift Sequences & Collections

section.minElement() // 911

section.maxElement() // Cayman

section.sort() // ["911", "Cayenne", "Cayman"]

Page 33: Swift Sequences & Collections

section.contains("911") // true

Page 34: Swift Sequences & Collections

section.filter { $0.characters.count > 3 } // ["Cayman", "Cayenne"]

Page 35: Swift Sequences & Collections

section.map { $0.characters.count } // [3, 6, 7]

Page 36: Swift Sequences & Collections

section.reduce(0) { $0 + $1.characters.count } // 16

Page 37: Swift Sequences & Collections

Collection A multi-pass *sequence* with addressable positions

Page 38: Swift Sequences & Collections

protocol CollectionType : Indexable, SequenceType { }

Page 39: Swift Sequences & Collections

protocol Indexable { var startIndex: Index { get } var endIndex: Index { get } }

Page 40: Swift Sequences & Collections

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> { … }

}

Page 41: Swift Sequences & Collections

protocol Indexable { var startIndex: Index { get } var endIndex: Index { get } subscript(position: Index) -> Element { get } }

Page 42: Swift Sequences & Collections

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> { … } }

Page 43: Swift Sequences & Collections

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> { … } }

Page 44: Swift Sequences & Collections

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> { … } }

Page 45: Swift Sequences & Collections

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

Page 46: Swift Sequences & Collections

Epilogue So dictionaries aren’t Collections?

Page 47: Swift Sequences & Collections

struct Dictionary<K : Hashable, V> {

subscript(key: K) -> V? subscript(position: DictionaryIndex<K, V>) -> (K, V) }

Page 48: Swift Sequences & Collections

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

Page 49: Swift Sequences & Collections

Thank you! 🤗

@greg3z 🤔

medium.com/swift-programming