summary refs log tree commit diff stats
diff options
context:
space:
mode:
authordef <dennis@felsin9.de>2014-07-09 18:43:00 +0200
committerdef <dennis@felsin9.de>2014-07-09 18:43:00 +0200
commitc591db16c854780cae2f97ec096fed9835a8511b (patch)
tree1f7aee2b828ab78c9d6f0f704979720d9507d72d
parentec12922c43d132bbff452b1ea57bdabb5b8695de (diff)
downloadNim-c591db16c854780cae2f97ec096fed9835a8511b.tar.gz
Add reversed proc
-rw-r--r--lib/pure/algorithm.nim14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim
index 86d329763..7af1b2ad8 100644
--- a/lib/pure/algorithm.nim
+++ b/lib/pure/algorithm.nim
@@ -34,6 +34,20 @@ proc reverse*[T](a: var openArray[T]) =
   ## reverses the array `a`.
   reverse(a, 0, a.high)
 
+proc reversed*[T](a: openArray[T], first, last: int): seq[T] =
+  ## returns the reverse of the array `a[first..last]`.
+  result = newSeq[T](last - first)
+  var x = first
+  var y = last
+  while x < last:
+    result[x] = a[y]
+    dec(y)
+    inc(x)
+
+proc reversed*[T](a: openArray[T]): seq[T] =
+  ## returns the reverse of the array `a`.
+  reversed(a, 0, a.high)
+
 proc binarySearch*[T](a: openArray[T], key: T): int =
   ## binary search for `key` in `a`. Returns -1 if not found.
   var b = len(a)