Generating infinite sequences lets you create endless lists of values without using too much memory. You can get values one by one as you need them.
Generating infinite sequences in Kotlin
val sequence = generateSequence(seed) { nextValue -> nextValue + 1 }
seed is the first value of the sequence.
The lambda after seed defines how to get the next value from the current one.
val numbers = generateSequence(1) { it + 1 }
val evenNumbers = generateSequence(0) { it + 2 }
val fibonacci = generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) }.map { it.first }
This program creates an infinite sequence of numbers starting at 1. It then takes the first five numbers and prints them as a list.
fun main() { val numbers = generateSequence(1) { it + 1 } val firstFive = numbers.take(5).toList() println("First five numbers: $firstFive") }
Infinite sequences do not store all values, so they save memory.
Use functions like take() to get a limited number of items; otherwise, the program may run forever.
You can transform sequences with map, filter, and other sequence functions.
Infinite sequences generate values one by one as needed.
They start from a seed and use a function to get the next value.
Always limit infinite sequences when using them to avoid endless loops.
Practice
sequence { yield(1); yieldAll(generateSequence(2) { it + 1 }) }Solution
Step 1: Understand sequence behavior
The sequence starts by yielding 1, then continues with an infinite sequence starting at 2, increasing by 1 each time.Step 2: Recognize infinite generation
SincegenerateSequencewith a lambda that adds 1 never returns null, it produces values endlessly.Final Answer:
Generates values endlessly, one at a time, starting from 1 -> Option CQuick Check:
Infinite sequence = endless values [OK]
- Thinking infinite sequences create fixed lists
- Assuming infinite sequences cause errors immediately
- Believing sequences stop after a few values
Solution
Step 1: Identify infinite sequence syntax
generateSequence(seed) { nextValue }creates an infinite sequence by applying the lambda repeatedly.Step 2: Check each option
generateSequence(0) { it + 2 } correctly usesgenerateSequence(0) { it + 2 }to produce even numbers endlessly. Options B and D create finite sequences. generateSequence(0, 2) has incorrect syntax.Final Answer:
generateSequence(0) { it + 2 } -> Option DQuick Check:
Correct syntax = generateSequence(0) { it + 2 } [OK]
- Using sequenceOf for infinite sequences
- Passing two arguments to generateSequence incorrectly
- Not providing a lambda for next values
val seq = generateSequence(1) { it + 3 }
println(seq.take(4).toList())Solution
Step 1: Understand sequence generation
The sequence starts at 1, then each next value adds 3: 1, 4, 7, 10, ...Step 2: Apply take and toList
take(4)takes first 4 values: 1, 4, 7, 10.toList()converts them to a list.Final Answer:
[1, 4, 7, 10] -> Option BQuick Check:
Sequence values = [1,4,7,10] [OK]
- Adding 1 instead of 3 each step
- Confusing take with drop
- Expecting a finite sequence without take
val odds = generateSequence(1) { it + 2 }
println(odds.take(5).toList())Solution
Step 1: Check generateSequence usage
The code usesgenerateSequence(1) { it + 2 }, which correctly generates odd numbers starting at 1.Step 2: Verify output and syntax
take(5).toList()safely takes first 5 odd numbers. No syntax or runtime error exists.Final Answer:
No error; code runs correctly and prints odd numbers -> Option AQuick Check:
Correct infinite sequence = No error; code runs correctly and prints odd numbers [OK]
- Thinking yield is needed inside generateSequence
- Expecting lambda to return null to continue
- Confusing seed value for even numbers
Solution
Step 1: Analyze generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) }.map { it.first }
generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) }.map { it.first } usesgenerateSequencewith pairs to generate Fibonacci pairs, then maps to first element, correctly producing Fibonacci numbers infinitely.Step 2: Analyze sequence { var a=0; var b=1; while(true) { yield(a); val c=a+b; a=b; b=c } }
sequence { var a=0; var b=1; while(true) { yield(a); val c=a+b; a=b; b=c } } uses asequencebuilder with a while(true) loop yielding Fibonacci numbers step-by-step, also correct and infinite.Step 3: Analyze generateSequence(0) { it + it }
generateSequence(0) { it + it } doubles the number each time, not Fibonacci, so it's incorrect.Final Answer:
Both A and B -> Option AQuick Check:
Both methods generate Fibonacci infinite sequences [OK]
- Using generateSequence without pairs for Fibonacci
- Trying to double numbers instead of summing previous two
- Not using yield in sequence builder for infinite values
Ready to prove it?
Pass the test and this pattern moves unknown
