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

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

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

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

Af64bc38c0ffcfcabdf430759ee491ce?s=128

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. Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection

  4. Sequence • ෺ͷϦετ • ༗ݶ΋ΞϦɺແݶ΋ΞϦ • ΠςϨʔλʔ͸܁Γฦͯ͠࢖͑Δͱ͸ݶΒͳ͍

  5. Sequence protocol Sequence { associatedtype Iterator: IteratorProtocol func makeIterator() ->

    Iterator }
  6. Collection • Sequence ͔Βܧঝ • ৗʹ༗ݶ • ΠςϨʔλʔ͸܁Γฦͯ͠࢖͑Δ

  7. 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 }
  8. BidirectionalCollection • Collection ͔Βܧঝ • ਖ਼ํ޲͚ͩͰͳ͘ٯํ޲͔Β΋਺͑ΒΕΔ

  9. protocol BidirectionalCollection: Collection { func index(before index: Index) -> Index

    } BidirectionalCollection protocol Collection { func index(after index: Index) -> Index }
  10. RandomAccessCollection • BidirectionalCollection ͔Βܧঝ • ೚ҙͷΠϯσοΫεʹૉૣ͘ΞΫηεͰ͖Δ • ೚ҙͷ 2 ΠϯσοΫεؒͷڑ཭͕ૉૣ͘ٻΊΒΕΔ

  11. 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
  12. Complexity: O(1) ͱ͸ • Φʔμʔه๏ʮO(1)ʯ • σʔλྔʢཁૉ਺ʣʹࠨӈͤͣɺৗʹҰఆ࣌ؒͰߦ͏ • ΞϧΰϦζϜͷ଎͞ΛධՁ͢ΔࢦඪͷҰͭ •

    ଞʹ΋ʮO(n)ʯ΍ʮO(log n)ʯͳͲ
  13. 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
  14. // RandomAccessCollection ʹ४ڌ͠ͳ͍ྫ protocol String.CharacterView: BidirectionalCollection { /// - Complexity:

    O(n) func index(_ i: String.Index, offsetBy n: String.IndexDistance) -> String.Index }
  15. ʮจࣈ਺ʯͬͯͳ͊ʹʁ ʙString, NSString, Unicodeͷجຊʙ by takasek 2016/11/16 ୈ5ճελʔτΞοϓiOSษڧձ 1 https://speakerdeck.com/takasek/wen-zi-shu-tutenaani-string-

    nsstring-unicodefalseji-ben
  16. ࠓ๺࢈ۀ RandomAccessCollection ͱ͸ ू߹಺ͷ ϥϯμϜͳཁૉʹ ૉૣ͘ΞΫηεͰ͖Δ ू߹ͷ͜ͱ

  17. MutableCollection • Collection ͔Βܧঝ • ಛఆཁૉʹର͠ήολʔͱηολʔ྆ํ༻ҙ͞Ε͍ͯΔ

  18. protocol MutableCollection: Collection { subscript(position: Index) -> Iterator.Element { get

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

    Swift" str.characters[str.startIndex] = "H" // error: cannot assign through subscript: subscript is get- only
  20. ʮจࣈ਺ʯͬͯͳ͊ʹʁ ʙString, NSString, Unicodeͷجຊʙ by takasek 2016/11/16 ୈ5ճελʔτΞοϓiOSษڧձ 1 https://speakerdeck.com/takasek/wen-zi-shu-tutenaani-string-

    nsstring-unicodefalseji-ben
  21. ࠓ๺࢈ۀ MutableCollection ͱ͸ ू߹಺ͷ ಛఆͳཁૉΛ ஔ͖׵͑ΒΕΔ ू߹ͷ͜ͱ

  22. RangeReplaceableCollection

  23. Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection

  24. RangeReplaceableCollection • RandomAccessCollection ͔Βܧঝ !

  25. RangeReplaceableCollection • Collection ͔Βܧঝ • ू߹ͷཁૉ਺Λมߋ͢ΔΠϯλʔϑΣΠεΛఏڙ

  26. 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 } }
  27. ࠓ๺࢈ۀ RangeReplaceableCollection ͱ͸ ू߹ͷ ཁૉΛ ఴ࡟Ͱ͖Δ ू߹ͷ͜ͱ

  28. Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection

  29. Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection

  30. Θ͔ͬͨΘ͔ͬͨɻ
 ͰɺԿͷ໾ʹཱͭͷʁ "

  31. ࣮͸ҙ֎ͱ୯७ͩͬͨ
 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 } }
  32. ໭Γ஋͕ࣙॻܕͷ
 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"]
  33. ॳظ஋ͷཁΒͳ͍
 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
  34. ࡞ͬͯΈΔͱ໘നͦ͏ͳ΋ͷ • ഑ྻ͔ΒάϧʔϓͰ·ͱΊͨೋ࣍ݩ഑ྻΛ࡞Δؔ਺ • ԿΛ৚݅ʹͯ͠άϧʔϓΛ·ͱΊΔ͔ • ͲͷϓϩτίϧʢSequence ͔ Collection ͔Կ͔ʣΛ֦ு͢΂͖͔

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

  36. None
  37. None
  38. !ank y"