Hey all! I'm currently diving into Kotlin on JVM, ...
# stdlib
k
Hey all! I'm currently diving into Kotlin on JVM, coming from C# background, and I have some questions about iterables, sequences and streams, can anyone help me? My questions are the following • I understand the different intentions behind Iterable and Sequence implementations, however I still don't quite understand why two different interfaces with identical signatures are needed. In C# there is only one
IEnumerable
interface and its extension methods behave like the ones for
Sequence
- that is, they are evaluated lazily, processing items from the source one by one. Therefore in C# any LINQ method applied to a collection starts a chain of lazy methods operating on
IEnumerable
and we use
ToList
in the end if a materialized collection is needed as a result of the pipeline. In Kotlin, then, not only the lazy to eager transition should be done explicitly (by calling
toList
) but also an eager to lazy one (by calling
asSequence
). Is this explicitness the purpose of existing separate
Sequence
and
Iterable
interfaces? Why is this explicitness in going from eager to lazy evaluation needed? Or does it have something to do with the fact that
Iterable
comes from Java and has been therefore in some way not suitable for lazy evaluations? • I also had a glimpse of streams in Java. Am I right that conceptually this is the same as
Sequence
in Kotlin, so that
Sequence
should be used everywhere unless Java interop is required? Are there some advantages of streams over sequences or sequences over streams besides those imposed by the fact that one comes from Kotlin and the other from Java?
r
On most small Iterable's, eager evaluation will be faster than lazy. Note also the evaluation order of sequences and iterables are different. See https://stackoverflow.com/questions/35629159/kotlins-iterable-and-sequence-look-exactly-same-why-are-two-types-required. I believe you are right about streams. I think the only use case for streams in Kotlin is if you want to use their parallel processing mode.
💯 1
👍 1
z
Yea, the different types are required so the operators know whether to use lazy or eager eval, and so that they can return the same type.
Kotlin
Sequence
isn’t exactly like Java
Stream
, streams have lots of machinery for parallel processing and threading management. Sequences are just lazy Iterables. You may also be interested in
Flow
, which is kind of like a
Sequence
that can be asynchronous, hop threads, and has backpressure.
e
Also, stream is single-use and can be backed by a closeable resource (in which case the user must rember to use it), while Sequence is multi-use and cold (you don’t need to remember closing it, you can just drop it).