Slide 1

Slide 1 text

Kotlin Sequences Deep Dive Phil Shadlyn | @physphil November 16, 2020 #dcAmericas

Slide 2

Slide 2 text

What is a Sequence?

Slide 3

Slide 3 text

What are Kotlin Sequences? “A sequence is an iterable type that can be operated upon without creating unnecessary intermediate collections, by running all applicable operations on each element before moving onto the next.” https://typealias.com/guides/kotlin-sequences-illustrated-guide/

Slide 4

Slide 4 text

Great... What does that mean?

Slide 5

Slide 5 text

An Example ● TV Show reference app ● Display 10 favourites and their rating ● Consume API of 30,000 TV Shows

Slide 6

Slide 6 text

data class NetworkTvShow( val id: String val title: String, val year: String?, val ratings: List?, val favourite: Boolean, val characters: List? ) data class TvShow( val title: String, val year: Int, val rating: Int, val favourite: Boolean ) Server model App model

Slide 7

Slide 7 text

Order of operations: ● Get list of shows from API ● Filter by favourite ● Convert network model to domain model ● Take first 10 elements ● Display in list

Slide 8

Slide 8 text

val favouritesList = tvShowApi.getShowsList() .filter { it.favourite } .map { it.toTvShow() } .take(10) view.displayFavouriteList(favouritesList)

Slide 9

Slide 9 text

val favouritesList = tvShowApi.getShowsList() .filter { it.favourite } .map { it.toTvShow() } .take(10) view.displayFavouriteList(favouritesList)

Slide 10

Slide 10 text

val favouritesList = tvShowApi.getShowsList() .filter { it.favourite } .map { it.toTvShow() } .take(10) view.displayFavouriteList(favouritesList) New collection

Slide 11

Slide 11 text

val favouritesList = tvShowApi.getShowsList() .filter { it.favourite } .map { it.toTvShow() } .take(10) view.displayFavouriteList(favouritesList) New collection

Slide 12

Slide 12 text

val favouritesList = tvShowApi.getShowsList() .filter { it.favourite } .map { it.toTvShow() } .take(10) view.displayFavouriteList(favouritesList) New collection

Slide 13

Slide 13 text

val favouritesList = tvShowApi.getShowsList() .filter { it.favourite } .map { it.toTvShow() } .take(10) view.displayFavouriteList(favouritesList)

Slide 14

Slide 14 text

val favouritesList = tvShowApi.getShowsList() .filter { it.favourite } .map { it.toTvShow() } .take(10) 30,000 shows 31,941 operations 30,000 operations 1,931 operations 10 operations 3 new collections

Slide 15

Slide 15 text

val favouritesList = tvShowApi.getShowsList() .asSequence() .filter { it.favourite } .map { it.toTvShow() } .take(10) .toList() 30,000 shows 39 operations 19 operations 10 operations 10 operations

Slide 16

Slide 16 text

Whoa… what just happened?

Slide 17

Slide 17 text

What are Kotlin Sequences? “A sequence is an iterable type that can be operated upon without creating unnecessary intermediate collections, by running all applicable operations on each element before moving onto the next.” https://typealias.com/guides/kotlin-sequences-illustrated-guide/

Slide 18

Slide 18 text

The Wire Favourite Seinfeld Favourite Breaking Bad Game of Thrones Favourite Mad Men filter { it.favourite } The Wire Seinfeld Game of Thrones map { it.toTvShow() } The Wire Seinfeld Game of Thrones take(10) The Wire Seinfeld Game of Thrones … … … …

Slide 19

Slide 19 text

Collection Processing ● Operations executed eagerly ● Operate on every element ● Intermediate collections

Slide 20

Slide 20 text

The Wire Favourite Seinfeld Favourite Breaking Bad Game of Thrones Favourite Mad Men filter { it.favourite } The Wire Seinfeld Game of Thrones map { it.toTvShow() } The Wire Seinfeld Game of Thrones take(10) The Wire Seinfeld Game of Thrones … …

Slide 21

Slide 21 text

Sequence Processing ● Operations executed lazily ● One element at a time ● Chain executed with terminal operator ● No intermediate collections ● Can short circuit

Slide 22

Slide 22 text

Intermediate Operators ● Return a Sequence ● Build out operation chain ● filter, map, take, etc...

Slide 23

Slide 23 text

val favouritesList = tvShowApi.getShowsList() .asSequence() .filter { it.favourite } .map { it.toTvShow() } .take(10) .toList()

Slide 24

Slide 24 text

Terminal Operators ● Return a result ● Trigger execution of operation chain ● toList(), count(), maxOf(), etc...

Slide 25

Slide 25 text

val favouritesList = tvShowApi.getShowsList() .asSequence() .filter { it.favourite } .map { it.toTvShow() } .take(10) .toList()

Slide 26

Slide 26 text

Sequence Summary ● Operations executed lazily ● Execute chain on each item ● Chain executed with terminal operator ● No intermediate collections ● Can short circuit

Slide 27

Slide 27 text

How can I create a Sequence?

Slide 28

Slide 28 text

Creating Sequences /** * Creates a [Sequence] instance that wraps the original * collection returning its elements when being iterated. */ public fun Iterable.asSequence(): Sequence val showSequence = tvShowApi.getShowsList().asSequence()

Slide 29

Slide 29 text

Creating Sequences /** * Creates a sequence that returns the specified values. */ public fun sequenceOf(vararg elements: T): Sequence val showSequence = sequenceOf(TheWire, Seinfeld, GameOfThrones)

Slide 30

Slide 30 text

Creating Sequences /** * Returns a sequence defined by the starting value [seed] * and the function [nextFunction] */ public fun generateSequence( seed: T?, nextFunction: (T) -> T? ): Sequence val incrementalSequence = generateSequence(1) { it + 1 }

Slide 31

Slide 31 text

Should I use a Collection or Sequence?

Slide 32

Slide 32 text

Use a Collection if: ● Few items ● Few operators ● Stateful operations ● Missing Sequence operators

Slide 33

Slide 33 text

Use a Sequence if: ● Many items ● Many operations ● Stateless operations ● Short circuits

Slide 34

Slide 34 text

Want to Learn More? ● Kotlin Sequences docs https://kotlinlang.org/docs/reference/sequences.html ● Kotlin Sequences: An Illustrated Guide https://typealias.com/guides/kotlin-sequences-illustrated-guide/ ● Kotlin Sequence Tutorial https://winterbe.com/posts/2018/07/23/kotlin-sequence-tutorial/ ● Kotlin Vocabulary: Collections and Sequences https://www.youtube.com/watch?v=77hfjIYwouw

Slide 35

Slide 35 text

Thank You! Phil Shadlyn | @physphil November 16, 2020 #dcAmericas