Collection

protocol Collection : Sequence where Self.SubSequence : Collection
  • SwifterSwift: Performs each closure for each element of collection in parallel.

    array.forEachInParallel { item in
        print(item)
    }
    

    Declaration

    Swift

    public func forEachInParallel(_ each: (Self.Element) -> Void)

    Parameters

    each

    closure to run for each element.

  • SwifterSwift: Safe protects the array from out of bounds by use of optional.

    let arr = [1, 2, 3, 4, 5]
    arr[safe: 1] -> 2
    arr[safe: 10] -> nil
    

    Declaration

    Swift

    public subscript(safe index: Index) -> Element? { get }

    Parameters

    index

    index of element to access element.

  • SwifterSwift: Random item from array.

    Declaration

    Swift

    public var randomItem: Element? { get }
  • SwifterSwift: Get the first index where condition is met.

       [1, 7, 1, 2, 4, 1, 6].firstIndex { $0 % 2 == 0 } -> 3
    

    Declaration

    Swift

    public func firstIndex(where condition: (Element) throws -> Bool) rethrows -> Index?

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    first index where the specified condition evaluates to true. (optional)

  • SwifterSwift: Get the last index where condition is met.

    [1, 7, 1, 2, 4, 1, 8].lastIndex { $0 % 2 == 0 } -> 6
    

    Declaration

    Swift

    public func lastIndex(where condition: (Element) throws -> Bool) rethrows -> Index?

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    last index where the specified condition evaluates to true. (optional)

  • SwifterSwift: Get all indices where condition is met.

    [1, 7, 1, 2, 4, 1, 8].indices(where: { $0 == 1 }) -> [0, 2, 5]
    

    Declaration

    Swift

    public func indices(where condition: (Element) throws -> Bool) rethrows -> [Index]?

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    all indices where the specified condition evaluates to true. (optional)

  • SwifterSwift: Calls the given closure with an array of size of the parameter slice.

    [0, 2, 4, 7].forEach(slice: 2) { print($0) } -> //print: [0, 2], [4, 7]
    [0, 2, 4, 7, 6].forEach(slice: 2) { print($0) } -> //print: [0, 2], [4, 7], [6]
    

    Declaration

    Swift

    public func forEach(slice: Int, body: ([Element]) throws -> Void) rethrows

    Parameters

    slice

    size of array in each interation.

    body

    a closure that takes an array of slice size as a parameter.

  • SwifterSwift: Returns an array of slices of length size from the array. If array can’t be split evenly, the final slice will be the remaining elements.

    [0, 2, 4, 7].group(by: 2) -> [[0, 2], [4, 7]]
    [0, 2, 4, 7, 6].group(by: 2) -> [[0, 2], [4, 7], [6]]
    

    Declaration

    Swift

    public func group(by size: Int) -> [[Element]]?

    Parameters

    size

    The size of the slices to be returned.

    Return Value

    grouped self.

  • SwifterSwift: First index of a given item in an array.

       [1, 2, 2, 3, 4, 2, 5].firstIndex(of: 2) -> 1
       [1.2, 2.3, 4.5, 3.4, 4.5].firstIndex(of: 6.5) -> nil
       ["h", "e", "l", "l", "o"].firstIndex(of: "l") -> 2
    

    Declaration

    Swift

    public func firstIndex(of item: Element) -> Index?

    Parameters

    item

    item to check.

    Return Value

    first index of item in array (if exists).

  • SwifterSwift: Last index of element in array.

       [1, 2, 2, 3, 4, 2, 5].lastIndex(of: 2) -> 5
       [1.2, 2.3, 4.5, 3.4, 4.5].lastIndex(of: 6.5) -> nil
       ["h", "e", "l", "l", "o"].lastIndex(of: "l") -> 3
    

    Declaration

    Swift

    public func lastIndex(of item: Element) -> Index?

    Parameters

    item

    item to check.

    Return Value

    last index of item in array (if exists).

  • SwifterSwift: Average of all elements in array.

    Declaration

    Swift

    public func average() -> Double

    Return Value

    the average of the array’s elements.

  • SwifterSwift: Average of all elements in array.

       [1.2, 2.3, 4.5, 3.4, 4.5].average() = 3.18
    

    Declaration

    Swift

    public func average() -> Element

    Return Value

    average of the array’s elements.