coming from Rust: does Kotlin `sequence` have an e...
# getting-started
y
coming from Rust: does Kotlin
sequence
have an equivalent of Rust’s
.next()
for `Iterator`s, which (lazily) returns the next item in the sequence if there is one, or
null
otherwise?
c
could you elaborate more on the specific use case? there are
map
and
filter
operations, and many others that suit specific cases. Sequences are, by design, lazy, so there’s typically no need to check for the next item.
y
well, a common use case for Rust iterators is chaining a couple of Iterator adapters (such as the above mentioned map and filter) on a collection, to create some Iterator object. then you use this Iterator, kind of like a Python generator: you can check if there’s a next value by calling
.next()
on the iterator, which is lazily evaluated, and decide what to do based on what is yielded.
(sorry for bringing up other languages, I’m just used to this paradigm)
c
typically no need to check for a next value - is this conceptually what you are after?
Copy code
listOf("a","b","c").asSequence().map { "mapped-$it" }.filter { it.endsWith("b") }.toList()
y
no, this collects
r
You could do
.take(1).singleOrNull()
c
it does collect - that was the point - do the “decide what to do on each element” in the mapping.
that is the typically paradigm for functional constructs/
y
@Ruckus that would work, thanks.
here’s an example: maybe you’re iterating over a stream of tokens
and you want to read up to a certain point, but you don’t know in advance how far you’re going to read.
you don’t want to allocate for the entire stream
so you’d go, if the next token is whatever, then use it and return
c
Copy code
val tokenStream = listOf("a","b","c").asSequence()
    tokenStream.filter { it.endsWith("b") }.forEach { 
        // do something with each 'b' token
    }
Or this if you only want the first matching item.
Copy code
val tokenStream = listOf("a","b","c").asSequence()
    tokenStream.filter { it.endsWith("b") }.firstOrNull()
☝️ 1
y
but maybe you’d want to go back to that iterator later on in the code, to start iterating from where you last stopped or maybe once you reached some point in the sequence, you want to apply more iterator adaptors so that API is a bit more flexible but I’m sure I can make Kotlin work for me just as well.
would just take getting used to, so far it’s a good experience
c
all doable. you could save the sequence at key spots to continue processing (differently) from there if need be. yea, it’s an adjustment from other languages that don’t have as much functional programming stuff baked in.
y
Kotlin sequences aren't necessarily meant to be used as such. In fact,
Iterator
s are exactly used for this purpose. However, if you want laziness, you'll need to use either `Channel`s or `Flow`s. The former is hot, so you can
poll
it and figure out if it has a value ready, and the latter is cold, so it will lazily get the next value, and if it is empty I think there is a build-in way to signal that, so you can return
null
then
y
@Youssef Shoaib [MOD] thanks a lot!