109: Learning Kotlin - Sequences the new Iterables episode artwork

EPISODE · Jan 16, 2018 · 22 MIN

109: Learning Kotlin - Sequences the new Iterables

from Fragmented - AI Developer Podcast · host Spec

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 Kotlin Sequence Java Iterable vs Iterator - stackoverflow.com 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

Episode metadata supplied by the publisher feed · Published Jan 16, 2018

Embed 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? Listen on and find out! Show notes at http://fragmentedpodcast.com/episodes/109/

Distinct summary based on available episode metadata or transcript content.

NOW PLAYING

109: Learning Kotlin - Sequences the new Iterables

0:00 22:36

No transcript for this episode yet

We transcribe on demand. Request one and we'll notify you when it's ready — usually under 10 minutes.

No similar episodes found.

No similar podcasts found.

Frequently Asked Questions

How long is this episode of Fragmented - AI Developer Podcast?

This episode is 22 minutes long.

When was this Fragmented - AI Developer Podcast episode published?

This episode was published on January 16, 2018.

Can I download this Fragmented - AI Developer Podcast episode?

Yes. Use the download control on the episode player to save the publisher-provided media file.
URL copied to clipboard!