Sequence

protocol Sequence
  • SwifterSwift: Check if all elements in collection match a conditon.

       [2, 2, 4].all(matching: {$0 % 2 == 0}) -> true
       [1,2, 2, 4].all(matching: {$0 % 2 == 0}) -> false
    

    Declaration

    Swift

    func all(matching condition: (Element) throws -> Bool) rethrows -> Bool

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    true when all elements in the array match the specified condition.

  • SwifterSwift: Check if no elements in collection match a conditon.

       [2, 2, 4].none(matching: {$0 % 2 == 0}) -> false
       [1, 3, 5, 7].none(matching: {$0 % 2 == 0}) -> true
    

    Declaration

    Swift

    func none(matching condition: (Element) throws -> Bool) rethrows -> Bool

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    true when no elements in the array match the specified condition.

  • SwifterSwift: Check if any element in collection match a conditon.

       [2, 2, 4].any(matching: {$0 % 2 == 0}) -> false
       [1, 3, 5, 7].any(matching: {$0 % 2 == 0}) -> true
    

    Declaration

    Swift

    func any(matching condition: (Element) throws -> Bool) rethrows -> Bool

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    true when no elements in the array match the specified condition.

  • SwifterSwift: Get last element that satisfies a conditon.

       [2, 2, 4, 7].last(where: {$0 % 2 == 0}) -> 4
    

    Declaration

    Swift

    func last(where condition: (Element) throws -> Bool) rethrows -> Element?

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    the last element in the array matching the specified condition. (optional)

  • SwifterSwift: Filter elements based on a rejection condition.

       [2, 2, 4, 7].reject(where: {$0 % 2 == 0}) -> [7]
    

    Declaration

    Swift

    func reject(where condition: (Element) throws -> Bool) rethrows -> [Element]

    Parameters

    condition

    to evaluate the exclusion of an element from the array.

    Return Value

    the array with rejected values filtered from it.

  • SwifterSwift: Get element count based on condition.

       [2, 2, 4, 7].count(where: {$0 % 2 == 0}) -> 3
    

    Declaration

    Swift

    func count(where condition: (Element) throws -> Bool) rethrows -> Int

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    number of times the condition evaluated to true.

  • SwifterSwift: Iterate over a collection in reverse order. (right to left)

       [0, 2, 4, 7].forEachReversed({ print($0)}) -> //Order of print: 7,4,2,0
    

    Declaration

    Swift

    func forEachReversed(_ body: (Element) throws -> Void) rethrows

    Parameters

    body

    a closure that takes an element of the array as a parameter.

  • SwifterSwift: Calls the given closure with each element where condition is true.

       [0, 2, 4, 7].forEach(where: {$0 % 2 == 0}, body: { print($0)}) -> //print: 0, 2, 4
    

    Declaration

    Swift

    func forEach(where condition: (Element) throws -> Bool, body: (Element) throws -> Void) rethrows

    Parameters

    condition

    condition to evaluate each element against.

    body

    a closure that takes an element of the array as a parameter.

  • SwifterSwift: Reduces an array while returning each interim combination.

    [1, 2, 3].accumulate(initial: 0, next: +) -> [1, 3, 6]
    

    Declaration

    Swift

    func accumulate<U>(initial: U, next: (U, Element) throws -> U) rethrows -> [U]

    Parameters

    initial

    initial value.

    next

    closure that combines the accumulating value and next element of the array.

    Return Value

    an array of the final accumulated value and each interim combination.

  • SwifterSwift: Filtered and map in a single operation.

    [1,2,3,4,5].filtered({ $0 % 2 == 0 }, map: { $0.string }) -> ["2", "4"]
    

    Declaration

    Swift

    func filtered<T>(_ isIncluded: (Element) throws -> Bool, map transform: (Element) throws -> T) rethrows -> [T]

    Parameters

    isIncluded

    condition of inclusion to evaluate each element against.

    transform

    transform element function to evaluate every element.

    Return Value

    Return an filtered and mapped array.

  • SwifterSwift: Get the only element based on a condition.

    [].single(where: {_ in true}) -> nil
    [4].single(where: {_ in true}) -> 4
    [1, 4, 7].single(where: {$0 % 2 == 0}) -> 4
    [2, 2, 4, 7].single(where: {$0 % 2 == 0}) -> nil
    

    Declaration

    Swift

    func single(where condition: ((Element) throws -> Bool)) rethrows -> Element?

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    The only element in the array matching the specified condition. If there are more matching elements, nil is returned. (optional)

  • SwifterSwift: Check if array contains an array of elements.

       [1, 2, 3, 4, 5].contains([1, 2]) -> true
       [1.2, 2.3, 4.5, 3.4, 4.5].contains([2, 6]) -> false
       ["h", "e", "l", "l", "o"].contains(["l", "o"]) -> true
    

    Declaration

    Swift

    func contains(_ elements: [Element]) -> Bool

    Parameters

    elements

    array of elements to check.

    Return Value

    true if array contains all given items.

  • SwifterSwift: Check whether a sequence contains duplicates.

    Declaration

    Swift

    func containsDuplicates() -> Bool

    Return Value

    true if the receiver contains duplicates.

  • SwifterSwift: Getting the duplicated elements in a sequence.

    [1, 1, 2, 2, 3, 3, 3, 4, 5].duplicates().sorted() -> [1, 2, 3])
    ["h", "e", "l", "l", "o"].duplicates().sorted() -> ["l"])
    

    Declaration

    Swift

    func duplicates() -> [Element]

    Return Value

    An array of duplicated elements.

  • SwifterSwift: Sum of all elements in array.

       [1, 2, 3, 4, 5].sum() -> 15
    

    Declaration

    Swift

    func sum() -> Element

    Return Value

    sum of the array’s elements.