Collection

public extension Collection
public extension Collection where Iterator.Element: Equatable
  • chunks(of: n) splits a list into length-n pieces. The last piece will be shorter if n does not evenly divide the length of the list. If n <= 0, `chunksOf(n) returns an infinite list of empty lists. For example:

    [-7, 5, 9].chunks(of: -1) = []
    

    Note that [].chunksOf(n) is [], not [[]]. This is intentional, and is consistent with a recursive definition of chunksOf; it satisfies the property that

    xs.chunks(of: n) + ys.chunks(of: n) == (xs + ys).chunks(of: n)
    

    whenever n evenly divides the length of xs.

    Declaration

    Swift

    func chunks(of size: Int) -> [SubSequence]

    Parameters

    size

    Size of chunk.

    Return Value

    Array of length-size pieces.

  • split(places: []) split a list into chunks of the given lengths. If the input list is longer than the total of the given lengths, then the remaining elements are dropped. If the list is shorter than the total of the given lengths, then the result may contain fewer chunks than requested, and the last chunk may be shorter than requested.

    If chunk <= 0, split(places: []) returns empty list. For example:

    [-7, 5, 9].split(places: [-1]) = [[]]
    

    Declaration

    Swift

    func split(places: [Int]) -> [SubSequence]

    Parameters

    places

    List of chunks.

    Return Value

    Array of chunks-size pieces.

Available where Iterator.Element: Equatable

  • Returns the longest possible subsequences of the collection, in order, that don’t contain element.

    Declaration

    Swift

    func split(on element: Iterator.Element) -> [SubSequence]

    Parameters

    element

    Splitter.

    Return Value

    An array of subsequences, split from this collection’s elements.

  • Split on any of the given elements.

    Declaration

    Swift

    func split<C: Collection>(oneOf elements: C) -> [SubSequence]
        where
        C.Iterator.Element == Iterator.Element

    Parameters

    elements

    Splitters.

    Return Value

    An array of subsequences, split from this collection’s elements.