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
}
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/if-empty.html