0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

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

  1. Laziness Explained
  2. Building a LazyList
  3. Infinite Streams
  4. Taking and Filtering Lazily
← Back to Scala for Backend Engineering & Functional Programming