diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2009-05-11 19:28:25 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2009-05-11 19:28:25 +0200 |
commit | 233687adf7613305664d9e02c5f37e898f7ba255 (patch) | |
tree | 2d069f01bc358cd9cd33247fd15714382bf6b992 | |
parent | eb6d77e46dd127b13e3fe3d07cee1d01a67d79ec (diff) | |
download | Nim-233687adf7613305664d9e02c5f37e898f7ba255.tar.gz |
added generic min;max;each
-rw-r--r-- | lib/system.nim | 17 |
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*() |