Kiryushin Andrey
01/21/2020, 1:29 PMIEnumerable
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?rocketraman
01/21/2020, 2:40 PMZach Klippenstein (he/him) [MOD]
01/21/2020, 7:04 PMZach Klippenstein (he/him) [MOD]
01/21/2020, 7:05 PMSequence
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.elizarov
01/22/2020, 9:46 AM