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

Swift の表現力を楽しもう!

Swift の表現力を楽しもう!

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

Yuka Ezura

April 27, 2016
Tweet

More Decks by Yuka Ezura

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. Represents a discrete value in a series QSPUPDPM'PSXBSE*OEFY5ZQF 5 6

    7 … … Equatable
 +
 func successor() -> Self
  4. 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
  5. 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 ྫ
  6. 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
  7. let loopRange: Range<LoopIndex> = ͭ͡͸ɻɻ ࣗ࡞ͷܕ 5...2 extension LoopIndex :

    IntegerLiteralConvertible ॳظԽ࣌ʹ
 ੔਺ϦςϥϧΛड͚औΕΔ
  8. 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 ࣗ࡞ͷܕ
  9. 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,
  10. 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,
  11. enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,

    Saturday } 8FFL Sunday Monday Tuesday … Saturday 8FFL
  12. 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
  13. 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 ࣗ࡞ͷܕ
  14. 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
  15. 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
  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 // ऴ୺ } } } 8FFL'PSXBSE*OEFY5ZQF Sunday Monday Tuesday … Saturday 8FFL
  17. 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