4 ms·
I don't know Zig, but aren't we missing a return here? fn count_nonzero(a: []const i32) i32 { var count: i32 = 0; for (items) |value| { // "for" work
by sakex 4y ago
I don't know Zig, but aren't we missing a return here?
fn count_nonzero(a: []const i32) i32 {
var count: i32 = 0;
for (items) |value| { // "for" works only on arrays and slices, use >"while" for generic loops.
if (value == 0) {
continue;
}
count += 1; // there is no increment operator, but there are shortcuts for +=, \*=, >>= etc.
}
}
- anonymoushn 4y agoThat's correct.
- goldenshale 4y agoAlso shouldn't the input argument a be named items instead? From what I can see items is an invalid variable as is.
- sakex 4y agoMight be, I assumed it was one of the const-variables declared before, but didn't bother to check
- nrclark 4y agoI'm not super-great with Zig, but my understanding is that `count` should be the return value, post-increment. When the return value is unspecified, Zig will default to an implicit return of the last-calculated rvalue. The same behavior is also found in Ruby, Lisp, and some other languages as well.