Slide 1

Slide 1 text

Everything You Ever Wanted To Know About Sequence and Collection for AKIBA.swift×SwiftѪ޷ձ vol2 Alm ost supplem ent

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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 }

Slide 8

Slide 8 text

BidirectionalCollection • Collection ͔Βܧঝ • ਖ਼ํ޲͚ͩͰͳ͘ٯํ޲͔Β΋਺͑ΒΕΔ

Slide 9

Slide 9 text

protocol BidirectionalCollection: Collection { func index(before index: Index) -> Index } BidirectionalCollection protocol Collection { func index(after index: Index) -> Index }

Slide 10

Slide 10 text

RandomAccessCollection • BidirectionalCollection ͔Βܧঝ • ೚ҙͷΠϯσοΫεʹૉૣ͘ΞΫηεͰ͖Δ • ೚ҙͷ 2 ΠϯσοΫεؒͷڑ཭͕ૉૣ͘ٻΊΒΕΔ

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Complexity: O(1) ͱ͸ • Φʔμʔه๏ʮO(1)ʯ • σʔλྔʢཁૉ਺ʣʹࠨӈͤͣɺৗʹҰఆ࣌ؒͰߦ͏ • ΞϧΰϦζϜͷ଎͞ΛධՁ͢ΔࢦඪͷҰͭ • ଞʹ΋ʮO(n)ʯ΍ʮO(log n)ʯͳͲ

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

// RandomAccessCollection ʹ४ڌ͠ͳ͍ྫ protocol String.CharacterView: BidirectionalCollection { /// - Complexity: O(n) func index(_ i: String.Index, offsetBy n: String.IndexDistance) -> String.Index }

Slide 15

Slide 15 text

ʮจࣈ਺ʯͬͯͳ͊ʹʁ ʙString, NSString, Unicodeͷجຊʙ by takasek 2016/11/16 ୈ5ճελʔτΞοϓiOSษڧձ 1 https://speakerdeck.com/takasek/wen-zi-shu-tutenaani-string- nsstring-unicodefalseji-ben

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

// MutableCollection ʹ४ڌ͠ͳ͍ྫ protocol String.CharacterView: BidirectionalCollection var str = "Hello, Swift" str.characters[str.startIndex] = "H" // error: cannot assign through subscript: subscript is get- only

Slide 20

Slide 20 text

ʮจࣈ਺ʯͬͯͳ͊ʹʁ ʙString, NSString, Unicodeͷجຊʙ by takasek 2016/11/16 ୈ5ճελʔτΞοϓiOSษڧձ 1 https://speakerdeck.com/takasek/wen-zi-shu-tutenaani-string- nsstring-unicodefalseji-ben

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

RangeReplaceableCollection

Slide 23

Slide 23 text

Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection

Slide 24

Slide 24 text

RangeReplaceableCollection • RandomAccessCollection ͔Βܧঝ !

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

protocol RangeReplaceableCollection: Collection { mutating func replaceSubrange(_ subrange: Range, 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 } }

Slide 27

Slide 27 text

ࠓ๺࢈ۀ RangeReplaceableCollection ͱ͸ ू߹ͷ ཁૉΛ ఴ࡟Ͱ͖Δ ू߹ͷ͜ͱ

Slide 28

Slide 28 text

Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection

Slide 29

Slide 29 text

Sequence Collection BidirectionalCollection RandomAccessCollection RangeReplaceableCollection MutableCollection

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

࣮͸ҙ֎ͱ୯७ͩͬͨ
 Sequence#map ͷ࣮ݱ extension Sequence { // ࣮ࡍͷ࣮૷ͱ͸গʑҧ͏ɺڵຯ͋Δํ͸ԼهͷϦϯΫ͔Β೷͍ͯΈͯͶ // https://github.com/apple/swift/blob/master/stdlib/public/ core/Sequence.swift func map(_ transform: (Iterator.Element) -> T) -> [T] { var result: [T] = [] var iterator = self.makeIterator() while let element = iterator.next() { result.append(transform(element)) } return result } }

Slide 32

Slide 32 text

໭Γ஋͕ࣙॻܕͷ
 Dictionary#map ࡞ͬͯΈΑ͏ extension Dictionary { func map(_ 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"]

Slide 33

Slide 33 text

ॳظ஋ͷཁΒͳ͍
 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

Slide 34

Slide 34 text

࡞ͬͯΈΔͱ໘നͦ͏ͳ΋ͷ • ഑ྻ͔ΒάϧʔϓͰ·ͱΊͨೋ࣍ݩ഑ྻΛ࡞Δؔ਺ • ԿΛ৚݅ʹͯ͠άϧʔϓΛ·ͱΊΔ͔ • ͲͷϓϩτίϧʢSequence ͔ Collection ͔Կ͔ʣΛ֦ு͢΂͖͔ • ແݶʹ޿͕Δ Sequence • Ͳ͏͢Ε͹ Sequence ͕ແݶʹͳΔͷ͔ • MutableCollection ʹରԠ͢Δ͚Ͳ RangeReplaceableCollection ʹର Ԡ͠ͳ͍ू߹ • etc...

Slide 35

Slide 35 text

⺄⇾ْطٳ

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

!ank y"