So, Kotlin uses streams for laziness, because they are good when it comes to the main use cases of lazy collections: iteration, filtering, mapping, and random access is unlikely to be encountered very often.Īs you, again, correctly observe, drop lets you access elements by index, which makes the performance implications more explicit in the code. In Kotlin/Java, list is a data structure with random access to its elements, and the main operation is get(int), which can be implemented lazily, of course, but its performance will often be surprising for the user. In Haskell, a list is primarily a linked list, a pair of head and tail, and the main operation is taking a head of such a list, which is straightforward to efficiently implement lazily. Unlike Haskell, there's no such thing as a lazy list in Kotlin's standard library, and for a good reason: the primary meaning of "list" in Haskell world and Java world is different. Beginner developers who’ve had some experience with the fundamentals of the Kotlin language, who want to gain in-depth knowledge of working with collections and lambdas in Kotlin. As you correctly observed, sequenceOf ( streamOf() in older versions) is the way to get a lazy stream of numbers.
0 Comments
Leave a Reply. |