Building a LazyList
Construct lazy sequences.
Construction Basics
There are several ways to build a LazyList. The simplest is the apply factory, just like building a List.
But the real power comes from constructors that keep the tail lazy, which we will explore in this lesson.
val ll = LazyList(10, 20, 30)
println(ll.head)The Empty LazyList
LazyList.empty is the terminator, the lazy equivalent of Nil. Every finite LazyList ends with it.
You use it as the base case when prepending elements with #::.
object Demo extends App {
val empty = LazyList.empty[Int]
println(empty.isEmpty)
println(empty.toList)
}All lessons in this course
- Laziness Explained
- Building a LazyList
- Infinite Streams
- Taking and Filtering Lazily