Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Almost Everything You Ever Wanted To Know About...

Almost Everything You Ever Wanted To Know About Sequence and Collection Supplement

Swift の Sequence と Collection についての簡単な説明 & try! Swift Tokyo 2017 の同名の発表についての追記

Elvis Shi

March 06, 2017
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

  1. Everything You Ever Wanted To Know About Sequence and Collection

    for AKIBA.swift×SwiftѪ޷ձ vol2 Alm ost supplem ent
  2. override init() { super.init() emplyedBy = "MAGES. 5pb. Game div"

    job = "iOS Developer" twitter = "@lovee" github = "el-hoshino" additionalInfo = "NotAutoLayout 0.8.0 released!" } class Speaker: Human { }
  3. Collection protocol Collection: Sequence { associatedtype Index: Comparable var startIndex:

    Index var endIndex: Index subscript(position: Index) -> Iterator.Element { get } func index(after index: Index) -> Index }
  4. protocol BidirectionalCollection: Collection { func index(before index: Index) -> Index

    } BidirectionalCollection protocol Collection { func index(after index: Index) -> Index }
  5. protocol RandomAccessCollection: BidirectionalCollection { /// - Complexity: O(1) func index(_

    i: Index, offsetBy n: IndexDistance) -> Index /// - Complexity: O(1) func distance(from start: Index, to end: Index) -> IndexDistance } RandomAccessCollection
  6. protocol RandomAccessCollection: BidirectionalCollection { /// - Complexity: O(1) func index(_

    i: Index, offsetBy n: IndexDistance) -> Index /// - Complexity: O(1) func distance(from start: Index, to end: Index) -> IndexDistance } RandomAccessCollection
  7. // RandomAccessCollection ʹ४ڌ͠ͳ͍ྫ protocol String.CharacterView: BidirectionalCollection { /// - Complexity:

    O(n) func index(_ i: String.Index, offsetBy n: String.IndexDistance) -> String.Index }
  8. protocol MutableCollection: Collection { subscript(position: Index) -> Iterator.Element { get

    set } } MutableCollection protocol Collection { subscript(position: Index) -> Iterator.Element { get } }
  9. // MutableCollection ʹ४ڌ͠ͳ͍ྫ protocol String.CharacterView: BidirectionalCollection var str = "Hello,

    Swift" str.characters[str.startIndex] = "H" // error: cannot assign through subscript: subscript is get- only
  10. protocol RangeReplaceableCollection: Collection { mutating func replaceSubrange(_ subrange: Range<Index>, with

    newElements: Collection) mutating func append(_ newElement: Iterator.Element) mutating func insert(_ newElement: Iterator.Element, at i: Index) mutating func remove(at i: Index) -> Iterator.Element // etc... } RangeReplaceableCollection protocol MutableCollection: Collection { subscript(position: Index) -> Iterator.Element { get set } }
  11. ࣮͸ҙ֎ͱ୯७ͩͬͨ
 Sequence#map ͷ࣮ݱ extension Sequence { // ࣮ࡍͷ࣮૷ͱ͸গʑҧ͏ɺڵຯ͋Δํ͸ԼهͷϦϯΫ͔Β೷͍ͯΈͯͶ // https://github.com/apple/swift/blob/master/stdlib/public/

    core/Sequence.swift func map<T>(_ transform: (Iterator.Element) -> T) -> [T] { var result: [T] = [] var iterator = self.makeIterator() while let element = iterator.next() { result.append(transform(element)) } return result } }
  12. ໭Γ஋͕ࣙॻܕͷ
 Dictionary#map ࡞ͬͯΈΑ͏ extension Dictionary { func map<T>(_ transform: (Value)

    -> T) -> [Key: T] { var result: [Key: T] = [:] var iterator = self.makeIterator() while let element = iterator.next() { result[element.key] = transform(element.value) } return result } } [1: 1, 2: 2].map { $0.description } // [1: "1", 2: "2"]
  13. ॳظ஋ͷཁΒͳ͍
 Sequence#reduce ࡞ͬͯΈΑ͏ extension Sequence { func reduce(_ nextPartialResult: (_

    result: Iterator.Element, _ next: Iterator.Element) -> Iterator.Element) -> Iterator.Element? { var iterator = self.makeIterator() guard var result = iterator.next() else { return nil } while let next = iterator.next() { result = nextPartialResult(result, next) } return result } } [1, 2, 3, 4, 5].reduce(*) // 120
  14. ࡞ͬͯΈΔͱ໘നͦ͏ͳ΋ͷ • ഑ྻ͔ΒάϧʔϓͰ·ͱΊͨೋ࣍ݩ഑ྻΛ࡞Δؔ਺ • ԿΛ৚݅ʹͯ͠άϧʔϓΛ·ͱΊΔ͔ • ͲͷϓϩτίϧʢSequence ͔ Collection ͔Կ͔ʣΛ֦ு͢΂͖͔

    • ແݶʹ޿͕Δ Sequence • Ͳ͏͢Ε͹ Sequence ͕ແݶʹͳΔͷ͔ • MutableCollection ʹରԠ͢Δ͚Ͳ RangeReplaceableCollection ʹର Ԡ͠ͳ͍ू߹ • etc...