for i in countdown(high(list), 0):
template pair: auto = list[i]
pair.modify(acc)
acc += pair.one
In the thread 'reverse seq mitems' Jehan mentioned above example. After that I read about template and auto in the manual but I have to admit I didn't really understand what's going on.
Here two alternatives:
2.
for i in countdown(high(list), 0):
var pair = list[i]
pair.modify(acc)
acc += pair.one
3.
for i in countdown(high(list), 0):
list[i].modify(acc)
acc += pair.one
What are the (dis)advantages of the different samples?
Here my incomplete take:
Would be nice to learn here from the far more experienced in Nim?
Thanks a lot. -- Manfred
var pair = list[i]
does a copy of the list item -- and that is not what we want of course. You may check my assumption, please let me know if I should be wrong. And I would say that the template in the above form is just something like an alias. (Note, in your third example you used pair, but it is not defined there.)
From what you say I understand that the template/auto thingie is a more efficient way (no actual copying) to avoid writing list[i] over and over (if there are many statements in the loop)?
< (Note, in your third example you used pair, but it is not defined there.)
Yes, it should be a second occurence of list[i]
the template/auto thingie is a more efficient way (no actual copying) to avoid writing list[i] over and over
Yes, I really think so. For this example. But of course templates (and macros) are very powerful in Nim, I still have to learn much...
I still have to learn much...
I most probably are more a beginner than you are.
I stumbled upon Nim a week ok and out of curiosity I tried to rewrite a utility of mine (which was written in a Scheme dialect). Although, I never coded in Python before (ok, I saw Python code before) it was really easy to do this. As the language looks quite sophisticated, the binaries are small and coding is real fun. Therefore, I will try to learn a bit more of Nim.
A template does a textual (well, AST) substitution. When you use let or var, you perform an assignment, which wasn't what you wanted, since you wanted to mutate the entry in place (note that it's not just about copying). For this type of application, you can think of template as providing sort of an alias mechanism.
The auto type means that the type of the template is automatically inferred.
A template does a textual (well, AST) substitution. When you use let or var, you perform an assignment, which wasn't what you wanted, since you wanted to mutate the entry in place (note that it's not just about copying).
You are right. So my sample no. 2 was plain wrong as of course list will not be changed in this case.
The auto type means that the type of the template is automatically inferred.
I think I got it now.
Thanks, Manfred