Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering High Performance with Kotlin

You're reading from   Mastering High Performance with Kotlin Overcome performance difficulties in Kotlin with a range of exciting techniques and solutions

Arrow left icon
Product type Paperback
Published in Jun 2018
Publisher Packt
ISBN-13 9781788996648
Length 316 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Igor Kucherenko Igor Kucherenko
Author Profile Icon Igor Kucherenko
Igor Kucherenko
Arrow right icon
View More author details
Toc

Sequences in Kotlin

Java 8 introduced lazy evaluation for collections using the Stream API. Kotlin has a similar feature called Sequence. The API of the Sequence class is similar to the API of the List class. But from the point of view of the return type, Sequence methods class can be classified into the following groups:

  • Intermediate
  • Terminal

Intermediate methods return an instance of the Sequence class and terminal methods return an instance of the List class. The Sequence class actually starts to perform evaluations when meeting the terminal operator.

To create an instance of the Sequence class, you can just invoke the asSequence() method on a list or range:

(0..1_000_000)
.asSequence()

This method looks as follows:

public fun <T> Iterable<T>.asSequence(): Sequence<T> {
return Sequence { this.iterator() }
}

Sequence is a method that creates a...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime