jw
01/02/2016, 4:32 AMjw
01/02/2016, 4:32 AMapatrida
01/02/2016, 4:32 AMjw
01/02/2016, 4:32 AMapatrida
01/02/2016, 4:32 AMjw
01/02/2016, 4:32 AMapatrida
01/02/2016, 4:33 AMkittinunf
01/02/2016, 4:48 AMjw
01/02/2016, 4:49 AMSequence
is an Iterator
factory. To read the file again you would call .iterator()
again (just like you would on an Iterable
)jw
01/02/2016, 4:49 AMjw
01/02/2016, 5:04 AMasIterable()
and asSequence
on each, which at least makes it easy to re-use the implementationapatrida
01/02/2016, 5:40 AMjw
01/02/2016, 5:41 AMjw
01/02/2016, 5:42 AMapatrida
01/02/2016, 5:42 AMapatrida
01/02/2016, 5:42 AMjw
01/02/2016, 5:42 AMjw
01/02/2016, 5:42 AMapatrida
01/02/2016, 5:42 AMapatrida
01/02/2016, 5:42 AMapatrida
01/02/2016, 5:43 AMasSequence()
returns this type as well https://github.com/JetBrains/kotlin/blob/74c13906ed7fd47c0d68b64a0db7c467c93d7379/libraries/stdlib/src/kotlin/collections/Sequences.kt#L10-L18apatrida
01/02/2016, 5:44 AMjw
01/02/2016, 5:44 AMfun <T> Sequence<T>.defaultIfEmpty(value: T): Sequence<T> {
val upstream = this;
return object : Sequence<T> {
private val delegate: Iterator<T> by lazy {
val first = upstream.iterator()
if (first.hasNext()) first else listOf(value).iterator()
}
override fun iterator() = object: Iterator<T> by delegate {}
}
}
jw
01/02/2016, 5:44 AMjw
01/02/2016, 5:45 AMswitchIfEmpty
, just replace listOf(value).iterator()
with other.iterator()
jw
01/02/2016, 5:47 AMswitchIfEmpty(listOf(value))
actuallyapatrida
01/02/2016, 5:47 AMjw
01/02/2016, 5:48 AMapatrida
01/02/2016, 5:49 AMjw
01/02/2016, 5:50 AM