Collection
protocol Collection : Sequence
-
Mechanica
Returns the element at the specified index or nil if not exists.
Example:
let array = [1, 2, 3] array[100] -> crash array[safe: 100] -> nilDeclaration
Swift
public subscript(safe index: Index) -> Element? { get }Parameters
indexthe index of the desired element.
-
Mechanica
Example:
let array = [1, 2, 3] array[100] -> crash array.at(100) -> nilDeclaration
Swift
public func at(_ index: Index) -> Element?Parameters
indexthe index of the desired element.
Return Value
The element at a given index or nil if not exists.
-
Mechanica
Returns all the indices of a specified item.
Example:
[1, 2, 1, 2, 3, 4, 5, 1].indices(of: 1) -> [0, 2, 7])Declaration
Swift
public func indices(of item: Element) -> [Index]Parameters
itemitem to verify.
Return Value
all the indices of the given item.
-
Mechanica
Scans
selfexactly for a given count of elements, removing all the scanned elements fromself.Example:
var phrase = "tin robots"[...] phrase.scan(count: 4) -> "tin " phrase -> robotsDeclaration
Swift
public mutating func scan(count: Int) -> Self?Parameters
countThe number of elements to be scanned.
Return Value
The scanned elements.
-
Mechanica
Scans
selfuntil a given condition is met, removing all the scanned elements fromselfand accumulating them into a given buffer.Example:
var phrase = "tin robots"[...] var buffer = String() phrase.scan { $0 == Character("o") } phrase -> "obots"Declaration
Swift
public mutating func scan(upToCondition condition: ((Element) -> Bool))Parameters
conditionThe condition to be met.
-
Mechanica
Scans
selfuntil a given condition is met, removing all the scanned elements fromselfand accumulating them into a given buffer.Example:
var phrase = "tin robots"[...] var buffer = String() phrase.scan(upToCondition: { $0 == Character("o")}, into: &buffer) phrase -> "obots" buffer -> "tin r"Declaration
Swift
public mutating func scan<C>(upToCondition condition: ((Element) -> Bool), into buffer: inout C) where C : RangeReplaceableCollection, Self.Element == C.ElementParameters
conditionThe condition to be met.
bufferThe buffer where all the scanned elements are accumulated.
-
Mechanica
Scans
selffor a specific index, removing all the scanned elements fromself.Example:
var phrase = "tin robots"[...] phrase.scan(prefix: "tin") -> true phrase -> " robots") var phrase2 = "tin robots"[...] phrase2.scan(prefix: "in") -> false phrase2 -> "tin robots"Declaration
Swift
public mutating func scan<C>(prefix: C) -> Bool where C : Collection, Self.Element == C.ElementParameters
prefixThe prefix to be scanned.
Return Value
True is the prefix is met.
-
Mechanica
Scans
selfuntil a given collection is encountered, removing all the scanned elements fromselfand accumulating them into a given buffer.Example:
var phrase = "tin robots"[...] var buffer = String() phrase.scan(upToCollection: "obot", into: &buffer) phrase -> "obots" buffer -> "tin r"Declaration
Swift
public mutating func scan<C>(upToCollection collection: Self, into buffer: inout C) where C : RangeReplaceableCollection, Self.Element == C.ElementParameters
collectionThe collection to be encountered.
bufferThe buffer where all the scanned elements are accumulated.
-
Mechanica
Scans
selfuntil a given collection is encountered, removing all the scanned elements fromself.Example:
var phrase = "tin robots"[...] var buffer = String() phrase.scan(upToCollection: "obot") phrase -> "obots"Declaration
Swift
public mutating func scan(upToCollection collection: Self)Parameters
collectionThe collection to be encountered.
Collection Extension Reference