https://kotlinlang.org logo
l

Leon K

02/19/2020, 1:04 PM
Sequence
is lazy
m

Matej Drobnič

02/19/2020, 1:04 PM
Aren't iterators also lazy?
f

Fleshgrinder

02/19/2020, 1:06 PM
They are and sequences are iterators. The difference is that sequences are suspendable.
no red 1
l

Leon K

02/19/2020, 1:06 PM
from the kotlin doc: " Along with collections, the Kotlin standard library contains another container type – sequences (Sequence<T>). Sequences offer the same functions as Iterable but implement another approach to multi-step collection processing. When the processing of an Iterable includes multiple steps, they are executed eagerly: each processing step completes and returns its result – an intermediate collection. The following step executes on this collection. In turn, multi-step processing of sequences is executed lazily when possible: actual computing happens only when the result of the whole processing chain is requested. " -> https://kotlinlang.org/docs/reference/sequences.html
l

Leon K

02/19/2020, 1:09 PM
with lazy i meant lazy in the way they apply and calculate transformations pretty sure normal iterators don't do it the same "lazy" way that sequences do
m

Matej Drobnič

02/19/2020, 1:10 PM
You described difference between
Sequence
and
Iterable
not between
Sequence
and
Iterator
l

Leon K

02/19/2020, 1:10 PM
that might be my mistake, yes
sorry in that case
f

Fleshgrinder

02/19/2020, 1:11 PM
An iterator can be anything, in Rust all iterators are by default lazy. That was not possible in Kotlin because they are backed by Java and Java iterators are mostly eager. It's an implementation detail.
l

Leon K

02/19/2020, 1:16 PM
yea, i just confused
Iterator<T>
with
Iterable<T>
😕
2 Views