Sequence
public extension Sequence
public extension Sequence where Element: Equatable
public extension Sequence where Element: Hashable
public extension Sequence where Element: Numeric
-
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: Remove duplicate elements based on condition.
[1, 2, 1, 3, 2].withoutDuplicates { $0 } -> [1, 2, 3] [(1, 4), (2, 2), (1, 3), (3, 2), (2, 1)].withoutDuplicates { $0.0 } -> [(1, 4), (2, 2), (3, 2)]
Complexity
O(n), where n is the length of the sequence.Declaration
Swift
func withoutDuplicates<T>(transform: (Element) throws -> T) rethrows -> [Element] where T : Hashable
Parameters
transform
A closure that should return the value to be evaluated for repeating elements.
Return Value
Sequence without repeating elements
-
SwifterSwift: Separates all items into 2 lists based on a given predicate. The first list contains all items for which the specified condition evaluates to true. The second list contains those that don’t.
let (even, odd) = [0, 1, 2, 3, 4, 5].divided { $0 % 2 == 0 } let (minors, adults) = people.divided { $0.age < 18 }
Declaration
Swift
func divided(by condition: (Element) throws -> Bool) rethrows -> (matching: [Element], nonMatching: [Element])
Parameters
condition
condition to evaluate each element against.
Return Value
A tuple of matched and non-matched items
-
SwifterSwift: Return a sorted array based on a key path and a compare function.
Declaration
Swift
func sorted<T>(by keyPath: KeyPath<Element, T>, with compare: (T, T) -> Bool) -> [Element]
Parameters
keyPath
Key path to sort by.
compare
Comparation function that will determine the ordering.
Return Value
The sorted array.
-
SwifterSwift: Return a sorted array based on a key path.
Declaration
Swift
func sorted<T>(by keyPath: KeyPath<Element, T>) -> [Element] where T : Comparable
Parameters
keyPath
Key path to sort by. The key path type must be Comparable.
Return Value
The sorted array.
-
SwifterSwift: Returns a sorted sequence based on two key paths. The second one will be used in case the values of the first one match.
Declaration
Swift
func sorted<T: Comparable, U: Comparable>(by keyPath1: KeyPath<Element, T>, and keyPath2: KeyPath<Element, U>) -> [Element]
Parameters
keyPath1
Key path to sort by. Must be Comparable.
keyPath2
Key path to sort by in case the values of
keyPath1
match. Must be Comparable. -
SwifterSwift: Returns a sorted sequence based on three key paths. Whenever the values of one key path match, the next one will be used.
Declaration
Swift
func sorted<T: Comparable, U: Comparable, V: Comparable>(by keyPath1: KeyPath<Element, T>, and keyPath2: KeyPath<Element, U>, and keyPath3: KeyPath<Element, V>) -> [Element]
Parameters
keyPath1
Key path to sort by. Must be Comparable.
keyPath2
Key path to sort by in case the values of
keyPath1
match. Must be Comparable.keyPath3
Key path to sort by in case the values of
keyPath1
andkeyPath2
match. Must be Comparable. -
SwifterSwift: Sum of a
AdditiveArithmetic
property of eachElement
in aSequence
.["James", "Wade", "Bryant"].sum(for: \.count) -> 15
Declaration
Swift
func sum<T>(for keyPath: KeyPath<Element, T>) -> T where T : AdditiveArithmetic
Parameters
keyPath
Key path of the
AdditiveArithmetic
property.Return Value
The sum of the
AdditiveArithmetic
propertys atkeyPath
. -
SwifterSwift: Returns an array containing the results of mapping the given key path over the sequence’s elements.
Declaration
Swift
func map<T>(by keyPath: KeyPath<Element, T>) -> [T]
Parameters
keyPath
Key path to map.
Return Value
An array containing the results of mapping.
-
SwifterSwift: Returns an array containing the non-nil results of mapping the given key path over the sequence’s elements.
Declaration
Swift
func compactMap<T>(by keyPath: KeyPath<Element, T?>) -> [T]
Parameters
keyPath
Key path to map.
Return Value
An array containing the non-nil results of mapping.
-
SwifterSwift: Returns an array containing the results of filtering the sequence’s elements by a boolean key path.
Declaration
Swift
func filter(by keyPath: KeyPath<Element, Bool>) -> [Element]
Parameters
keyPath
Boolean key path. If it’s value is
true
the element will be added to result.Return Value
An array containing filtered elements.
-
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.