summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2012-01-29 11:35:10 +0100
committerAraq <rumpf_a@web.de>2012-01-29 11:35:10 +0100
commite27da767a2e833966a418d805abb45a9f172be47 (patch)
treea775e45d8c3ae8bc28bf1a980f897273bf0ae4b1
parente75be73688dece12ae4d0b844cb8c3cddb9d45be (diff)
downloadNim-e27da767a2e833966a418d805abb45a9f172be47.tar.gz
implemented/fixed #92
-rwxr-xr-xlib/system.nim32
-rwxr-xr-xweb/news.txt1
2 files changed, 33 insertions, 0 deletions
diff --git a/lib/system.nim b/lib/system.nim
index f8083eef8..1e3509174 100755
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1256,6 +1256,38 @@ iterator items*(a: cstring): char {.inline.} =
     yield a[i]
     inc(i)
 
+
+iterator pairs*[T](a: openarray[T]): tuple[key: int, val: T] {.inline.} =
+  ## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
+  var i = 0
+  while i < len(a):
+    yield (i, a[i])
+    inc(i)
+
+iterator pairs*[IX, T](a: array[IX, T]): tuple[key: IX, val: T] {.inline.} =
+  ## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
+  var i = low(IX)
+  if i <= high(IX):
+    while true:
+      yield (i, a[i])
+      if i >= high(IX): break
+      inc(i)
+
+iterator pairs*[T](a: seq[T]): tuple[key: int, val: T] {.inline.} =
+  ## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
+  var i = 0
+  while i < len(a):
+    yield (i, a[i])
+    inc(i)
+
+iterator pairs*(a: string): tuple[key: int, val: char] {.inline.} =
+  ## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
+  var i = 0
+  while i < len(a):
+    yield (i, a[i])
+    inc(i)
+
+
 proc isNil*[T](x: seq[T]): bool {.noSideEffect, magic: "IsNil".}
 proc isNil*[T](x: ref T): bool {.noSideEffect, magic: "IsNil".}
 proc isNil*(x: string): bool {.noSideEffect, magic: "IsNil".}
diff --git a/web/news.txt b/web/news.txt
index 5181baadf..db4658722 100755
--- a/web/news.txt
+++ b/web/news.txt
@@ -159,6 +159,7 @@ Library Additions
   the old implementation.
 - Added ``intsets.assign``.
 - Added ``system.astToStr`` and ``system.rand``, ``system.doAssert``.
+- Added ``system.pairs`` for built-in types like arrays and strings.
 
 
 2011-07-10 Version 0.8.12 released