Sequence

interface Sequence<out T>(source)

Produces elements on demand through an iterator, without storing a collection of results.

Each traversal asks iterator for its own state. Repeatability and mutation behavior follow the source. Iterators, wrappers and captured callbacks use ordinary managed heap quotas.

Type Parameters

T

element type, including nullable types.

Functions

Link copied to clipboard
inline fun <T> Sequence<T>.all(predicate: (T) -> Boolean): Boolean

Stops at the first failure; returns true when empty without invoking predicate. Exceptions propagate; the immediate inline callback admits non-local returns.

Link copied to clipboard
fun <T> Sequence<T>.any(): Boolean

Checks availability without reading an element; returns false when empty.

inline fun <T> Sequence<T>.any(predicate: (T) -> Boolean): Boolean

Stops at the first match; returns false when empty without invoking predicate. Exceptions propagate; the immediate inline callback admits non-local returns.

Link copied to clipboard
fun <T> Sequence<T>.count(): Int

Consumes all elements and returns their count, including null elements.

inline fun <T> Sequence<T>.count(predicate: (T) -> Boolean): Int

Counts matches in iteration order, consuming the entire sequence. Exceptions propagate; the immediate inline callback admits non-local returns.

Link copied to clipboard
fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T>

Retains matching elements in source order, testing only as far as the consumer requests. hasNext may advance the source and invoke the retained predicate. Repeated hasNext calls reuse a cached element, including null. Each traversal has its own cache.

Link copied to clipboard
fun <T> Sequence<T>.filterNot(predicate: (T) -> Boolean): Sequence<T>

Lazily retains elements for which the retained predicate returns false, in source order.

Link copied to clipboard

Lazily skips null elements and exposes a non-null element type. Searches only as far as the consumer requests; repeated hasNext calls reuse the cache. Primitive accepted values use the existing scalar cache; reference slots clear on consumption.

Link copied to clipboard
fun <T> Sequence<T>.firstOrNull(): T?

Returns the first element, including null, or null when empty. Stops after that element.

inline fun <T> Sequence<T>.firstOrNull(predicate: (T) -> Boolean): T?

Returns the first matching element, including null, or null when none matches. Stops immediately at a match. The immediate inline predicate admits non-local returns.

Link copied to clipboard
inline fun <T, R> Sequence<T>.fold(initial: R, operation: (R, T) -> R): R

Accumulates elements in order, passing the accumulator before the current element. Returns initial for an empty sequence without invoking operation. Exceptions propagate; the immediate inline callback admits non-local returns.

Link copied to clipboard
inline fun <T> Sequence<T>.forEach(action: (T) -> Unit)

Visits every element in iteration order without constructing a result list. Exceptions propagate; the immediate inline callback admits non-local returns.

Link copied to clipboard
abstract operator fun iterator(): Iterator<T>

Returns an iterator for this traversal; creating it does not request an element.

Link copied to clipboard
fun <T, R> Sequence<T>.map(transform: (T) -> R): Sequence<R>

Transforms elements when the returned iterator's next is called. The callback is retained and may run again on subsequent traversals; exceptions propagate. No intermediate list is created. Unlike eager inline map, non-local returns are unavailable.

Link copied to clipboard
fun <T, R : Any> Sequence<T>.mapNotNull(transform: (T) -> R?): Sequence<R>

Transforms elements on demand and skips null results, preserving source order. Repeated hasNext calls reuse the accepted result without invoking transform again. The callback is retained; exceptions propagate and non-local returns are unavailable. Nullable primitive results use their ordinary boxes before non-null scalar caching.

Link copied to clipboard
fun <T> Sequence<T>.none(): Boolean

Checks availability without reading an element; returns true when empty.

inline fun <T> Sequence<T>.none(predicate: (T) -> Boolean): Boolean

Stops at the first match; returns true when empty without invoking predicate. Exceptions propagate; the immediate inline callback admits non-local returns.

Link copied to clipboard
fun <T> Sequence<T>.take(count: Int): Sequence<T>

Returns at most count elements. Zero does not even request an upstream iterator.

Link copied to clipboard
fun <T> Sequence<T>.toList(): List<T>

Consumes the sequence into a new list, preserving order and nullable elements. An unbounded source does not finish and remains subject to execution and heap quotas.