Swift の表現力を楽しもう!

Swift の表現力を楽しもう!

標準ライブラリ遊び
「5 ... 1」を紐解きながら標準ライブラリを理解していきましょう。

6a55a7fb19c1fa78e86ddca7a69db088?s=128

Yuka Ezura

April 27, 2016
Tweet

Transcript

  1. 4XJGUͰ
 ؾ࣋ͪྑ͘։ൃ͢Δ !)FBMUI4XJGU.FFUVQ F[VSB

  2. 4XJGUͷදݱྗɺؾ࣋ͪྑ͍ʂʂ

  3. 5...2

  4. print(self) • ៸໘༑߳ (ezura) • ޷͖ͳ΋ͷ: protocol, enum • τϥΠϑΥʔτۈ຿

    • twitter: @eduraaa
  5. Ұϲ݄΄Ͳલʹ4XJGUѪ޷ձͱ͍͏ษڧձͰ
 ͓࿩͍͍ͤͯͨͩͨ͜͠͞ͱ

  6. https://speakerdeck.com/ezura/swift-falseshi-jie-guan-to-sofalsezhi-xu 4XJGUͷੈք؍ͱͦͷடং 4XJGUͷඪ४ϥΠϒϥϦ ͱͷ஥ྑ͘ͳΓํ

  7. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ } 1 ... 3 if array.isEmpty { /* do something */ } SequenceType
 (CollectionType) ForwardIndexType BooleanType Comparable IntegerLiteral
 Convertible ArrayLiteral
 Convertible
  8. 4XJGUΛܗ࡞Δཁૉɺඪ४ϥΠϒϥϦ ཧղ͢Ε͹ Swift ʹௐ࿨ͨ͠ίʔυΛؾ࣋ͪྑ͘ॻ͚ͦ͏ʂ

  9. ෼Ͱඪ४ϥΠϒϥϦͱ༡ΜͰΈΑ͏

  10. 5 ͔Β 2 ͷൣғ
 (Range) 5...2

  11. for index in 1...5 { print("\(index) times 5 is \(index

    * 5)") } let names = ["Anna", "Alex", "Brian", "Jack"] let count = names.count for i in 0..<count { print("Person \(i + 1) is called \(names[i])") } 3BOHF
  12. TUSVDU3BOHF&MFNFOU'PSXBSE*OEFY5ZQF *OU͸'PSXBSE*OEFY5ZQF
 ʹ४ڌ͍ͯ͠ΔͷͰѻ͑ͨ ݫີʹ͸ɺ*OU4JHOFE*OUFHFS5ZQFͰ͢ɻ
 ܧঝؔ܎͸4JHOFE*OUFHFS5ZQF*OUFHFS5ZQF 3BOEPN"DDFTT*OEFY5ZQF#JEJSFDUJPOBM*OEFY5ZQF 'PSXBSE*OEFY5ZQF 1...10 &MFNFOU

  13. Represents a discrete value in a series QSPUPDPM'PSXBSE*OEFY5ZQF Value Value

    Value … …
  14. Represents a discrete value in a series QSPUPDPM'PSXBSE*OEFY5ZQF 5 6

    7 … … Equatable
 +
 func successor() -> Self
  15. ࣗ࡞ͷܕʹ'PSXBSE*OEFY5ZQFΛ
 ४ڌͤ͞Δྫ

  16. enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,

    Saturday } extension Week: ForwardIndexType { // ͜ͷ৔߹ͷ enum ͸ Hashable ʹ҉໧తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return self // ऴ୺ } } } 'PSXBSE*OEFY5ZQF ྫ Sunday Monday Tuesday … Saturday 8FFL
  17. enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,

    Saturday } extension Week: ForwardIndexType { // ͜ͷ৔߹ͷ enum ͸ Hashable ʹ҉໧తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return self // ऴ୺ } } } Week.Friday == Week.Friday // true (Week.Monday ... .Friday).contains(.Wednesday) // true 'PSXBSE*OEFY5ZQF ྫ
  18. Ұ࿈ͷۭؒͷதͷ཭ࢄ஋ Sunday Monday Tuesday … Saturday ͜ͷੑ࣭Λදݱ͢ΔͨΊͷཁ݅ l࣍ͷ஋Λࣔ͢z

  19. enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,

    Saturday } extension Week: ForwardIndexType { // ͜ͷ৔߹ͷ enum ͸ Hashable ʹ҉໧తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return .Sunday // ࠷ॳʹ໭Δ } } } (Week.Saturday ... .Monday).contains(.Sunday) // true 'PSXBSE*OEFY5ZQF ྫ Sunday Monday Tuesday … Saturday
  20. 5...2 ͭ͡͸ɻɻ

  21. let loopRange: Range<LoopIndex> = 5...2 ͭ͡͸ɻɻ ࣗ࡞ͷܕ

  22. let loopRange: Range<LoopIndex> = LoopIndex(5)...LoopIndex(2) ͭ͡͸ɻɻ ࣗ࡞ͷܕ

  23. let loopRange: Range<LoopIndex> = ͭ͡͸ɻɻ ࣗ࡞ͷܕ 5...2 extension LoopIndex :

    IntegerLiteralConvertible ॳظԽ࣌ʹ
 ੔਺ϦςϥϧΛड͚औΕΔ
  24. Conforming types can be initialized with integer literals. QSPUPDPM*OUFHFS-JUFSBM$POWFSUJCMF

  25. struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {

    self.rawValue = rawValue } } // OK let loopIndex = LoopIndex(1) // ௨ৗ͸Ͱ͖ͳ͍ // error: cannot convert value of type 'Int' to specified type 'LoopIndex' let beforeConform: LoopIndex = 5 -PPQ*OEFY ࣗ࡞ͷܕ
  26. struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {

    self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } let loopIndex: LoopIndex = 1 -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF 0,
  27. struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {

    self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } extension LoopIndex: ForwardIndexType { // ུ } let loopIndex: LoopIndex = 1 let loopRange : Range<LoopIndex> = 5...2 loopRange.contains(1) // true -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF 0,
  28. let loopRange: Range<LoopIndex> = 5...2

  29. 5...2 ஫τϦοΩʔͳ͜ͱΛ͢Δͱ͖͸৻ॏʹʂʂ

  30. ͪΐͬͱৼΓฦͬͯΈ·͠ΐ͏

  31. QSPUPDPM'PSXBSE*OEFY5ZQF QSPUPDPM*OUFHFS-JUFSBM$POWFSUJCMF

  32. enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,

    Saturday } 8FFL Sunday Monday Tuesday … Saturday 8FFL
  33. enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,

    Saturday } extension Week: ForwardIndexType { // ͜ͷ৔߹ͷ enum ͸ Hashable ʹ҉໧తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return self // ऴ୺ } } } 8FFL'PSXBSE*OEFY5ZQF Sunday Monday Tuesday … Saturday 8FFL
  34. struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {

    self.rawValue = rawValue } } // OK let loopIndex = LoopIndex(1) // ௨ৗ͸Ͱ͖ͳ͍ // error: cannot convert value of type 'Int' to specified type 'LoopIndex' let beforeConform: LoopIndex = 5 -PPQ*OEFY ࣗ࡞ͷܕ
  35. struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {

    self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } let loopIndex: LoopIndex = 1 -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF -PPQ*OEFY 1
  36. struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {

    self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } let loopIndex: LoopIndex = 1 -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF -PPQ*OEFY 1
  37. ⭕࢖͑Δػೳ͕૿͑Δ

  38. ΋ͬͱخ͍͜͠ͱ

  39. ϓϩτίϧʹ४ڌ͢Δ͜ͱͰ
 ܕ͕ද֓͢೦ΛΑΓ໌֬ʹදݱͰ͖Δʂ

  40. enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,

    Saturday } extension Week: ForwardIndexType { // ͜ͷ৔߹ͷ enum ͸ Hashable ʹ҉໧తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return self // ऴ୺ } } } 8FFL'PSXBSE*OEFY5ZQF Sunday Monday Tuesday … Saturday 8FFL
  41. struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {

    self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } let loopIndex: LoopIndex = 1 -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF -PPQ*OEFY 1
  42. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

  43. ࢀߟ w 4XJGU4UBOEBSE-JCSBSZ3FGFSFODF
 IUUQTEFWFMPQFSBQQMFDPNMJCSBSZJPT EPDVNFOUBUJPO(FOFSBM3FGFSFODF 4XJGU4UBOEBSE-JCSBSZ3FGFSFODF w 4XJGU%PDPSH
 IUUQTXJGUEPDPSHWQSPUPDPM'PSXBSE*OEFY5ZQF w

    Ϧςϥϧͱܕͷ࿩
 IUUQXXXTMJEFTIBSFOFUUPNPIJSPLVNBHBJ TXJGU