Operators

Every operator takes a stream and returns a new stream. Chain them freely.

steady

Not an operator on an existing stream — a factory for a Stream<T> that always holds the same value. It never changes and never notifies beyond the initial delivery.

import io.peekandpoke.ultra.streams.steady

val pi = steady(3.14)

println(pi())  // 3.14

// Subscribers are called once with the value and never again
pi.subscribeToStream { println(it) }  // prints "3.14"
Handy when an API wants a Stream<T> but you only have a plain value — no need to spin up a StreamSource just to never update it.

map

Transform each value:

val source = StreamSource(5)
val doubled = source.map { it * 2 }

println(doubled())  // 10

source(10)
println(doubled())  // 20

To follow a stream that lives inside the value, see switchMap.

onEach

Execute a side effect for each value without changing it. Useful for logging:

val source = StreamSource(1)

val stream = source
    .onEach { println("Saw: $it") }
    .map { it * 10 }

indexed

Pairs each value with its index (starting at 0):

val source = StreamSource("a")
val indexed = source.indexed()

// Pair(0, "a"), then Pair(1, "b"), Pair(2, "c")...

filter

Only pass values matching a predicate. Since streams always have a value, you must provide an initial value for when nothing matches yet:

This is the "always has a value" contract showing its teeth. You pay with one extra parameter. Honestly, it's worth it.

A filter sees values only while someone listens: read without a subscriber, it keeps what it had.

val source = StreamSource(3)

val big = source.filter(initial = 0) { it > 10 }
big.subscribeToStream { }  // listening, so the filter sees the source's values

println(big())  // 0

source(20)
println(big())  // 20

source(5)
println(big())  // 20 (keeps last matching value)

Nullable filter

When you don't want to provide an initial value, use the nullable variant. It returns null until a value matches:

val source = StreamSource(3)
val big = source.filter { it > 10 }  // Stream<Int?>
big.subscribeToStream { }

println(big())  // null (nothing matched yet)

source(20)
println(big())  // 20

filterNotNull

Remove nulls from a nullable stream. The result is a Stream<String>: until a non-null value arrives it holds the initial value:

val source = StreamSource<String?>(null)
val safe = source.filterNotNull(initial = "")  // Stream<String>
safe.subscribeToStream { }

println(safe())  // "" (no non-null value yet)

source("hello")
println(safe())  // "hello"

source(null)
println(safe())  // "hello" (keeps last non-null)

filterIsInstance and filterIsInstanceOf

Filter by type. filterIsInstance takes the type and the stream's own type as type arguments (<String, _>); filterIsInstanceOf takes a KClass and needs none. Both reject a type that is not a subtype of the stream's. Never pass a class to filterIsInstance: on a Stream<Any?>, filterIsInstance(String::class) compiles as the initial overload, a Stream<KClass<String>> that never lets a string through.

val source = StreamSource<Any?>("text")
val strings = source.filterIsInstanceOf(String::class)  // Stream<String?>
// the same: source.filterIsInstance<String, _>()

strings.subscribeToStream { println(it) }  // "text"

source(42)          // not a String: nothing published, strings() stays "text"
source("new text")  // "new text"

// With an initial value, published until the first match: Stream<String>
val labels = source.filterIsInstanceOf(String::class, initial = "")

distinct

Only publish when the value actually changes:

val source = StreamSource(1)
val distinct = source.distinct()

val received = mutableListOf<Int>()
distinct.subscribeToStream { received.add(it) }

source(1)  // same — skipped
source(2)  // different — published
source(2)  // same — skipped
source(3)  // different — published

// received = [1, 2, 3]

Use distinctStrict() for reference equality (!==) instead of structural equality (!=).

fallback

Replace null values with a default:

val source = StreamSource<String?>(null)

val safe = source.fallbackTo("default")
println(safe())  // "default"

source("hello")
println(safe())  // "hello"

// Or compute the fallback dynamically
val computed = source.fallbackBy { "fallback-value" }