ifEmpty

Common
JVM
JS
Native
1.3
fun <T> Sequence<T>.ifEmpty(
    defaultValue: () -> Sequence<T>
): Sequence<T>

Returns a sequence that iterates through the elements either of this sequence or, if this sequence turns out to be empty, of the sequence returned by defaultValue function.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val empty = emptySequence<Int>()

val emptyOrDefault = empty.ifEmpty { sequenceOf("default") }
println(emptyOrDefault.toList()) // [default]

val nonEmpty = sequenceOf("value")

val nonEmptyOrDefault = nonEmpty.ifEmpty { sequenceOf("default") }
println(nonEmptyOrDefault.toList()) // [value]
//sampleEnd
}