Hacking with Kotlin: ranges

Photo by Stephan Seeber on Unsplash

As you may know, C# 8 introduces bounded ranges.

The syntax looks like this:

var array = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var subarray = array[3..5]; // The selected items: 3, 4

Can we achieve the same with Kotlin?

Of course we can!

We’ll start with a list:

val list = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

But there’s nicer syntax for that already:

val list = (0..10).toList()

And there’s already subList() function available:

println(list.subList(3, 5)) // [3, 4]

Let’s first make it work with the following syntax:

println(list.subList(3..5)) // Doesn't compile yet

For that we’ll use extension function that accepts range of integers:

fun <E> List<E>.subList(range: ClosedRange<Int>): List<E> {
return this.subList(range.start, range.endInclusive)
}

As you can see, 3..5 is just a ClosedRange that has start and endInclusive members

Now the following code should work:

println(list.subList(3..5)) // [3, 4]

Next we want the following nice syntax to work too:

println(list[3..5]) // Doesn't compile yet

For that we’ll have another extension function:

operator fun <E> List<E>.get(range: ClosedRange<Int>): List<E> {
return this.subList(range)
}

Since square brackets are special symbols, we mark this function as operator to be able to use them.

And we simply delegate the logic to the first function we had written.

We can also make it even shorter:

operator fun <E> List<E>.get(range: ClosedRange<Int>) = this.subList(range)

And now, we’re in C# 8 land:

println(list[3..5]) // [3, 4]

No need to wait for the next major version of your favorite language.

Happy Kotlin, and stay tuned for next post in series!

Solutions Architect @Depop, author of “Hands-on Design Patterns with Kotlin” book and “Web Development with Kotlin” course

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

RESTful APIs: Your Round-Trip Ticket to Structuring the Internet

Why is Pivotal’s Dojo so good?

Microsoft SQL Server for Beginners to expert | Free Certification Course

Introduction to ChatOps

Test Driven Refinement

MECH MASTER AIRDROP OPENS MORE SPECIAL REWARDS

Aspects of my pre-Microverse that are helping me to become a better developer.

Being part of a distributed team is cool — you can work how you want, when you want and (almost)…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alexey Soshin

Alexey Soshin

Solutions Architect @Depop, author of “Hands-on Design Patterns with Kotlin” book and “Web Development with Kotlin” course

More from Medium

How to Avoid Model Changes Nightmare in Java or Kotlin

Was Kotlin made to replace Java?

Introduction to Kotlin for Java Developers

Springboot Kotlin and Redis — Great partners