W3cubDocs

/Kotlin

ifEmpty

Platform and version requirements: JVM (1.3), JS (1.3), 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
}

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/if-empty.html