0Pricing
Kotlin Academy · Lesson

Why Sequences

Lazy evaluation.

Why Sequences is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Sequences?

A Sequence is like a collection but processes elements lazily. Operations are not run until a terminal operation requests results, and elements flow through the whole chain one at a time.

Eager Collections

Standard collection operations are eager: each step (filter, map) fully processes the list and creates a new intermediate list before the next step begins.

fun main() {
    val result = listOf(1, 2, 3, 4)
        .map { it * 2 }
        .filter { it > 4 }
    println(result)
}

The Cost of Intermediate Lists

For large data, eager processing allocates a new list at every step. With many operations or millions of elements, this wastes memory and time.

Lazy Sequences

A sequence processes each element through the entire pipeline before moving to the next. No intermediate lists are created. Convert with asSequence().

fun main() {
    val result = listOf(1, 2, 3, 4)
        .asSequence()
        .map { it * 2 }
        .filter { it > 4 }
        .toList()
    println(result)
}

Seeing the Execution Order

Adding prints reveals the difference. With a sequence, each element travels through map then filter before the next element starts.

fun main() {
    listOf(1, 2, 3).asSequence()
        .map { println("map $it"); it * 2 }
        .filter { println("filter $it"); it > 2 }
        .toList()
}

Eager Execution Order

Compare with a normal list: map runs for ALL elements first, then filter runs for all elements.

fun main() {
    listOf(1, 2, 3)
        .map { println("map $it"); it * 2 }
        .filter { println("filter $it"); it > 2 }
}

Short-Circuiting

Lazy evaluation enables short-circuiting: operations like first or find stop as soon as a result is found, avoiding work on the rest.

fun main() {
    val firstBig = (1..1000000).asSequence()
        .map { it * 2 }
        .first { it > 10 }
    println(firstBig)
}

Intermediate vs Terminal Operations

Sequence operations split into two kinds:

  • Intermediate (map, filter, take): lazy, return another sequence.
  • Terminal (toList, sum, first): trigger the computation and produce a value.

Nothing Happens Without a Terminal

An intermediate chain alone runs no code. Until a terminal operation is called, the lambdas never execute.

fun main() {
    val seq = listOf(1, 2, 3).asSequence()
        .map { println("processing $it"); it }
    println("Nothing printed yet")
    seq.toList()
}

When Sequences Help

Sequences shine with large datasets, long operation chains, or when you only need part of the result (like the first match). For small lists, plain collections are simpler and just as fast.

A Mental Picture

Think of a collection as processing in horizontal layers (all elements per step), and a sequence as processing in vertical streams (one element through all steps). This is why sequences avoid intermediate lists.

Quick Check

Why can sequences be more efficient than collections?

Recap

Sequences process data lazily, element by element, through a chain of intermediate operations, with computation triggered only by a terminal operation. This avoids intermediate lists and enables short-circuiting. Next you will learn how to create sequences.

Frequently asked questions

Is the “Why Sequences” lesson free?

Yes — the full text of “Why Sequences” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Why Sequences”?

Lazy evaluation. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why Sequences” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Sequences
  2. Creating Sequences
  3. Lazy Operations
  4. Sequences vs Collections
← Back to Kotlin Academy