Lazy List, a Scala language concept
Last updated
The 'LazyList' type (previously known as 'Stream' in Scala) is used to describe a potentially infinite list that evaluates only when necessary ('lazily').
Key example
This example shows how a LazyList is evaluated.
In particular, notice that the values that were pulled out are persisted for futher retrieval.
Self-recursion
Perhaps most interesting is the canonical example of how to use LazyList, where it refers to itself: the Fibonacci sequence. Have a look at the solution here: FibonacciLazyList.
Memory safety
LazyList may persist the items in memory for longer than needed, so do be careful about them, and test their edge cases when you work with them.
In practice, use cases of LazyList are few, and it may make more sense to use things like View, which provides lazy non-persistent evaluation. Ultimately, it will depend on your use case.