summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authorMiran <narimiran@disroot.org>2020-09-22 13:08:36 +0200
committerGitHub <noreply@github.com>2020-09-22 13:08:36 +0200
commit11c377c1149a23657a7f0dd897866cb550ade8d1 (patch)
treedc3b5e26959b17950c70785b1999fd715969d8c0 /doc
parentab05e141c0ee298d42344e8a15101097e73ff2f9 (diff)
downloadNim-11c377c1149a23657a7f0dd897866cb550ade8d1.tar.gz
add `enumerate` macro (#15297)
* add `enumerate` macro

* address the comments

* put `enumerate` in its own module
Diffstat (limited to 'doc')
-rw-r--r--doc/manual.rst11
1 files changed, 6 insertions, 5 deletions
diff --git a/doc/manual.rst b/doc/manual.rst
index ada6261b9..8c167bf62 100644
--- a/doc/manual.rst
+++ b/doc/manual.rst
@@ -5508,10 +5508,11 @@ type ``system.ForLoopStmt`` can rewrite the entirety of a ``for`` loop:
 
   macro enumerate(x: ForLoopStmt): untyped =
     expectKind x, nnkForStmt
-    # we strip off the first for loop variable and use
-    # it as an integer counter:
+    # check if the starting count is specified:
+    var countStart = if x[^2].len == 2: newLit(0) else: x[^2][1]
     result = newStmtList()
-    result.add newVarStmt(x[0], newLit(0))
+    # we strip off the first for loop variable and use it as an integer counter:
+    result.add newVarStmt(x[0], countStart)
     var body = x[^1]
     if body.kind != nnkStmtList:
       body = newTree(nnkStmtList, body)
@@ -5520,7 +5521,7 @@ type ``system.ForLoopStmt`` can rewrite the entirety of a ``for`` loop:
     for i in 1..x.len-3:
       newFor.add x[i]
     # transform enumerate(X) to 'X'
-    newFor.add x[^2][1]
+    newFor.add x[^2][^1]
     newFor.add body
     result.add newFor
     # now wrap the whole macro in a block to create a new scope
@@ -5532,7 +5533,7 @@ type ``system.ForLoopStmt`` can rewrite the entirety of a ``for`` loop:
 
   # without wrapping the macro in a block, we'd need to choose different
   # names for `a` and `b` here to avoid redefinition errors
-  for a, b in enumerate([1, 2, 3, 5]):
+  for a, b in enumerate(10, [1, 2, 3, 5]):
     echo a, " ", b