Book Image

Programming Kotlin

Book Image

Programming Kotlin

Overview of this book

Quickly learn the fundamentals of the Kotlin language and see it in action on the web. Easy to follow and covering the full set of programming features, this book will get you fluent in Kotlin for Android.
Table of Contents (20 chapters)
Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Sequences


We defined what a sequence is and what it does at the start of this chapter. Sequences are great for scenarios when the size of the collection is not known in advance. Think about reading a table from a database, where you wouldn't know how many records you will get back; or reading a local .csv file, where you don't know how many lines it contains. You can think of a sequence as a list that goes on and on. A sequence is evaluated on a need-to-know basis, and only to the point needed. Think of the Fibonacci series; there is no point in constructing the collection in advance. How many items do you need to compute? The caller determines that.

If you have worked with Scala or Java 8, you will see the sequences as the Kotlin equivalent of Stream types. Since Kotlin supports Java 6 and it doesn't support a streaming library, they had to come with their own version. To avoid the confusion with Java 8, the Kotlin team has chosen this term. Unfortunately, the Kotlin library doesn't come...