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

Swift の世界観と
その秩序

Swift の世界観と
その秩序

Yuka Ezura

March 29, 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. let elements: [CupSize] = [.Short, .Grande, .Short]
 
 // error:

    missing argument for parameter #1 in call elements.sort() ྫ͑͹ɾɾɾ
  3. 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. 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. for element in [1, 2, 3] { /* do something

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

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

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort() ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ
  12. ˛ ഑ྻ͔ͩΒ for … in ΍ forEach ͕࢖͑Δ
 ⭕️ SequenceType

    ʹ४ڌ͍ͯ͠Δ͔Β for … in ΍ 
 forEach ͕࢖͑Δ ⭕ ഑ྻ͕ʮҰ࿈ͷ஋Λදݱ͢Δʯ΋ͷ͔ͩΒɺ
 ͦͷੑ࣭͕࢖͑Δ ݫີʹ͸ Array ͸ SequenceType Λܧঝͨ͠
 CollectionType ʹ४ڌ͍ͯ͠·͢
  13. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort() SequenceType ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ
  14. 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. 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. Objective-C Ͱ΋ɺNSFastEnumeration ʹ४ڌ͢Ε͹ 
 for … in ͕࢖͑Δ @protocol NSFastEnumeration

    - (NSUInteger)countByEnumeratingWithState: (NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len; @end
  17. • for … in ͸ SequenceType ͷҰػೳ • ͦͷੑ࣭ɾ֓೦͔ͩΒͦ͜ͷೳྗΛϓϩτίϧଆͰ ఆٛ


    (ʮͦͷੑ࣭ɾ֓೦ͳΒ͜͏ಇ͘ΑͶʯ͕هड़͞Ε͍ͯΔ)
 forEach, sort, map, … Swift ͱ Objective-C ͰҟͳΔͱ͜Ζ
  18. • ಛఆͷλεΫɺػೳΛຬͨͨ͢Ίʹඞཁͳཁ݅Λ
 એݴ • ֦ு͢Δ͜ͱʹΑͬͯඞཁཁ݅ͷҰ෦Λຬͨͨ͠Γɺ ४ڌͨ͠ܕ͕࢖͑ΔػೳΛఏڙͰ͖Δ ࢀর: The Swift Programming

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