Kotlin Academy · Lesson

Why Sequences

Lazy evaluation.

Lesson 1 of 413 steps

Why Sequences is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

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.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Why Sequences” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Why Sequences”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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