Cursor
A type that supplies the values of some external resource, one at a time.
protocol Cursor<Element> : AnyObject
Browse conforming typesOverview
To iterate over the elements of a cursor, use a while
loop:
while let element = try cursor.next() {
print(element)
}
Relationship with standard Sequence and IteratorProtocol
Cursors share traits with lazy sequences and iterators from the Swift standard library. Differences are:
Cursor types are classes, and have a lifetime.
Cursor iteration may throw errors.
A cursor can not be repeated.
The protocol comes with default implementations for many operations similar to those defined by Swift’s Sequence protocol: contains
, dropFirst
, dropLast
, drop(while:)
, enumerated
, filter
, first
, flatMap
, forEach
, joined
, joined(separator:)
, max
, max(by:)
, min
, min(by:)
, map
, prefix
, prefix(while:)
, reduce
, reduce(into:)
, suffix
.
Supporting Types
class AnyCursor<Element>
A type-erased cursor.
class DropFirstCursor<Base>
A
Cursor
that consumes and drops n elements from an underlyingBase
cursor before possibly returning the first available element.class DropWhileCursor<Base>
A
Cursor
whose elements consist of the elements that follow the initial consecutive elements of someBase
cursor that satisfy a given predicate.class EnumeratedCursor<Base>
An enumeration of the elements of a cursor.
class FilterCursor<Base>
A
Cursor
whose elements consist of the elements of someBase
cursor that also satisfy a given predicate.class FlattenCursor<Base>
A
Cursor
consisting of all the elements contained in each segment contained in someBase
cursor.class MapCursor<Base, Element>
A
Cursor
whose elements consist of those in aBase
cursor passed through a transform function returning Element.class PrefixCursor<Base>
A
Cursor
that only consumes up ton
elements from an underlyingBase
cursor.class PrefixWhileCursor<Base>
A
Cursor
whose elements consist of the initial consecutive elements of someBase
cursor that satisfy a given predicate.