ifEmpty
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.
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() { //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 }
xxxxxxxxxx
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]
Thanks for your feedback!