Creating Sequences
From collections and generators.
Creating Sequences is a free Kotlin Academy lesson on CoddyKit — lesson 2 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.
Creating Sequences
There are several ways to create a Sequence: from existing collections, from elements, with a generator function, or with a builder. This lesson covers each.
From a Collection with asSequence
The most common approach is to call asSequence() on an existing collection to process it lazily.
fun main() {
val seq = listOf(1, 2, 3, 4).asSequence()
val result = seq.map { it * 2 }.toList()
println(result)
}From Elements with sequenceOf
sequenceOf creates a sequence directly from a fixed set of values.
fun main() {
val seq = sequenceOf("a", "b", "c")
seq.forEach { println(it) }
}Infinite Sequences with generateSequence
generateSequence builds a (possibly infinite) sequence from a seed and a function that computes the next element. Pair it with take to limit it.
fun main() {
val powers = generateSequence(1) { it * 2 }
val first5 = powers.take(5).toList()
println(first5)
}Stopping generateSequence
If the next-element function returns null, the sequence ends. This lets you generate until a condition is met.
fun main() {
val countdown = generateSequence(5) { if (it > 1) it - 1 else null }
println(countdown.toList())
}generateSequence Without a Seed
You can pass a single producer lambda that returns the next value or null. This is handy for reading input streams lazily.
fun main() {
var n = 0
val seq = generateSequence { if (n < 3) n++ else null }
println(seq.toList())
}The sequence Builder
The sequence { } builder lets you produce elements imperatively using yield (one value) and yieldAll (many values).
fun main() {
val seq = sequence {
yield(1)
yield(2)
yieldAll(listOf(3, 4))
}
println(seq.toList())
}Lazy Yielding
The builder is lazy too: code between yields runs only as elements are pulled. Combined with take, you can describe infinite generators safely.
fun main() {
val naturals = sequence {
var i = 1
while (true) {
yield(i)
i++
}
}
println(naturals.take(4).toList())
}Fibonacci with generateSequence
A classic example: generate Fibonacci numbers lazily using a Pair as the state.
fun main() {
val fib = generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) }
.map { it.first }
println(fib.take(8).toList())
}Converting Back to a Collection
A sequence becomes a concrete collection only when you call a terminal operation like toList(), toSet(), or toMutableList().
fun main() {
val set = sequenceOf(1, 2, 2, 3).toSet()
println(set)
}Choosing a Creation Method
Use asSequence() for existing data, sequenceOf for fixed values, generateSequence for computed series, and the sequence { } builder for custom imperative generation.
Quick Check
How does generateSequence know when to stop?
Recap
You can create sequences with asSequence(), sequenceOf, generateSequence (seed plus next, ends on null), and the sequence { } builder with yield/yieldAll. Next you will see how lazy operations avoid intermediate lists.
Frequently asked questions
Is the “Creating Sequences” lesson free?
Yes — the full text of “Creating 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 “Creating Sequences”?
From collections and generators. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Creating 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
- Why Sequences
- Creating Sequences
- Lazy Operations
- Sequences vs Collections