Source Edit

This module implements enumerate syntactic sugar based on Nim’s macro system.

Imports

since, macros

Macros

  1. macro enumerate(x: ForLoopStmt): untyped

Enumerating iterator for collections.

It yields (count, value) tuples (which must be immediately unpacked). The default starting count 0 can be manually overridden if needed.

Example:

  1. let a = [10, 20, 30]
  2. var b: seq[(int, int)] = @[]
  3. for i, x in enumerate(a):
  4. b.add((i, x))
  5. assert b == @[(0, 10), (1, 20), (2, 30)]
  6. let c = "abcd"
  7. var d: seq[(int, char)]
  8. for (i, x) in enumerate(97, c):
  9. d.add((i, x))
  10. assert d == @[(97, 'a'), (98, 'b'), (99, 'c'), (100, 'd')]

Source Edit