
109: Learning Kotlin - Sequences the new Iterables
About this episode
In this episode of Fragmented, we go back to learning some Kotlin and look at the Iterable like data structure introduced called "Sequences". What is a sequence? How is it different from Iterable? When should I use it?
Show Notes
Eager/Lazy
Eager evaluation:
val lst = listOf(1, 2)
val lstMapped: List = lst.map { print("$it "); it * it }
print("before sum ")
val sum = lstMapped.sum()
// prints "1 2 before sum"
Lazy evaluation:
val seq = sequenceOf(1, 2)
val seqMapped: Sequence = seq.map { print("$it "); it * it }
print("before sum ")
val sum = seqMapped.sum()
// prints "before sum 1 2"
Source stackoverflow.com answer
Intermediate and terminal operations
Notice that at each chain operation, a new temporary list is created:
data class Person(val name: String, val age: Int)
fun main(args: Array) {
val people =
listOf(Person("Chris Martin", 31),
Person("Will Champion", 32),
Person("Jonny Buckland", 33),
Person("Guy Berryman", 34),
Person("Mhris Cartin", 30))
println(people
.filter { it.age > 30 } // new temp. list
.map {
it.name.split(" ").map {it[0]}.joinToString("")
} // new temp. list
.map { it.toUpperCase() }) // new temp. list
}
Using a sequence:
println(people
.asSequence() // convert to sequence
.filter { it.age > 30 } // lazy eval (intermediate op)
.map {
it.name.split(" ").map {it[0]}.joinToString("")
} // lazy eval (intermediate op)
.map { it.toUpperCase() } // lazy eval (intermediate op)
.toList() // terminal operation
)
Without a terminal operation, Sequences won't print anything:
val seq = sequenceOf(1, 2, 3)
println(seq) // prints address
println(seq.toList()) // [1, 2, 3]
You can't pick an index from a sequence:
println(seq[0]) // throws ERROR "No get method providing array access"
println(seq.toList()[0]) // 1
Sponsors
- Mapbox - Android developers don't have to settle for a default same-map-no-matter-what option in their Android app. Mapbox offers complete map design control, allowing you to create beautiful custom maps to meet the needs of your Android users.
Check them out today at mapbox.com/android
Contact
- @fragmentedcast [twitter.com]
- @donnfelker and +DonnFelker
- @kaushikgopal and +KaushikGopalIsMe
Get every episode summarized
Each time Fragmented - AI Developer Podcast publishes, we email you a written briefing from the transcript — the topics, who appeared, and any specific claims, with the ad reads skipped.
Email me new episodesFree for 3 shows. No card needed.
Hosts & guests
No transcript yet
This episode has not been transcribed. Request it and it moves to the front of the queue.
More episodes
More from Fragmented - AI Developer Podcast

311 - Self learning harnesses are here
Fragmented - AI Developer Podcast

310 - Mitchell Hashimoto on Ghostty & His Agentic Coding Workflow
Fragmented - AI Developer Podcast

309 - Background Agents
Fragmented - AI Developer Podcast

308 - How Image Diffusion Models Work - the 20 minute explainer
Fragmented - AI Developer Podcast