Cursor
An immutable, persistent, singly-linked view over a (possibly lazily produced) sequence of elements of type T, akin to a Lisp-style (head . tail) cons cell.
A Cursor differs from Kotlin's Iterator in two important ways: it is immutable (advancing to next yields a new Cursor, leaving the original one untouched and still usable), and it can be shared/reused from multiple call sites without any of them interfering with the others' traversal position. This makes it a good building block for lazily-constructed, structurally-shared data (e.g. Prolog lists, see it.unibo.tuprolog.core.List.from(Cursor, Term?) in the :core module, which wraps a Cursor of Terms without eagerly materializing them), where a plain Iterator would be unsuitable because it can only be consumed once and does not support branching.
Use Cursor.of (or the toCursor/cursor extension functions) to obtain a Cursor out of an Iterator, Iterable, Sequence, Array or Collection; use Cursor.empty to get the canonical empty cursor. A finished cursor (i.e. one for which isOver is true) has no current element and calling next on it throws.
var cursor: Cursor<out T> = listOf(1, 2, 3).cursor()
while (!cursor.isOver) {
println(cursor.current)
cursor = cursor.next
}Type Parameters
is the type of the elements traversed by this cursor
Functions
Returns an Iterable view of this cursor; each call to Iterable.iterator yields a fresh traversal starting again from current.
Returns a Sequence view of this cursor, equivalent to asIterable().asSequence().