SequenceType

protocol SequenceType
  • Return the result of repeatedly calling combine with an accumulated value initialized to the first element of self and each element of self, in turn, i.e. return combine(combine(…combine(combine(self[0], self[1]), self[2]),…self[count-2]), self[count-1]). swift [1, 2, 3].reduce(+) // 6

    Declaration

    Swift

    func reduce
        (@noescape combine: (accumulator: Generator.Element, element: Generator.Element) throws -> Generator.Element)
        rethrows -> Generator.Element?
  • Return an array where every value is equal to combine called on the previous element, and the current element. The first element is taken to be initial. “`swift [1, 2, 3].scan(0, combine: +)

    [1, 3, 6] ”`

    Declaration

    Swift

    func scan<T>(var initial: T, @noescape combine: (accumulator: T, element: Generator.Element) throws -> T) rethrows -> [T]
  • Return an array where every value is equal to combine called on the previous element, and the current element. “`swift [1, 2, 3].scan(+)

    [3, 6] ”`

    Declaration

    Swift

    func scan(@noescape combine: (accumulator: Generator.Element, element: Generator.Element) throws -> Generator.Element) rethrows -> [Generator.Element]
  • Returns an array of arrays of n non-overlapping elements of self - Parameter n: The size of the chunk - Precondition: n > 0 - SeeAlso: func window(n: Int) -> [[Self.Generator.Element]] “`swift [1, 2, 3, 4, 5].chunk(2)

    [[1, 2], [3, 4], [5]] ”`

    Declaration

    Swift

    public func chunk(n : Int) -> [[Generator.Element]]

    Parameters

    n

    The size of the chunk

  • Returns an array of arrays of n overlapping elements of self - Parameter n: The size of the window - SeeAlso: func chunk(n: Int) -> [[Self.Generator.Element]] “`swift [1, 2, 3, 4].window(2)

    [[1, 2], [2, 3], [3, 4]] ”`

    Declaration

    Swift

    func window(n : Int) -> [[Generator.Element]]

    Parameters

    n

    The size of the window

  • Returns an array that alternates between successive elements of self and element “`swift [1, 2, 3].interpose(10)

    [1, 10, 2, 10, 3] ”`

    Declaration

    Swift

    func interpose(element: Generator.Element) -> [Generator.Element]
  • Returns an array that alternates between n successive elements of self and element “`swift [1, 2, 3, 4, 5].interpose(10, n: 2)

    [1, 2, 10, 3, 4, 10, 5] ”`

    Declaration

    Swift

    func interpose(element: Generator.Element, n: Int) -> [Generator.Element]
  • Returns an array that alternates between successive elements of self and elements of a colletion “`swift [1, 2, 3].interpose([10, 20])

    [1, 10, 20, 2, 10, 20, 3] ”`

    Declaration

    Swift

    func interpose<C : CollectionType where C.Generator.Element == Generator.Element>
        (col: C) -> [Generator.Element]
  • Returns an array that alternates between n successive elements of self and elements of a colletion “`swift [1, 2, 3, 4, 5].interpose([10, 20], n: 2)

    [1, 2, 10, 20, 3, 4, 10, 20, 5] ”`

    Declaration

    Swift

    func interpose<C : CollectionType where C.Generator.Element == Generator.Element>
        (col: C, n: Int) -> [Generator.Element]
  • Returns an array of self up until the first element that returns false for condition “`swift [1, 2, 3, 4, 5, 2].prefixWhile { $0 < 4 }

    [1, 2, 3] ”`

    Declaration

    Swift

    func prefixWhile(@noescape condition: Generator.Element throws -> Bool) rethrows -> [Generator.Element]
  • Returns an array of self with the first elements that return true for condition dropped “`swift [1, 2, 3, 4, 5, 2].dropWhile { $0 < 4 }

    [4, 5, 2] ”`

    Declaration

    Swift

    func dropWhile(@noescape condition: Generator.Element throws -> Bool) rethrows -> [Generator.Element]
  • Returns a tuple of the prefix and suffix of self, the first element being the prefix up to n.

    Declaration

    Swift

    public func breakAt(n: Int) -> ([Generator.Element],[Generator.Element])
  • Returns a tuple of the prefix and suffix of self, the first element being the prefix up to the first elements of self which returns true for isBreak.

    Declaration

    Swift

    public func breakAt(@noescape isBreak: Generator.Element throws -> Bool)
        rethrows -> ([Generator.Element],[Generator.Element])
  • Returns an array of the permutations of self, ordered lexicographically, according to the closure isOrderedBefore. - Note: The permutations returned follow self, so if self is not the first lexicographically ordered permutation, not all permutations will be returned. “`swift [1, 2, 3].lexPermutations(<)

    [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] swift [1, 2, 3].lexPermutations(>)

    [[1, 2, 3]] ”`

    Declaration

    Swift

    public func lexPermutations
        (isOrderedBefore: (Generator.Element, Generator.Element) -> Bool) -> [[Generator.Element]]
  • Returns an array of the permutations of self. “`swift [1, 2, 3].permutations()

    [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] ”`

    Declaration

    Swift

    public func permutations() -> [[Generator.Element]]
  • Returns an array of the permutations of length n of self. “`swift [1, 2, 3].permutations()

    [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] ”`

    Declaration

    Swift

    public func permutations(n: Int) -> [[Generator.Element]]
  • Returns a lazy sequence of the permutations of self, ordered lexicographically, according to the closure isOrderedBefore. - Note: The permutations returned follow self, so if self is not the first lexicographically ordered permutation, not all permutations will be returned. “`swift lazy([1, 2, 3]).lazyLexPermutations(<)

    [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] swift lazy([1, 2, 3]).lazyLexPermutations(>)

    [1, 2, 3] ”`

    Declaration

    Swift

    public func lazyLexPermutations(isOrderedBefore: (Generator.Element, Generator.Element) -> Bool)
        -> LexPermSeq<[Generator.Element]>
  • Returns a lazy sequence of the permutations of self, ordered lexicographically. - Note: The permutations returned follow self, so if self is not the first lexicographically ordered permutation, not all permutations will be returned. “`swift lazy([1, 2, 3]).lazyLexPermutations()

    [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] swift lazy([3, 2, 1]).lazyLexPermutations()

    [3, 2, 1] ”`

    Declaration

    Swift

    public func lazyLexPermutations()  -> LexPermSeq<[Generator.Element]>
  • Returns a lazy sequence of the permutations of self. - Note: The permutations are lexicographically ordered, based on the indices of self “`swift lazy([1, 2, 3]).lazyPermutations()

    [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] swift lazy([3, 2, 1]).lazyPermutations()

    [3, 2, 1], [3, 1, 2], [2, 3, 1], [2, 1, 3], [1, 3, 2], [1, 2, 3] ”`

    Declaration

    Swift

    public func lazyPermutations() -> LazyMapSequence<LexPermSeq<[Int]>, [Self.Generator.Element]>
  • Returns a cartesian product of self swift [[1, 2], [3, 4]].product() [[1, 3], [1, 4], [2, 3], [2, 4]]

    Declaration

    Swift

    public func product() -> [[Generator.Element.Generator.Element]]
  • Returns a cartesian product of self swift [[1, 2], [3, 4]].product() [[1, 3], [1, 4], [2, 3], [2, 4]]

    Declaration

    Swift

    public func product() -> [[Generator.Element.Generator.Element]]
  • Returns a transposed self “`swift [[1, 2, 3], [1, 2, 3], [1, 2, 3]].transpose()

    [[1, 1, 1], [2, 2, 2], [3, 3, 3]] ”`

    Declaration

    Swift

    func transpose() -> [[Generator.Element.Generator.Element]]
  • Lazy Cartesian Product “`swift [[1, 2], [3, 4]].lazyProduct()

    [1, 3], [1, 4], [2, 3], [2, 4] ”`

    Declaration

    Swift

    func lazyProduct() -> ProdSeq<[Generator.Element.Generator.Element]>
  • Lazy Cartesian Product “`swift [[1, 2], [3, 4]].lazyProduct()

    [1, 3], [1, 4], [2, 3], [2, 4] ”`

    Declaration

    Swift

    func lazyProduct() -> ProdSeq<Generator.Element>
  • Returns the first element in self that satisfies a predicate, or nil if it doesn’t exist

    Declaration

    Swift

    func first(@noescape predicate: Generator.Element throws -> Bool) rethrows -> Generator.Element?
  • Returns the number of elements in self that satisfy predicate

    Declaration

    Swift

    func count(@noescape predicate: Generator.Element throws -> Bool) rethrows -> Int
  • Returns an array with n elements of self hopped over. The sequence includes the first element of self. “`swift [1, 2, 3, 4, 5, 6, 7, 8].hop(2)

    [1, 3, 5, 7] ”`

    Declaration

    Swift

    func hop(n: Int) -> [Generator.Element]
  • Categorises elements of self into a dictionary, with the keys given by keyFunc “`swift struct Person : CustomStringConvertible { let name: String let age : Int init(_ name: String, _ age: Int) { self.name = name self.age = age } var description: String { return name } }

    let people = [ Person(Jo, 20), Person(An, 20), Person(Cthulu, 4000) ]

    people.categorise { p in p.age }

    //[20: [Jo, An], 4000: [Cthulu]] ”`

    Declaration

    Swift

    func categorise<U : Hashable>(@noescape keyFunc: Generator.Element throws -> U)
        rethrows -> [U:[Generator.Element]]
  • Returns a dictionary where the keys are the elements of self, and the values are their respective frequencies swift [0, 3, 0, 1, 1, 3, 2, 3, 1, 0].freqs() // [2: 1, 0: 3, 3: 3, 1: 3]

    Declaration

    Swift

    func freqs() -> [Generator.Element:Int]
  • Returns an array of the elements of self, in order, with duplicates removed swift [3, 1, 2, 3, 2, 1, 1, 2, 3, 3].uniques() // [3, 1, 2]

    Declaration

    Swift

    public func uniques() -> [Generator.Element]
  • Returns the element which occurs most frequently in self

    Declaration

    Swift

    public func mostFrequent() -> Generator.Element?