Quick 'for' and 'if' one-line loop

In general, you can use && as an if condition as in i==0 && c[i]+=1 (|| as well)
In this case, you could also do

for i in 1:length(c)
    c[i] += (c[i] == 0)
end

It should also be noted that the function is not the same as the original map. map does not modify c in place. You could do map!(x -> x + (x==0), c, c) for the same effect though.

It is almost always possible to condense short for loops and if conditions into a single line, but it’s not often worth it IMO.

1 Like