KStack

class KStack<T>(initialItems: List<T> = emptyList())(source)

A reactive LIFO stack.

Pairs naturally with KQueue for producer/consumer patterns.

val stack = KStack<String>()
stack.push("first")
stack.push("second")

println(stack.peek()) // "second"
println(stack.pop()) // "second"
println(stack.size) // 1

stack.state.collect { items -> render(items) }

Parameters

initialItems

Items to pre-populate (index 0 = bottom of stack).

Constructors

Link copied to clipboard
constructor(initialItems: List<T> = emptyList())

Properties

Link copied to clipboard

true when the stack contains no items.

Link copied to clipboard
val size: Int

Number of items currently in the stack.

Link copied to clipboard
val state: StateFlow<List<T>>

Stack contents as a StateFlow (index 0 = bottom, last = top).

Functions

Link copied to clipboard
fun clear()

Removes all items.

Link copied to clipboard
fun peek(): T?

Returns the top item without removing it, or null if empty.

Link copied to clipboard
fun pop(): T?

Removes and returns the top item, or null if empty.

Link copied to clipboard
fun push(item: T)

Pushes item onto the top of the stack.

Link copied to clipboard
open override fun toString(): String