Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a tuple in Nim?

Tags:

nim-lang

Let's say I have a procedure getTuple(): (int, int, int). How do I iterate over the returned tuple? It doesn't look like items is defined for tuple, so I can't do for i in getTuple().

I initially had this returning a sequence, which proved to be a bottleneck. Can I get this to work without affecting performance? I guess as a last resort I could unroll my loop, but is there any way around that?

like image 748
Imran Avatar asked Feb 13 '18 08:02

Imran


1 Answers

OK, I figured this out. You can iterate over the tuple's fields:

let t = (2,4,6)

for x in t.fields:
  echo(x)
like image 59
Imran Avatar answered Dec 06 '22 20:12

Imran