Nim enumeration function such as Python

Learning Nim , and I like this resemblance to Python (but fast). In Python, I can do this:

item_index = [(idx, itm) for idx, itm in enumerate(row)] 

Im looking for a way to list a Nim sequence, so I would write the following:

 item_index = lc[(idx, itm) | (idx, itm <- enumerate(row))] 

Does this functionality exist? I'm sure you can create it, perhaps with proc, template or macro, but I'm still very new, and it seems to me that it's still hard to create yourself. Here is my attempt:

 iterator enumerate[T](s: seq[T]): (int, T) = var i = 0 while i < len(s): yield (i, s[i]) i += 1 
+5
source share
1 answer

I'm new to nim, and I'm not quite sure what you want, but ... If you use two variables in a for statement, you will get the index and value:

 for x, y in [11,22,33]: echo x, " ", y 

gives:

 0 11 1 22 2 33 

NTN.

+6
source

Source: https://habr.com/ru/post/1274535/


All Articles