# For (let i = len - 1; i \> 0; i /= 2)

**URL:** <https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573>\
**Category:** Uncategorized\
**Created:** [February 10, 2018, 12:08pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573 "2018-02-10T12:08:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rabbit](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rabbit](https://discuss.kotlinlang.org/u/rabbit)\
**Post date:** [February 10, 2018, 12:08pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/1 "2018-02-10T12:08:00Z")

</div>

How to write that in Kotlin without exposing variable “i” and implement it gracefully?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 10, 2018, 2:02pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/2 "2018-02-10T14:02:30Z")

</div>

You can use generateSequence for this. I’m not sure it is the best way, but it is cleaner than using a `while` loop.

```nohighlight
for (i in generateSequence(length - 1) {last -> (last / 2).let { if(it > 0) it else null }})

```

generateSequence will have a “next” element as long as it is not null ( I hope that makes sense). The other advantage to it is, that it is lazily executed.

---

<div class="post-metadata">

**Author:** ![rabbit](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rabbit](https://discuss.kotlinlang.org/u/rabbit)\
**Post date:** [February 10, 2018, 2:53pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/3 "2018-02-10T14:53:43Z")

</div>

Thanks for your help. It does work well. But I think it’s a little bit long(half of my screen).  
What do you think about removing `for(;;)` in Kotlin?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 10, 2018, 2:58pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/4 "2018-02-10T14:58:20Z")

</div>

I think there was a discussion about this here previously. I personally like the `for (i in 0..100 step 3)` syntax. I think it is much cleaner than the old `for(;;)`. Yours is the first example I have seen so far, where the old syntax is superior.

Btw I would pull the generateSequence part out into its own function and just call `for(i in halvingSequence(lenght - 1)`. That again would make your code cleaner and you can easily use it in other places as well.

---

<div class="post-metadata">

**Author:** ![rabbit](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rabbit](https://discuss.kotlinlang.org/u/rabbit)\
**Post date:** [February 10, 2018, 3:13pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/5 "2018-02-10T15:13:58Z")

</div>

I want the old syntax comes back. Q.Q The new syntax is not flexible, it can only be used in limited scene.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [February 10, 2018, 3:23pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/6 "2018-02-10T15:23:24Z")

</div>

I think it is a good example why `for(;;)` was removed. In this case what we have is an iteration with uncontrollable number of steps. It could have side effects and very hard to debug (something could be broken on step number 100000). Also it could pollute the memory.

The kotlinish way to treat this case would be infinite lazy sequence:

```kotlin
val sequence = buildSequence {
    var value = start
    while (value > 0){ // add any other conditions
       value /= 2
       yield(value)
    }
}

sequence.forEach{ <do something>}

```

In this case, you generate and consume numbers when you need them without creating intermediate large array. You can also limit the number of iterations and perform a lot of interesting [sequence operations](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence/index.html) without additional complications.

---

<div class="post-metadata">

**Author:** ![rabbit](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rabbit](https://discuss.kotlinlang.org/u/rabbit)\
**Post date:** [February 10, 2018, 3:43pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/7 "2018-02-10T15:43:44Z")

</div>

I think it is making a simple thing complex. I just want to get a simple sequence for a simple function. It’s not making things easier.

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [February 10, 2018, 6:43pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/8 "2018-02-10T18:43:06Z")

</div>

This is an improvement of @Wasabi375 suggestion

```
for (i in generateSequence(len - 1) { it / 2 }.takeWhile { it > 0 })

```

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [February 10, 2018, 7:10pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/9 "2018-02-10T19:10:18Z")

</div>

First, your code is broken. The loop does not terminate when `len` is greater than or equal to 1. Please take the time to ensure that your code samples are correct.

Second, even after switching to `i > 0`, the loop seems very odd. What is its purpose? If we know what you want to achieve, the best implementation in Kotlin can be decided upon. Now we only have a strange loop to improve.

---

<div class="post-metadata">

**Author:** ![rabbit](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rabbit](https://discuss.kotlinlang.org/u/rabbit)\
**Post date:** [February 11, 2018, 3:30am UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/10 "2018-02-11T03:30:20Z")

</div>

Thank you.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 11, 2018, 3:57am UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/11 "2018-02-11T03:57:54Z")

</div>

Edit: I misunderstood @SackCastellon. I did not know about `takeWhile` as I explained later. Both his and my version are working. I leave my original post here for context.

@SackCastellon I think you where thinking of `takeIf`. I forgot about it in my example. Fixing yours it would be

```nohighlight
for (i in generateSequence(length - 1) { last -> (last / 2).takeIf {it > 0 } }) {
...

```

Take if must be inside the `generateSequence` call. As @jstuyts pointed out your version would create a non terminating loop. `generateSequence` will generate a new entry as long as it is not null. By pulling the `takeIf` into the sequence the sequence is terminating.

---

<div class="post-metadata">

**Author:** ![rabbit](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rabbit](https://discuss.kotlinlang.org/u/rabbit)\
**Post date:** [February 11, 2018, 4:00am UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/12 "2018-02-11T04:00:57Z")

</div>

I am trying to implement Heap, `i = (i - 1) / 2` for calculating parent’s index.

```
fun insert(value: Int) {
        nodes.add(value)
// for ((child, parent) in generateSequence(nodes.lastIndex to (nodes.lastIndex - 1) / 2) {
// it.second to (it.second - 1) / 2
// }.takeWhile { it.second >= 0 && nodes[it.second] < nodes[it.first] }) {
// nodes[child] = nodes[parent].also { nodes[parent] = nodes[child] }
// }

        for ((child, parent) in generateSequence(nodes.lastIndex to (nodes.lastIndex - 1) / 2) {
            (it.second to (it.second - 1) / 2).takeIf { it.second >= 0 && nodes[it.second] < nodes[it.first] }
        }) {
            nodes[child] = nodes[parent].also { nodes[parent] = nodes[child] }
        }
    }

```

---

<div class="post-metadata">

**Author:** ![rabbit](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rabbit](https://discuss.kotlinlang.org/u/rabbit)\
**Post date:** [February 11, 2018, 5:21am UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/13 "2018-02-11T05:21:28Z")

</div>

One more question.  
How to implement it without exposing variables? (I know `run {}` can do that but it’s too ugly)

```
int[] arr = {/*...*/};
for (int a = 0, b = 1, c = 2; b < arr.length; b = a * 2 + 1, c = b + 1) {
    a = c < arr.length && arr[c] > arr[b] ? c : b;
}

```

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [February 11, 2018, 11:33am UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/14 "2018-02-11T11:33:51Z")

</div>

Both my code:

```
for (i in generateSequence(len - 1) { it / 2 }.takeWhile { it > 0 })

```

And yours:

```
for (i in generateSequence(len - 1) { (it / 2).takeIf { it > 0 } })

```

Produce the same result, the difference resides on how the work under the hood.

My code generates a sequence like `16, 8, 4, 2, 1, 0, 0, 0, ...` but the `takeWhile()` function tels the secuence to stop giving elemets when one of them doesn’t satisfy `{ it > 0 }`

However, your code generates a sequence like `16, 8, 4, 2, 1, null, null, null, ...` (because `takeIf()` is inside the [`nextFunction`](https://goo.gl/eeWziW)) but by default, sequences stop giving elements when one of them is `null`.

So, in the end, we have the same result which would be `16, 8, 4, 2, 1` in this case.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 11, 2018, 1:25pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/15 "2018-02-11T13:25:23Z")

</div>

Woops, sry you are right. I did not know about `takeWhile` and assumed you meant to use `takeIf`. In that case I prefer your version. I will edit my answer accordingly to not spread missinformation.

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [February 12, 2018, 5:43am UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/16 "2018-02-12T05:43:23Z")

</div>

Sorry, I still don’t understand what you are trying to achieve. You are adding a value to `nodes`, and then you are modifying values in `nodes` at multiple locations.

It is also very unclear what is happening because of the “idiomatic” Kotlin.

So I am afraid I cannot help you with my solution to your problem.

---

<div class="post-metadata">

**Author:** ![rabbit](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rabbit](https://discuss.kotlinlang.org/u/rabbit)\
**Post date:** [February 12, 2018, 3:06pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/18 "2018-02-12T15:06:45Z")

</div>

Just a kind of data structure Heap.

---

<div class="post-metadata">

**Author:** ![dico.karssiens](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@dico.karssiens](https://discuss.kotlinlang.org/u/dico.karssiens)\
**Post date:** [February 13, 2018, 5:52am UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/19 "2018-02-13T05:52:12Z")

</div>

I won’t ask you what you need this for, I don’t think that’s necessary to be able to answer the question.  
I think you should use a while loop. “Keep it simple stupid” (edit, see: KISS Principle, thanks to ilya from JetBrains for pointing out that I should add the reference).  
In the end, the java for loop will do the exact same thing.

If you don’t want the variable exposed, I can’t really help there. I guess you can just declare the variable `i` and then reuse it though, if you need another loop?

```auto
var i: Int

i = <some start value>
while (i > 0) {
    <do something>
    i /= 2
}

<repeat code>

```

You can go with the more kotlin-y solutions like what SackCastellon posted as well, but as you can see, you needed to ask around to find out how to do it. Everyone can use a while loop and everyone can understand a while loop.

---

<div class="post-metadata">

**Author:** ![dico.karssiens](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@dico.karssiens](https://discuss.kotlinlang.org/u/dico.karssiens)\
**Post date:** [February 13, 2018, 6:10am UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/20 "2018-02-13T06:10:30Z")

</div>

Alternatively, you can use something like the below. I don’t think this helper function exists in the stdlib.

```auto
fun example() {

    forLegacy(20, { it / 2 }, { it > 0 }) l@ { i ->
        // to continue
        return@l
    }

    forLegacyWithBreak(20, { it / 2 }, { it > 0 }) l@ { i ->
        // to break
        breakLoop()
        return@l
    }

}

inline fun <T> forLegacy(start: T, next: (T) -> T, cond: (T) -> Boolean, action: (T) -> Unit) {
    var cur = start
    while (cond(cur)) {
        action(cur)
        cur = next(cur)
    }
}

class ForWithBreak {
    var broken = false
    fun breakLoop() {
        broken = true
    }
}

inline fun <T> forLegacyWithBreak(start: T, next: (T) -> T, cond: (T) -> Boolean, action: ForWithBreak.(T) -> Unit) {
    val receiver = ForWithBreak()
    var cur = start
    while (cond(cur) && !receiver.broken) {
        receiver.action(cur)
        cur = next(cur)
    }
}

```

Advantages:

- It functions exactly like a legacy for loop, with the exception that you can’t declare multiple variables
- It produces the same bytecode (with the exception of the breakable one)
- Doesn’t create multiple lambda objects like `generateSequence` does (because it’s inlined)

---

<div class="post-metadata">

**Author:** ![gesh](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@gesh](https://discuss.kotlinlang.org/u/gesh)\
**Post date:** [February 14, 2018, 5:14pm UTC](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573/21 "2018-02-14T17:14:35Z")

</div>

TBH I’m glad Kotlin dropped `for(;;)` and I don’t see why anyone should use `for(i in Iterable)` directly in their code (apart from it being a more familiar syntax). After all, you can just write `Iterable.foreach { ... }` and it will inline the same code in the call site. Anyway back to your question and having the above in mind I would write:

```
generateSequence(len - 1) { it / 2 }.takeWhile { it > 0 }.forEach {_ -> ... }

```

I added `_` because you said you want to hide the number itself from the code inside your block. Also because this is just a sequence you can probably use some of the many transformation functions on sequences and avoid the `forEach` entirely. But

Going a step further you can generalize any `for(;;)` loop as follows:

```
for( <initial_value> ; <break_conditions> ; <action_steps> )

generateSequence(<initial_value>) { <action_steps> }
        .takeWhile { <break_conditions> }
        .forEach

```

It is more verbose than Java which is a rare thing in Kotlin 🙂

I have no idea what your code does, but I just reformatted it the way I would write it and used named pairs in all blocks for consistency.

```
generateSequence(nodes.lastIndex to (nodes.lastIndex - 1) / 2) { (child, parent) ->
    parent to (parent - 1) / 2
}.takeWhile { (child, parent) ->
    parent >= 0 && nodes[parent] < nodes[child]
}.forEach { (child, parent) ->
    nodes[child] = nodes[parent]
}

```

[Next page](https://discuss.kotlinlang.org/t/for-let-i-len-1-i-0-i-2/6573.md?page=2)
