summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/system.nim17
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/system.nim b/lib/system.nim
index 23dbfc816..ca7ef0bc6 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -438,6 +438,11 @@ proc min*(x, y: int32): int32 {.magic: "MinI", noSideEffect.}
 proc min*(x, y: int64): int64 {.magic: "MinI64", noSideEffect.}
   ## The minimum value of two integers.
 
+proc min*[T](x: openarray[T]): T = 
+  ## The minimum value of an openarray.
+  result = x[0]
+  for i in 1..high(x): result = min(result, x[i])
+
 proc max*(x, y: int): int {.magic: "MaxI", noSideEffect.}
 proc max*(x, y: int8): int8 {.magic: "MaxI", noSideEffect.}
 proc max*(x, y: int16): int16 {.magic: "MaxI", noSideEffect.}
@@ -445,6 +450,11 @@ proc max*(x, y: int32): int32 {.magic: "MaxI", noSideEffect.}
 proc max*(x, y: int64): int64 {.magic: "MaxI64", noSideEffect.}
   ## The maximum value of two integers.
 
+proc max*[T](x: openarray[T]): T = 
+  ## The maximum value of an openarray.
+  result = x[0]
+  for i in 1..high(x): result = max(result, x[i])
+
 proc `+%` *(x, y: int): int {.magic: "AddU", noSideEffect.}
 proc `+%` *(x, y: int8): int8 {.magic: "AddU", noSideEffect.}
 proc `+%` *(x, y: int16): int16 {.magic: "AddU", noSideEffect.}
@@ -1102,6 +1112,13 @@ proc pop*[T](s: var seq[T]): T {.inline.} =
   result = s[L]
   setLen(s, L)
 
+proc each*[T, S](data: openArray[T], op: proc (x: T): S): seq[S] = 
+  ## The well-known ``map`` operation from functional programming. Applies
+  ## `op` to every item in `data` and returns the result as a sequence.
+  result = @[]
+  for d in items(data): add(result, op(d))
+
+
 # ----------------- FPU ------------------------------------------------------
 
 #proc disableFPUExceptions*()