Reputation: 4688
What are the differences between the following collections types in Scala: List
and LazyList
types?
Upvotes: 8
Views: 4261
Reputation: 48400
The question
What are the differences between
LazyList
andList
?
can be rephrased as the question
What are the differences between
Stream
andList
?
because by Scala 2.13 release notes
immutable.LazyList
replacesimmutable.Stream
.Stream
had different laziness behavior and is now deprecated. (#7558, #7000)
and the answer to the rephrased question is provided by what is the difference between Scala Stream vs Scala List vs Scala Sequence.
Performance judgements are best addressed by measurements within particular scenarios.
Upvotes: 4
Reputation: 4688
LazyList
is a new type introduced in Scala Standard Library 2.13.1.
scala.collection.immutable
package. The major difference between the common List
type is the fact that the elements of the LazyList
are computed lazily, so only those elements that are requested are computed. By this means, a lazy list can have an infinite number of elements.LazyList
and List
) are comparable.LazyList
is constructed with an operator having a similar-looking to the one specific to the List
type (::
), #::
.LazyList
can't produce a StackOverFlowError
in a recursive loop, as an old List
could do.Upvotes: 6