Swift の世界観と
その秩序

Swift の世界観と
その秩序

6a55a7fb19c1fa78e86ddca7a69db088?s=128

Yuka Ezura

March 29, 2016
Tweet

Transcript

  1. 5.

    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. 16.

    let elements: [CupSize] = [.Short, .Grande, .Short]
 
 // error:

    missing argument for parameter #1 in call elements.sort() ྫ͑͹ɾɾɾ
  3. 17.

    enum CupSize: Int { case Short = 0, Tall, Grande,

    Venti } extension CupSize : Comparable {}
 // ུ let cupSizes: [CupSize] = [.Short, .Grande, .Short] cupSizes.sort() // [Short, Short, Grande] ഑ྻͷཁૉ͕ Comparable ʹ४ڌ͍ͯ͠Δͱɺ
 ͜ͷॻ͖ํͰ sort ͕࢖͑ΔΑ͏ʹͳΔ ྫ͑͹ɾɾɾ
  4. 21.

    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
  5. 22.

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

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort()
  6. 23.

    SequenceType for element in [1, 2, 3] { /* do

    something */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort() ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ Ұͭͷճ౴
 ഑ྻ͸ཁૉΛҰͭҰͭऔΓग़ͤΔ͔Β
  7. 24.

    let array: Array = [1, 2, 3] struct Array<Element> :

    CollectionType
 SequenceType Λܧঝ for element in array { /* do something */ } array.forEach { _ in /* do something */ }
 
 array.sort(<)
  8. 27.

    protocol SequenceType { associatedtype Generator : GeneratorType public func generate()

    -> Self.Generator
 public func map<T> … ུ public func forEach … ུ
 // ུ } extension SequenceType where Self.Generator.Element : Comparable { public func sort() -> [Self.Generator.Element] } SequenceType required
  9. 28.

    extension MySequenceTypeStruct: SequenceType { func generate() -> AnyGenerator<Int> { var

    iteration = 0 return AnyGenerator { defer { iteration += 1 } guard iteration < 3 else { return nil } return iteration } } } let sequenceTypeStruct = MySequenceTypeStruct()
 
 for element in sequenceTypeStruct { print(element) } sequenceTypeStruct.sort() SequenceType
 ʹ४ڌ SequenceType
 ͷೳྗΛߦ࢖
  10. 30.

    protocol SequenceType { associatedtype Generator : GeneratorType public func generate()

    -> Self.Generator
 public func map<T> … ུ public func forEach … ུ } extension SequenceType where Self.Generator.Element : Comparable { public func sort() -> [Self.Generator.Element] } SequenceType
 Ұ࿈ͷ஋Λදݱ ஋ΛҰͭҰͭॱ൪ʹ
 औΓग़ͨ͢ΊͷػߏΛ࡞੒͢Δ
 ϝιου “Ұ࿈ͷ஋Λදݱ͢Δ” 
 ͜ͱʹΑͬͯͰ͖Δೳྗ
  11. 31.

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

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort() ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ
  12. 32.

    ˛ ഑ྻ͔ͩΒ for … in ΍ forEach ͕࢖͑Δ
 ⭕️ SequenceType

    ʹ४ڌ͍ͯ͠Δ͔Β for … in ΍ 
 forEach ͕࢖͑Δ ⭕ ഑ྻ͕ʮҰ࿈ͷ஋Λදݱ͢Δʯ΋ͷ͔ͩΒɺ
 ͦͷੑ࣭͕࢖͑Δ ݫີʹ͸ Array ͸ SequenceType Λܧঝͨ͠
 CollectionType ʹ४ڌ͍ͯ͠·͢
  13. 33.

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

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort() SequenceType ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ
  14. 34.

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

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort()
 
 [1, 2, 3].count ❌ SequenceType
 ⭕️ CollectionType ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ
  15. 36.

    AbsoluteValuable AnyCollectionType AnyObject ArrayLiteralConvertible BidirectionalIndexType BitwiseOperationsType BooleanLiteralConvertible BooleanType CVarArgType CollectionType

    Comparable CustomDebugStringConverti ble CustomLeafReflectable CustomPlaygroundQuickLoo kable CustomReflectable CustomStringConvertible DictionaryLiteralConvertible Equatable ErrorType ExtendedGraphemeClusterL iteralConvertible FloatLiteralConvertible FloatingPointType ForwardIndexType GeneratorType Hashable Indexable IntegerArithmeticType IntegerLiteralConvertible IntegerType IntervalType LazyCollectionType LazySequenceType MirrorPathType MutableCollectionType MutableIndexable MutableSliceable NilLiteralConvertible OptionSetType OutputStreamType RandomAccessIndexType RangeReplaceableCollectio nType RawRepresentable ReverseIndexType SequenceType SetAlgebraType SignedIntegerType SignedNumberType Streamable Strideable StringInterpolationConvertibl e StringLiteralConvertible UnicodeCodecType UnicodeScalarLiteralConvert ible UnsignedIntegerType _ArrayBufferType _ArrayType _CVarArgAlignedType _CVarArgPassedAsDouble _CollectionWrapperType _DestructorSafeContainer _DisallowMixedSignArithmeti c _Incrementable _IntegerArithmeticType _IntegerType _MirrorType _NSArrayCoreType _NSCopyingType _NSDictionaryCoreType _NSDictionaryType _NSEnumeratorType _NSFastEnumerationType _NSSetCoreType _NSSetType _NSStringCoreType _ObjectiveCBridgeable _PointerType _RandomAccessAmbiguity _Reflectable _ReverseCollectionType _SequenceWrapperType _ShadowProtocol _SignedIntegerType _SinkType _StringElementType Swift Standard Library Reference ʹهࡌ͞Ε͍ͯͳ͍΋ͷؚΉ
  16. 41.

    Objective-C Ͱ΋ɺNSFastEnumeration ʹ४ڌ͢Ε͹ 
 for … in ͕࢖͑Δ @protocol NSFastEnumeration

    - (NSUInteger)countByEnumeratingWithState: (NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len; @end
  17. 42.

    • for … in ͸ SequenceType ͷҰػೳ • ͦͷੑ࣭ɾ֓೦͔ͩΒͦ͜ͷೳྗΛϓϩτίϧଆͰ ఆٛ


    (ʮͦͷੑ࣭ɾ֓೦ͳΒ͜͏ಇ͘ΑͶʯ͕هड़͞Ε͍ͯΔ)
 forEach, sort, map, … Swift ͱ Objective-C ͰҟͳΔͱ͜Ζ
  18. 44.

    • ಛఆͷλεΫɺػೳΛຬͨͨ͢Ίʹඞཁͳཁ݅Λ
 એݴ • ֦ு͢Δ͜ͱʹΑͬͯඞཁཁ݅ͷҰ෦Λຬͨͨ͠Γɺ ४ڌͨ͠ܕ͕࢖͑ΔػೳΛఏڙͰ͖Δ ࢀর: The Swift Programming

    Language (Swift 2.2) The Swift Programming Language ΑΓ ඪ४ϥΠϒϥϦͷϓϩτίϧͷઃܭ (஫: ࢲݟ) ͦͷੑ࣭ɾ ֓೦ͨΒ͠ΊΔ΋ͷͷએݴ ͦͷੑ࣭ɾ֓೦͔ͩΒͦ͜ͷೳྗ ͦͷੑ࣭ɾ֓೦ͨΒ͠ΊΔͨ Ίͷಛੑ
  19. 46.

    protocol SequenceType { associatedtype Generator : GeneratorType public func generate()

    -> Self.Generator
 public func map<T> … ུ public func forEach … ུ } extension SequenceType where Self.Generator.Element : Comparable { public func sort() -> [Self.Generator.Element] } SequenceType
 Ұ࿈ͷ஋Λදݱ ֓೦ɾੑ࣭Λදݱ͢ΔͨΊཁ݅
 ϝιουɾؔ਺ɾϓϩύςΟͳͲ ͦͷ֓೦ɾੑ࣭Ͱ͋Δ͕
 Ώ͑ʹ࣋ͭೳྗ ͦͷ֓೦ɾੑ࣭Ͱ͋Δ͕
 Ώ͑ʹ࣋ͭೳྗ
  20. 49.

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

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort()
 
 [1, 2, 3].count SequenceType ❌ SequenceType
 ⭕️ CollectionType
  21. 55.

    public protocol CollectionType : Indexable, SequenceType { associatedtype Generator :

    GeneratorType = 
 IndexingGenerator<Self> public func generate() -> Self.Generator // ... public var count: Self.Index.Distance { get } public var first: Self.Generator.Element? { get } } // ... ֓೦ɾੑ࣭Λදݱ ֓೦ɾੑ࣭Λදݱ ͦͷੑ࣭ɾ֓೦
 ͔ͩΒͦ͜ͷೳྗ ্Ґ֓೦
  22. 59.

    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
  23. 60.

    enum CupSize: Int { case Short = 0, Tall, Grande,

    Venti } extension CupSize : Comparable {}
 // ུ let cupSizes: [CupSize] = [.Short, .Grande, .Short] cupSizes.sort() // [Short, Short, Grande] let numbers = [2, 5, 1]
 numbers.sort() // [1, 2, 5] Comparable
  24. 64.

    ࢀߟ • The Swift Programming Language (Swift 2.2)
 Language Guide

    / Protocols
 https://developer.apple.com/library/ios/documentation/Swift/ Conceptual/Swift_Programming_Language/Protocols.html#// apple_ref/doc/uid/TP40014097-CH25-ID267 • Swift Standard Library Reference
 The Swift Standard Library
 https://developer.apple.com/library/ios/documentation/ General/Reference/SwiftStandardLibraryReference/#protocols • SwiftDoc.org
 http://swiftdoc.org/v2.2/protocol