Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kotlin Standard Library Cookbook

You're reading from   Kotlin Standard Library Cookbook Master the powerful Kotlin standard library through practical code examples

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher Packt
ISBN-13 9781788837668
Length 242 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Samuel Urbanowicz Samuel Urbanowicz
Author Profile Icon Samuel Urbanowicz
Samuel Urbanowicz
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Ranges, Progressions, and Sequences FREE CHAPTER 2. Expressive Functions and Adjustable Interfaces 3. Shaping Code with Kotlin Functional Programming Features 4. Powerful Data Processing 5. Tasteful Design Patterns Adopting Kotlin Concepts 6. Friendly I/O Operations 7. Making Asynchronous Programming Great Again 8. Best Practices for the Android, JUnit, and JVM UI Frameworks 9. Miscellaneous 10. Other Books You May Enjoy

Traversing through ranges using progression with a custom step value

Besides doing so for the Iterator instances, progressions implementations for integral types, such as the Int, Long, and Char types, also include the step property. The step value specifies the intervals between the subsequent elements of a range. By default, the step value of a progression is equal to 1. In this recipe, we are going to learn how to traverse a range of alphabet characters with a step value equal to 2. In the result, we want to have every second alphabet letter printed to the console.

Getting ready

The Kotlin standard library provides a convenient way of creating progression with a custom step value. We can do so using an extension function for progressions of integral types called step(). We can also benefit from the infix notation and declare a progression with a custom step, as follows:

val progression: IntProgression = 0..1000 step 100

If we were to use progression in the for loop, it would iterate 10 times:

val progression: IntProgression = 0..1000 step 100
for
(i in progression) {
println(i)
}

We could also achieve the same result by iterating with the while loop, as follows:

var i = 0
while (i <= 1000) {
println(i)
i += 100
}

How to do it...

  1. Declare a range of the Char type using the downTo() function:
'z' downTo 'a'
  1. Convert the range to a progression with a custom step value using the step() function:
'z' downTo 'a' step 2
  1. Use the forEach() function to iterate through the elements of the progression and print each of them to the console:
('z' downTo 'a' step 2).forEach { character -> print(character) }

How it works...

In the result, we are going to get the following code printed to the console:

zxvtrpnljhfdb

In the beginning, we declared a range containing all the alphabet characters in decreasing order with the downTo() function. Then, we transformed the range a the custom progression containing every second character with the step() function. Finally, we are using the Iterable.forEach() function to iterate through the next elements of the progression and print each of them to the console.

The step() extension function is available for the IntProgression, LongProgression, and CharProgression types. Under the hood, it creates a new instance of a progression copying the properties of the original one and setting up the new step value.

See also

  • Apart from iteration, range expressions are useful for defining flow control conditions. You can read more about this in the Using range expressions with flow control statements recipe.

You have been reading a chapter from
Kotlin Standard Library Cookbook
Published in: Jul 2018
Publisher: Packt
ISBN-13: 9781788837668
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
Banner background image