Sequence
protocol Sequence
A Sequence can be either finite or infinite.
-
Mechanica
Checks if all the elements in collection satisfiy the given predicate.
Example:
[2, 2, 4, 4].all(matching: {$0 % 2 == 0}) -> true [1, 2, 2, 4].all(matching: {$0 % 2 == 0}) -> false
Attention
the sequence must be finite.Declaration
Swift
public func all(matching predicate: (Element) throws -> Bool) rethrows -> Bool
Parameters
predicate
condition to evaluate each element against.
Return Value
true when all elements in the array match the specified condition.
-
Mechanica
Checks if no elements in collection satisfiy the given predicate.
Example:
[2, 2, 4].none(matching: {$0 % 2 == 0}) -> false [1, 3, 5].none(matching: {$0 % 2 == 0}) -> true
Attention
the sequence must be finite.Declaration
Swift
public func none(matching predicate: (Element) throws -> Bool) rethrows -> Bool
Parameters
predicate
condition to evaluate each element against.
Return Value
true when no elements satisfy the specified condition.
-
Mechanica
Checks if any elements in collection satisfiy the given predicate.
Example:
[2, 2, 4].any(matching: {$0 % 2 == 0}) -> false [1, 3, 5, 7].any(matching: {$0 % 2 == 0}) -> true
Attention
the sequence must be finite.Declaration
Swift
public func any(matching predicate: (Element) throws -> Bool) rethrows -> Bool
Parameters
predicate
condition to evaluate each element against.
Return Value
true when no elements satisfy the specified condition.
-
Mechanica
Returns true if there is at least one element satisfying the given predicate.
Attention
the sequence must be finite.Declaration
Swift
public func hasAny(where predicate: (Element) -> Bool) -> Bool
Parameters
predicate
A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element is a match.
-
Mechanica
Returns true if all the elements satisfy the predicate.
Attention
the sequence must be finite.Declaration
Swift
public func hasAll(where predicate: (Element) -> Bool) -> Bool
Parameters
predicate
A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element is a match.
-
Mechanica
Attention
the sequence must be finite.Declaration
Swift
public func grouped<Key>(by criteria: (Element) -> (Key)) -> [Key : [Element]] where Key : Hashable
Parameters
criteria
The criteria closure takes an
Element
and returns its classification.Return Value
A grouped dictionary with the keys that the criteria function returns.
-
Mechanica
Returns the count of the elements satisfying the given predicate.
[2, 2, 4, 7].count(where: {$0 % 2 == 0}) -> 3
Attention
the sequence must be finite.Declaration
Swift
public func count(where predicate: (Element) throws -> Bool) rethrows -> Int
Parameters
predicate
predicate to evaluate each element against.
Return Value
number of times the condition evaluated to true.
-
Mechanica
Splits
self
into partitions of a givenlength
.Example:
let string = "Hello" string.split(by: 2) -> ["He", "ll", "o"]
Attention
the sequence must be finite.Declaration
Swift
public func split(by length: Int) -> [[Element]]
-
Mechanica
Returns true if the
Sequence
contains all the given elements.Example:
[1, 2, 3, 4, 5].contains([1, 2]) -> true ["h", "e", "l", "l", "o"].contains(["l", "o"]) -> true
Attention
the sequence must be finite.Declaration
Swift
public func contains(_ elements: [Element]) -> Bool
-
Mechanica
Returns a collection of tuples where it’s indicated the frequencies of the elements in the sequence.
Attention
the sequence must be finite.Declaration
Swift
public var frequencies: [(Element, Int)] { get }
-
Mechanica
Attention
the sequence must be finite.Declaration
Swift
public func hasDuplicates() -> Bool
Return Value
Returns true if the
Sequence
contains duplicates
-
Mechanica
Returns true if the
Sequence
contains an element identical (referential equality) to anobject
.Attention
the sequence must be finite.Declaration
Swift
public func containsObjectIdentical(to object: AnyObject) -> Bool
-
Mechanica
Sums of all elements in array.
Example:
[1, 2, 3, 4].sum() -> 10
Attention
the sequence must be finite.Declaration
Swift
public func sum() -> Element