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

Sean Levin - The Wacky Wild World of Swift Collection Types

Sean Levin - The Wacky Wild World of Swift Collection Types

In Objective-C we have NSArrays.

In Swift we have Arrays, Generators, FlattenBidirectionCollection, LazySequenceType, ReverseRandomAccessIndex, and 62 other types directly related to solving all your fine grained ordered collection needs.

Why do all of these exist? How can we even begin to remember when to use which? What insane person could possibly need this many?

In this talk Sean will dive into some stories and theories, attempting to answer these very questions. We will be covering topics in generic programming, performance, and programming languages on down to the very fundamentals of numbers and math. You will come away with some juicy crowd pleasing knowledge you can take home to impress your friends, family, and coworkers.

Recording from the March 2016 Brooklyn Swift Developers Meetup: http://meetup.com/Brooklyn-Swift-Developers/events/229464421/

Video: https://vimeo.com/160350970

More Decks by Brooklyn Swift Developers Meetup

Other Decks in Technology

Transcript

  1. SWIFT 2.2 LIBRARY AnyBidirectionalCollection AnyBidirectionalIndex AnyForwardCollection AnyForwardIndex AnyGenerator AnyRandomAccessCollection AnyRandomAccessIndex

    AnySequence Array ArraySlice AutoreleasingUnsafeMutablePointer Bit Bool COpaquePointer CVaListPointer Character ClosedInterval CodeUnit CollectionOfOne ContiguousArray Dictionary DictionaryGenerator DictionaryIndex DictionaryLiteral Double EmptyCollection EmptyGenerator EnumerateGenerator EnumerateSequence FlattenBidirectionalCollection FlattenBidirectionalCollectionIndex FlattenCollection FlattenCollectionIndex FlattenGenerator FlattenSequence PermutationGenerator PlaygroundQuickLook Process Range RangeGenerator RawByte Repeat ReverseCollection ReverseIndex ReverseRandomAccessCollection ReverseRandomAccessIndex Set SetGenerator SetIndex Slice StaticString StrideThrough StrideThroughGenerator StrideTo StrideToGenerator String String.CharacterView String.CharacterView.Index String.UTF16View String.UTF16View.Index String.UTF8View String.UTF8View.Index String.UnicodeScalarView String.UnicodeScalarView.Generator String.UnicodeScalarView.Index String.UnicodeScalarView.UnicodeScalarIndex UInt UInt16 UInt32 UInt64 FloatLiteralConvertible FloatingPointType ForwardIndexType GeneratorType Hashable Indexable IntegerArithmeticType IntegerLiteralConvertible IntegerType IntervalType LazyCollectionType LazySequenceType MirrorPathType MutableCollectionType MutableIndexable MutableSliceable NilLiteralConvertible OptionSetType OutputStreamType RandomAccessIndexType RangeReplaceableCollectionType RawRepresentable ReverseIndexType SequenceType SetAlgebraType SignedIntegerType SignedNumberType Streamable Strideable StringInterpolationConvertible StringLiteralConvertible UnicodeCodecType UnicodeScalarLiteralConvertible UnsignedIntegerType Float Float80 FloatingPointClassification GeneratorOfOne GeneratorSequence HalfOpenInterval ImplicitlyUnwrappedOptional IndexingGenerator Int Int16 Int32 Int64 Int8 IntMax JoinGenerator JoinSequence LazyCollection LazyFilterCollection LazyFilterGenerator LazyFilterIndex LazyFilterSequence LazyMapCollection LazyMapGenerator LazyMapSequence LazySequence ManagedBuffer ManagedBufferPointer ManagedProtoBuffer Mirror Mirror.AncestorRepresentation Mirror.DisplayStyle MutableSlice NonObjectiveCBase ObjectIdentifier Optional UInt8 UTF16 UTF32 UTF8 UnicodeDecodingResult UnicodeScalar Unmanaged UnsafeBufferPointer UnsafeBufferPointerGenerator UnsafeMutableBufferPointer UnsafeMutablePointer UnsafePointer VaListBuilder Zip2Generator Zip2Sequence AbsoluteValuable AnyCollectionType AnyObject ArrayLiteralConvertible BidirectionalIndexType BitwiseOperationsType BooleanLiteralConvertible BooleanType CVarArgType CollectionType Comparable CustomDebugStringConvertible CustomLeafReflectable CustomPlaygroundQuickLookable CustomReflectable CustomStringConvertible DictionaryLiteralConvertible Equatable ErrorType ExtendedGraphemeClusterLiteralConvertible 3
  2. SWIFT 2.2 LIBRARY FloatLiteralConvertible FloatingPointType ForwardIndexType GeneratorType Hashable Indexable IntegerArithmeticType

    IntegerLiteralConvertible IntegerType IntervalType LazyCollectionType LazySequenceType MirrorPathType MutableCollectionType MutableIndexable MutableSliceable NilLiteralConvertible OptionSetType OutputStreamType RandomAccessIndexType RangeReplaceableCollectionType RawRepresentable ReverseIndexType SequenceType SetAlgebraType SignedIntegerType SignedNumberType Streamable Strideable StringInterpolationConvertible StringLiteralConvertible UnicodeCodecType UnicodeScalarLiteralConvertible UnsignedIntegerType UInt8 UTF16 UTF32 UTF8 UnicodeDecodingResult UnicodeScalar Unmanaged UnsafeBufferPointer UnsafeBufferPointerGenerator UnsafeMutableBufferPointer UnsafeMutablePointer UnsafePointer VaListBuilder Zip2Generator Zip2Sequence AbsoluteValuable AnyCollectionType AnyObject ArrayLiteralConvertible BidirectionalIndexType BitwiseOperationsType BooleanLiteralConvertible BooleanType CVarArgType CollectionType Comparable CustomDebugStringConvertible CustomLeafReflectable CustomPlaygroundQuickLookable CustomReflectable CustomStringConvertible DictionaryLiteralConvertible Equatable ErrorType ExtendedGraphemeClusterLiteralConvertible PermutationGenerator PlaygroundQuickLook Process Range RangeGenerator RawByte Repeat ReverseCollection ReverseIndex ReverseRandomAccessCollection ReverseRandomAccessIndex Set SetGenerator SetIndex Slice StaticString StrideThrough StrideThroughGenerator StrideTo StrideToGenerator String String.CharacterView String.CharacterView.Index String.UTF16View String.UTF16View.Index String.UTF8View String.UTF8View.Index String.UnicodeScalarView String.UnicodeScalarView.Generator String.UnicodeScalarView.Index String.UnicodeScalarView.UnicodeScalarIndex UInt UInt16 UInt32 UInt64 Float Float80 FloatingPointClassification GeneratorOfOne GeneratorSequence HalfOpenInterval ImplicitlyUnwrappedOptional IndexingGenerator Int Int16 Int32 Int64 Int8 IntMax JoinGenerator JoinSequence LazyCollection LazyFilterCollection LazyFilterGenerator LazyFilterIndex LazyFilterSequence LazyMapCollection LazyMapGenerator LazyMapSequence LazySequence ManagedBuffer ManagedBufferPointer ManagedProtoBuffer Mirror Mirror.AncestorRepresentation Mirror.DisplayStyle MutableSlice NonObjectiveCBase ObjectIdentifier Optional AnyBidirectionalCollection AnyBidirectionalIndex AnyForwardCollection AnyForwardIndex AnyGenerator AnyRandomAccessCollection AnyRandomAccessIndex AnySequence Array ArraySlice AutoreleasingUnsafeMutablePointer Bit Bool COpaquePointer CVaListPointer Character ClosedInterval CodeUnit CollectionOfOne ContiguousArray Dictionary DictionaryGenerator DictionaryIndex DictionaryLiteral Double EmptyCollection EmptyGenerator EnumerateGenerator EnumerateSequence FlattenBidirectionalCollection FlattenBidirectionalCollectionIndex FlattenCollection FlattenCollectionIndex FlattenGenerator FlattenSequence 4
  3. ORDERED COLLECTION TYPES AnyBidirectionalCollection AnyBidirectionalIndex AnyForwardCollection AnyForwardIndex AnyGenerator AnyRandomAccessCollection AnyRandomAccessIndex

    AnySequence Array ArraySlice CollectionOfOne ContiguousArray DictionaryGenerator DictionaryIndex EmptyCollection EmptyGenerator EnumerateGenerator EnumerateSequence FlattenBidirectionalCollection FlattenBidirectionalCollectionIndex FlattenCollection UnsafeBufferPointer UnsafeBufferPointerGenerator UnsafeMutableBufferPointer Zip2Generator Zip2Sequence AnyCollectionType BidirectionalIndexType CollectionType ForwardIndexType GeneratorType Indexable LazyCollectionType LazySequenceType MutableCollectionType MutableIndexable MutableSliceable RandomAccessIndexType RangeReplaceableCollectionType ReverseIndexType SequenceType FlattenCollectionIndex FlattenGenerator FlattenSequence GeneratorOfOne GeneratorSequence IndexingGenerator JoinGenerator JoinSequence LazyCollection LazyFilterCollection LazyFilterGenerator LazyFilterIndex LazyFilterSequence LazyMapCollection LazyMapGenerator LazyMapSequence LazySequence MutableSlice PermutationGenerator Range RangeGenerator ReverseCollection ReverseIndex ReverseRandomAccessCollection ReverseRandomAccessIndex SetGenerator SetIndex Slice StrideThrough StrideThroughGenerator StrideTo StrideToGenerator String.CharacterView String.CharacterView.Index String.UTF16View String.UTF16View.Index String.UTF8View String.UTF8View.Index String.UnicodeScalarView String.UnicodeScalarView.Generator String.UnicodeScalarView.Index String.UnicodeScalarView.UnicodeScalarIndex 5
  4. EXPECTATIONS > four key concepts > code (to keep it

    real) > a few fun features > Existential Types 13
  5. 1. GENERATOR Let's you step through a group of items

    one at a time protocol GeneratorType { associatedtype Element mutating func next() -> Self.Element? } 15
  6. 16

  7. 17

  8. 18

  9. 19

  10. 2. SEQUENCE for x in [1, 2, 3] { /*

    do stuff */ } for y in [1, 2, 3].enumerate() { /* do stuff */ } for z in "swiftz rules!".UTF8View { /* do stuff */ } for whatever in __________ { /* do stuff */ } 20
  11. 22

  12. 23

  13. GOT THESE FOR FREE dropFirst, dropLast, forEach map, prefix, split,

    suffix, contains, lazy, elementsEqual, enumerate, filter, flatMap, lexicographicalCompare, maxElement, minElement, reduce, reverse, sort, startsWith, joinWithSeparator 24
  14. 25

  15. 26

  16. 3. INDEX Represents a discrete value in a series protocol

    ForwardIndexType : Equatable { func successor() -> Self } func ==(lhs: Self, rhs: Self) -> Bool 27
  17. 28

  18. 29

  19. 30

  20. 31

  21. 32

  22. 4. COLLECTION protocol CollectionType: SequenceType, Indexable protocol Indexable { associatedtype

    Index : ForwardIndexType var endIndex: Self.Index var startIndex : Self.Index subscript(position: Self.Index) -> Self._Element { get } } 33
  23. 4. COLLECTION Its what makes something look like an array.

    let todaysCar = armadaOfBentlys[76] 34
  24. 35

  25. 36

  26. 37

  27. 38

  28. 39

  29. SMART TYPES SMART FUN LazyCollection LazyFilterCollection LazyFilterGenerator LazyFilterIndex LazyFilterSequence LazyMapCollection

    LazyMapGenerator LazyMapSequence LazySequence LazyCollectionType LazySequenceType 44
  30. EXISTENTIAL TYPES func doSomeStuffWithGenerator(g: GeneratorType) { /* do stuff */

    } error: protocol 'GeneratorType' can only be used as a generic constraint because it has Self or associated type requirements protocol GeneratorType { associatedtype Element mutating func next() -> Self.Element? } 48
  31. EXISTENTIAL TYPES struct AnyGenerator2<T> : GeneratorType { typealias Element =

    T var localNext : () -> T? init<Base: GeneratorType where Base.Element == T>(var _ g: Base) { self.localNext = { () -> T? in return g.next() } } mutating func next() -> T? { return self.localNext() } } 49
  32. TYPES COMPOSE FUNNY func flatMap<Intermediate : CollectionType> (transform: (Base.Generator.Elements.Generator.Element) ->

    Intermediate) -> LazyCollection<FlattenCollection <LazyMapCollection<Base.Generator.Elements, Intermediate>>> 50
  33. THESIS That's a lot of abstract stuff especially when we

    have Array It's there when you need it. 52