diff options
author | Araq <rumpf_a@web.de> | 2013-01-23 13:30:26 -0800 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2013-01-23 13:30:26 -0800 |
commit | b0b4150df1507408a19ece95d79bb1700d05cdf0 (patch) | |
tree | 6dc46a00c2d9ab406453f8fbbeb6ba95a2c2a639 /lib | |
parent | 5568f85449cb193a0b81f0f3cb6c189cf0b4164c (diff) | |
parent | 91700f29e718e41026b536d72b9ff010a4574bf2 (diff) | |
download | Nim-b0b4150df1507408a19ece95d79bb1700d05cdf0.tar.gz |
Merge pull request #316 from gradha/pr_renames_each_to_map
Renames each proc to map, each is left deprecated.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/collections/sequtils.nim | 4 | ||||
-rwxr-xr-x | lib/system.nim | 44 |
2 files changed, 42 insertions, 6 deletions
diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 73713eec9..298e7f27e 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -12,8 +12,8 @@ ## This module implements operations for the built-in `seq`:idx: type which ## were inspired by functional programming languages. If you are looking for ## the typical `map` function which applies a function to every element in a -## sequence, it already exists as the `each` proc in the `system -## <system.html>`_ module in both mutable and immutable styles. +## sequence, it already exists in the `system <system.html>`_ module in both +## mutable and immutable styles. ## ## Also, for functional style programming you may want to pass `anonymous procs ## <manual.html#anonymous-procs>`_ to procs like ``filter`` to reduce typing. diff --git a/lib/system.nim b/lib/system.nim index 130b8532f..85d952376 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -1461,15 +1461,51 @@ proc pop*[T](s: var seq[T]): T {.inline, noSideEffect.} = result = s[L] setLen(s, L) -proc each*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] = - ## The well-known `map`:idx: operation from functional programming. Applies +proc each*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] {. + deprecated.} = + ## The well-known ``map`` operation from functional programming. Applies ## `op` to every item in `data` and returns the result as a sequence. + ## + ## **Deprecated since version 0.9:** Use the ``map`` proc instead. newSeq(result, data.len) for i in 0..data.len-1: result[i] = op(data[i]) -proc each*[T](data: var openArray[T], op: proc (x: var T) {.closure.}) = - ## The well-known `map`:idx: operation from functional programming. Applies +proc each*[T](data: var openArray[T], op: proc (x: var T) {.closure.}) {. + deprecated.} = + ## The well-known ``map`` operation from functional programming. Applies ## `op` to every item in `data` modifying it directly. + ## + ## **Deprecated since version 0.9:** Use the ``map`` proc instead. + for i in 0..data.len-1: op(data[i]) + +proc map*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] = + ## Returns a new sequence with the results of `op` applied to every item in + ## `data`. + ## + ## Since the input is not modified you can use this version of ``map`` to + ## transform the type of the elements in the input sequence. Example: + ## + ## .. code-block:: nimrod + ## let + ## a = @[1, 2, 3, 4] + ## b = map(a, proc(x: int): string = $x) + ## assert b == @["1", "2", "3", "4"] + newSeq(result, data.len) + for i in 0..data.len-1: result[i] = op(data[i]) + +proc map*[T](data: var openArray[T], op: proc (x: var T) {.closure.}) = + ## Applies `op` to every item in `data` modifying it directly. + ## + ## Note that this version of ``map`` requires your input and output types to + ## be the same, since they are modified in-place. Example: + ## + ## .. code-block:: nimrod + ## var a = @["1", "2", "3", "4"] + ## echo repr(a) + ## # --> ["1", "2", "3", "4"] + ## map(a, proc(x: var string) = x &= "42") + ## echo repr(a) + ## # --> ["142", "242", "342", "442"] for i in 0..data.len-1: op(data[i]) iterator fields*[T: tuple](x: T): TObject {. |