Hi,
For some very simple function like +=1 if the element of this array is 0, is there any concise way?
c=[0,1,0]
map(x->x=0, c) #but I wish to add a **if** in this one-line for loop
#otherwise how verbose my usual solution is:
function plusone(c) #empirically saying, need to make it a function for outputing
#the changed variable to main macro (instead of losing)
for i in 1:length(c)
if i == 0
c[i] = 1
end
return c #output make no dif
end
end
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.
What’s the purpose of having interpolation of global variable into the benchmark? Is this for mimicing the indexing of variable in a function or macro?