#convenient and fast way to count down to zero with unsigned index?

1 messages · Page 1 of 1 (latest)

sage kiln
#

When you want to count down to zero while using an unsigned index it is a bit inconvenient. I tried for(3..-1) but it seems negative/custom step sizes aren't supported at all.

Here are the variations I have come up with so far:

const len = 3;
{
    std.debug.print("=====================\nfor:\n", .{});
    const e = len;
    for (0..e) |j| { // no ranges that count down
        const i = e - j - 1;
        std.debug.print("i: {}\n", .{i});
    }
}
{
    std.debug.print("=====================\nfor 2:\n", .{});
    const e = len + 1;
    for (1..e) |j| { // no ranges that count down
        const i = len - j;
        std.debug.print("i: {}\n", .{i});
    }
}
{
    std.debug.print("=====================\nwhile:\n", .{});
    var j: u16 = len;
    while (j > 0) : (j -= 1) {
        const i = j - 1;
        std.debug.print("i: {}\n", .{i});
    }
}
{
    std.debug.print("=====================\nfor 3:\n", .{});
    var i: u16 = len;
    for (0..len) |_| {
        i -= 1;
        std.debug.print("i: {}\n", .{i});
    }
}

Currently I am leaning towards for 3, do you have a better one?

sturdy marsh
#

^ similar issue

#

i kinda like 3. this is my goto

var i =  items.len - 1;
while (true) : (i -= 1) { 
    _ = items[i]; 
    if(i == 0) break; 
}
sage kiln
#

Ah i had not considered just using a while(true) loop yet thought about while(i >= 0) (which is unnecessary with the break). I like it for simple loops.
Currently I have a few loops that call continue at different points which would make it difficult to place the if at the right place.

#

The thing I like about for 3 is that it combines the "initialization of i needs to point to last element (len - 1) with the i needs to be updated and that it doesn't require an if in the body"

#

@sturdy marsh what language do you specify for your code block?

sturdy marsh
#

```ts

sage kiln
#

thanks that is definitely better than no highlighting

sturdy marsh
#

np. think rs and js, maybe dart are good too

bleak portal
#

I don't like #3 because it forces you to add that extra pair of braces, or to inject a new variable in the scope (same problems with while). This is how I am writing my array iterations nowadays:

// iteration forward
for (array, 0..) |_, index| {
  const p: usize = index;
  // use array[p]
  // you can also capture the element, but it looks different in the next case
}

// iteration backwards
for (array, 0..) |_, index| {
  const p: usize = array.len - index - 1;
  // use array[p]
}
#

Hum... I entered that code snippet with three backticks and the word zig right after, but I don't see any color formatting. What's the secret sauce?

valid elbow
valid elbow
valid elbow
#

But that is also true of the while loop anyway

bleak portal
low steeple
#

fwiw my preferred one is usually this:

var i: u16 = len;
while (i > 0) {
    i -= 1;
    std.debug.print("i: {}\n", .{i});
}

like the third, but just decrement the var at the top of the loop rather than making a pointless second variable. ofc if i'm doing that i do usually wrap it in a scope to prevent leakage