Sequence

protocol Sequence
  • An alias to Sequence#forEach(body:).

    [1, 2, 3].each { elem in
        print(elem)     #=> 1, 2, 3
    }
    

    Declaration

    Swift

    func each(_ closure: (Self.Iterator.Element) throws -> Void) rethrows -> Self

    Parameters

    closure

    An closure will passed to forEach method

    Return Value

    Self

  • Iterate the receiver arrays with index which produced by a for...in and enumerated loop.

    [1, 2, 3].eachWithIndex { (index, elem) in
        // do something with index and elem
    }
    

    Declaration

    Swift

    func eachWithIndex(_ closure: (Int, Self.Iterator.Element) throws -> Void) rethrows -> Self

    Parameters

    closure

    A closure which accepts a index and an element

    Return Value

    Self

  • Iterate the receiver arrays with index which produced by a for...in and enumerated loop. Returns a new array which map from the receiver array’s values to a new one.

    let array = [1, 2, 3].mapWithIndex { (index, elem) in
        return index + elem
    }
    
    array       #=> [1, 3, 5]
    

    Declaration

    Swift

    func mapWithIndex<T>(_ closure: (Int, Iterator.Element) throws -> T) rethrows -> [T]

    Parameters

    closure

    A closure which accepts a index and an element

    Return Value

    An new array with mapped value

  • Invoke reversed first and call Sequence#each(closure:) methods on the receiver.

    [1, 2, 3].reverseEach { elem in
        print(elem)     #=> 3, 2, 1
    }
    

    Declaration

    Swift

    func reverseEach(_ closure: (Self.Iterator.Element) throws -> Void) rethrows -> Self

    Parameters

    closure

    A closure will eventually passed to forEach method

    Return Value

    Self

  • Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero, and x represents an element of the sequence.

    This example enumerates the characters of the string Swift and prints each character along with its place in the string.

    for (n, c) in "Swift".characters.withIndex {
        print("\(n): '\(c)'")
    }
    // Prints "0: 'S'"
    // Prints "1: 'w'"
    // Prints "2: 'i'"
    // Prints "3: 'f'"
    // Prints "4: 't'"
    

    When enumerating a collection, the integer part of each pair is a counter for the enumeration, not necessarily the index of the paired value. These counters can only be used as indices in instances of zero-based, integer-indexed collections, such as Array and ContiguousArray. For other collections the counters may be out of range or of the wrong type to use as an index. To iterate over the elements of a collection with its indices, use the zip(_:_:) function.

    This example iterates over the indices and elements of a set, building a list of indices of names with five or fewer letters.

    let names: Set = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]
    var shorterIndices: [SetIndex<String>] = []
    for (i, name) in zip(names.indices, names) {
        if name.characters.count <= 5 {
            shorterIndices.append(i)
        }
    }
    

    Now that the shorterIndices array holds the indices of the shorter names in the names set, you can use those indices to access elements in the set.

    for i in shorterIndices {
        print(names[i])
    }
    // Prints "Sofia"
    // Prints "Mateo"
    

    Declaration

    Swift

    var withIndex: EnumeratedSequence<Self>

    Return Value

    A sequence of pairs enumerating the sequence.

  • Returns the index of the first object in ary such that the object is == to obj.

    [1, 2, 3].index(1)              #=> 0
    [1, 2, 3].index(2)              #=> 1
    [1, 2, 3, 3, 4].index(3)        #=> 2
    

    Declaration

    Swift

    func index(_ element: Self.Iterator.Element) -> Int?

    Parameters

    element

    An element

    Return Value

    A index indicates the first element position in the array of nil

  • Returns the index of the last object in self == to obj.

    [1, 2, 3].rindex(1)              #=> 0
    [1, 2, 3].rindex(2)              #=> 1
    [1, 2, 3, 3, 4].rindex(3)        #=> 3
    

    Declaration

    Swift

    func rindex(_ element: Self.Iterator.Element) -> Int?

    Parameters

    element

    An element

    Return Value

    A index indicates the last element position in the array of nil

  • Returns true if self contains no elements.

    [].isEmpty      #=> true
    [1].isEmpty     #=> false
    

    Declaration

    Swift

    var isEmpty: Bool
  • Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false.

    [1, 2, 3].isAny { $0 == 1 }             #=> true
    ["a", "b", "c"].isAny { $0 == "b" }     #=> true
    [1, 2, 3].isAny { $0 == 100 }           #=> false
    ["a", "b", "c"].isAny { $0 == "d" }     #=> false
    

    Declaration

    Swift

    func isAny(_ closure: (Self.Iterator.Element) throws -> Bool) rethrows -> Bool

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    A bool value indicates there is an element cause the block to return true

  • Passes each element of the collection to the given block. The method returns true if the block never returns false.

    [1, 2, 3].isAll { $0.isPositive }       #=> true
    ["a", "a", "a"].isAll { $0 == "a" }     #=> true
    [1, 2, 3].isAll { $0 == 100 })          #=> false
    ["a", "b", "c"].isAll { $0 == "bbb" }   #=> false
    

    Declaration

    Swift

    func isAll(_ closure: (Self.Iterator.Element) throws -> Bool) rethrows -> Bool

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    A bool value indicates all the elements in array cause the block to return true

  • Returns a new array containing all elements of self for which the given block returns a true value.

    [1, 2, 3].select { $0 > 2 }         #=> [3]
    [1, 2, 3].select { $0 <= 2 }        #=> [1, 2]
    [1, 2, 3].select { _ in false }     #=> []
    

    Declaration

    Swift

    func select(_ closure: (Self.Iterator.Element) throws -> Bool) rethrows -> [Self.Iterator.Element]

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    A new array

  • An alias to select(closure:) method.

    [1, 2, 3].keepIf { $0 > 2 }         #=> [3]
    [1, 2, 3].keepIf { $0 <= 2 }        #=> [1, 2]
    [1, 2, 3].keepIf { _ in false }     #=> []
    

    Declaration

    Swift

    func keepIf(_ closure: (Self.Iterator.Element) throws -> Bool) rethrows -> [Self.Iterator.Element]

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    A new array

  • Returns a new array excluding all elements of self for which the given block returns a true value.

    [1, 2, 3].reject { $0 > 2 }         #=> [1, 2]
    [1, 2, 3].reject { $0 <= 2 }        #=> [3]
    [1, 2, 3].reject { _ in false }     #=> [1, 2, 3]
    

    Declaration

    Swift

    func reject(_ closure: (Self.Iterator.Element) throws -> Bool) rethrows -> [Self.Iterator.Element]

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    A new array

  • An alias to reject, see also Sequence#reject(closure:)

    [1, 2, 3].deleteIf { $0 > 2 }         #=> [1, 2]
    [1, 2, 3].deleteIf { $0 <= 2 }        #=> [3]
    [1, 2, 3].deleteIf { _ in false }     #=> [1, 2, 3]
    

    Declaration

    Swift

    func deleteIf(_ closure: (Self.Iterator.Element) throws -> Bool) rethrows -> [Self.Iterator.Element]

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    A new array

  • Drops elements up to, but not including, the first element for which the block returns nil or false and returns an array containing the remaining elements.

    [1, 2, 3].dropWhile { $0 > 2 }         #=> [1, 2, 3]
    [1, 2, 3].dropWhile { $0 <= 2 }        #=> [3]
    [1, 2, 3].dropWhile { _ in false }     #=> [1, 2, 3]
    

    Declaration

    Swift

    func dropWhile(_ closure: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.SubSequence

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    A new array

  • Passes elements to the block until the block returns nil or false, then stops iterating and returns an array of all prior elements.

    [1, 2, 3].takeWhile { $0 > 2 }         #=> []
    [1, 2, 3].takeWhile { $0 <= 2 }        #=> [1, 2]
    [1, 2, 3].takeWhile { _ in false }     #=> []
    

    Declaration

    Swift

    func takeWhile(_ closure: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.SubSequence

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    A new array

  • Invokes the given block once for each element of self. Creates a new array containing the values returned by the block.

    [1, 2, 3].collect { _ in "2" }              #=> ["2", "2", "2"]
    [1, 2, 3].collect { $0 * 2 }                #=> [2, 4, 6]
    ["1", "2", "3"].collect { $0 + "!" }        #=> ["1!", "2!", "3!"]
    

    Declaration

    Swift

    func collect<T>(closure: (Self.Iterator.Element) throws -> T) rethrows -> [T]

    Parameters

    closure

    A block accepts element in the receiver and returns a value

    Return Value

    A new array

  • Returns a new array with the elements of both arrays within it.

    let a = [1, 2, 3]
    a.concat([4, 5, 6])     #=> [1, 2, 3, 4, 5, 6]
    a                       #=> [1, 2, 3]
    a.concat(7, 8, 9)       #=> [1, 2, 3, 7, 8, 9]
    a                       #=> [1, 2, 3]
    

    Declaration

    Swift

    func concat(_ other: [Self.Iterator.Element]) -> [Self.Iterator.Element]

    Parameters

    other

    Another array

    Return Value

    A new array contains all the element in both array

  • Returns a new array with the elements of both arrays within it.

    let a = [1, 2, 3]
    a.concat([4, 5, 6])     #=> [1, 2, 3, 4, 5, 6]
    a                       #=> [1, 2, 3]
    a.concat(7, 8, 9)       #=> [1, 2, 3, 7, 8, 9]
    a                       #=> [1, 2, 3]
    

    Declaration

    Swift

    func concat(_ others: Self.Iterator.Element...) -> [Self.Iterator.Element]

    Parameters

    other

    Another array

    Return Value

    A new array contains all the element in both array

  • Counts the number of elements for which the block returns a true value.

    let a = [1, 2, 3, 10, 100, 1000]
    a.count { $0 > 10 }             #=> 2
    a.count { $0.isPositive }       #=> 6
    expect(a.count { $0.isEven }    #=> 4
    

    Declaration

    Swift

    func count(_ closure: (Self.Iterator.Element) throws -> Bool) rethrows -> Int

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    An integer of the count of element make the block returns true

  • Returns a new array that is a one-dimensional flattening of self (recursively)

    [1, 2, [3, 4, 5, [6, 7, [8]]]].flatten()        #=> [1, 2, 3, 4, 5, 6, 7, 8]
    

    Declaration

    Swift

    func flatten<T>() -> [T]

    Return Value

    A new array that is a one-dimensional flattening of self

  • Returns the first n elements of an array

    let arr = [1, 2, 3, 4]
    arr.take(1)         #=> [1]
    arr.take(2)         #=> [1, 2]
    arr.take(100)       #=> [1, 2, 3, 4]
    

    Returns an empty array if num is not positive

    arr.take(0)         #=> []
    arr.take(-1)        #=> []
    

    Declaration

    Swift

    func take(_ num: Int) -> [Self.Iterator.Element]

    Parameters

    num

    An integer specifies the element of the returning array

    Return Value

    An new array of first n elements

  • drop does the opposite of take, by returning the elements after n elements have been dropped.

    let arr = [1, 2, 3, 4]
    arr.drop(1)         #=> [2, 3, 4]
    arr.drop(2)         #=> [3, 4]
    arr.drop(100)       #=> []
    

    Returns an new array with the same element if num is not positive.

    arr.take(0)         #=> [1, 2, 3, 4]
    arr.take(-1)        #=> [1, 2, 3, 4]
    

    Declaration

    Swift

    func drop(_ num: Int) -> [Self.Iterator.Element]

    Parameters

    num

    How many element should be dropped from the beginning

    Return Value

    An new array with first n elements dropped

  • Return the first n elements of an array. An alias to Sequence#take(num:) method.

    let arr = [1, 2, 3, 4]
    arr.first(1)        #=> [1]
    arr.first(2)        #=> [1, 2]
    arr.first(100)      #=> [1, 2, 3, 4]
    arr.first(0)        #=> []
    arr.first(-1)       #=> []
    

    See

    See Also: Sequence#take(num:)

    Declaration

    Swift

    func first(_ num: Int) -> [Self.Iterator.Element]

    Parameters

    num

    An integer specifies the element of the returning array

    Return Value

    An new array of first n elements

  • Return the last n elements of an array.

    let arr = [1, 2, 3, 4]
    arr.last(1)        #=> [4]
    arr.last(2)        #=> [3, 4]
    arr.last(100)      #=> [1, 2, 3, 4]
    arr.last(0)        #=> []
    arr.last(-1)       #=> []
    

    Declaration

    Swift

    func last(_ num: Int) -> [Self.Iterator.Element]

    Parameters

    num

    An integer specifies the element of the returning array

    Return Value

    An new array of last n elements

  • Calls the given block for each element n times.

    let arr = [1, 2, 3, 4]
    arr.cycle(2)        #=> [1, 2, 3, 4, 1, 2, 3, 4]
    
    arr.cycle(10) { elem in
        print(elem)     #=> 1, 2, 3, 4, 1, 2, 3, 4...
    }
    

    Declaration

    Swift

    func cycle(_ n: Int = 1, closure: ((Self.Iterator.Element) throws -> Void)? = nil) rethrows -> [Self.Iterator.Element]

    Parameters

    n

    An integer that indicates how many times the element in array should be called

    closure

    A closure that accepts the element in array as parameter

    Return Value

    An new array with every elements in array repeated for n times

  • Returns an array with all elements in sequence.

    Declaration

    Swift

    var to_a: [Self.Iterator.Element]