downUntil

Kotlin has .. for upward closed ranges, downTo for downward closed ranges, and until for upward half-open ranges.

I’d like to propose a new downUntil function for the only missing combination: downward half-open ranges. For example, for (i in 4 downUntil 0) print(i) would print “4321”.

(At Duolingo we disallow .. and downTo for consistency since none of our other programming languages use closed ranges. This leaves the rather awkward (lastNum until firstNum + 1).reversed() as our internal recommendation for downward ranges in Kotlin.)

1 Like

Just so I understand, you use (lastNum until firstNum + 1).reversed() instead of firstNum downTo lastNum just so you don’t have to use closed ranges?
I get that you want consistency between programs inside of the company but restricting language features like that feels kind of strange too me. But then I personally don’t switch between languages that often.

That being said, I would support this function being added to the standard library. It is the logical next step, as we already have downTo and until. You might want to create an issue for this here. I’m not sure whether this should go through the KEEP process. Anyways an issue is the best first step I guess.
Until this is part of the standard library I’d suggest you just create the function yourself for your projects.

2 Likes

We don’t actually use that reversed snippet anywhere since we haven’t needed downward ranges yet, and we’ll probably end up creating our own downUntil if/when we do :slight_smile:

I’ve opened an issue at https://youtrack.jetbrains.com/issue/KT-24658

1 Like

I just needed a downUntil function and was surprised none exist. It’s disappointing none still exist 6 years later.

It’s a one liner, you can easily write it

1 Like

Let’s get rid of all standard library functions then, al3c says he can easily write them.

Also, it is not a one liner, there are dozens of overloads.