Renames each proc to map, each is left deprecated.
This commit is contained in:
parent
d0bd5d5cc3
commit
91700f29e7
7 changed files with 48 additions and 12 deletions
|
|
@ -218,7 +218,7 @@ So "pure object oriented" code is easy to write:
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
stdout.writeln("Give a list of numbers (separated by spaces): ")
|
stdout.writeln("Give a list of numbers (separated by spaces): ")
|
||||||
stdout.write(stdin.readLine.split.each(parseInt).max.`$`)
|
stdout.write(stdin.readLine.split.map(parseInt).max.`$`)
|
||||||
stdout.writeln(" is the maximum!")
|
stdout.writeln(" is the maximum!")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,4 @@
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
echo "Give a list of numbers (separated by spaces): "
|
echo "Give a list of numbers (separated by spaces): "
|
||||||
stdin.readLine.split.each(parseInt).max.`$`.echo(" is the maximum!")
|
stdin.readLine.split.map(parseInt).max.`$`.echo(" is the maximum!")
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,8 @@
|
||||||
## This module implements operations for the built-in `seq`:idx: type which
|
## This module implements operations for the built-in `seq`:idx: type which
|
||||||
## were inspired by functional programming languages. If you are looking for
|
## were inspired by functional programming languages. If you are looking for
|
||||||
## the typical `map` function which applies a function to every element in a
|
## the typical `map` function which applies a function to every element in a
|
||||||
## sequence, it already exists as the `each` proc in the `system
|
## sequence, it already exists in the `system <system.html>`_ module in both
|
||||||
## <system.html>`_ module in both mutable and immutable styles.
|
## mutable and immutable styles.
|
||||||
##
|
##
|
||||||
## Also, for functional style programming you may want to pass `anonymous procs
|
## Also, for functional style programming you may want to pass `anonymous procs
|
||||||
## <manual.html#anonymous-procs>`_ to procs like ``filter`` to reduce typing.
|
## <manual.html#anonymous-procs>`_ to procs like ``filter`` to reduce typing.
|
||||||
|
|
|
||||||
|
|
@ -1460,15 +1460,51 @@ proc pop*[T](s: var seq[T]): T {.inline, noSideEffect.} =
|
||||||
result = s[L]
|
result = s[L]
|
||||||
setLen(s, L)
|
setLen(s, L)
|
||||||
|
|
||||||
proc each*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] =
|
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
|
deprecated.} =
|
||||||
|
## The well-known ``map`` operation from functional programming. Applies
|
||||||
## `op` to every item in `data` and returns the result as a sequence.
|
## `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)
|
newSeq(result, data.len)
|
||||||
for i in 0..data.len-1: result[i] = op(data[i])
|
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.}) =
|
proc each*[T](data: var openArray[T], op: proc (x: var T) {.closure.}) {.
|
||||||
## The well-known `map`:idx: operation from functional programming. Applies
|
deprecated.} =
|
||||||
|
## The well-known ``map`` operation from functional programming. Applies
|
||||||
## `op` to every item in `data` modifying it directly.
|
## `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])
|
for i in 0..data.len-1: op(data[i])
|
||||||
|
|
||||||
iterator fields*[T: tuple](x: T): TObject {.
|
iterator fields*[T: tuple](x: T): TObject {.
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ import json, tables
|
||||||
proc run(json_params: TTable) =
|
proc run(json_params: TTable) =
|
||||||
let json_elems = json_params["files"].elems
|
let json_elems = json_params["files"].elems
|
||||||
# These fail compilation.
|
# These fail compilation.
|
||||||
var files = each(json_elems, proc (x: PJsonNode): string = x.str)
|
var files = map(json_elems, proc (x: PJsonNode): string = x.str)
|
||||||
#var files = json_elems.each do (x: PJsonNode) -> string: x.str
|
#var files = json_elems.map do (x: PJsonNode) -> string: x.str
|
||||||
echo "Hey!"
|
echo "Hey!"
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int =
|
||||||
result = x("123")
|
result = x("123")
|
||||||
|
|
||||||
echo "Give a list of numbers (separated by spaces): "
|
echo "Give a list of numbers (separated by spaces): "
|
||||||
var x = stdin.readline.split.each(parseInt).max
|
var x = stdin.readline.split.map(parseInt).max
|
||||||
echo x, " is the maximum!"
|
echo x, " is the maximum!"
|
||||||
echo "another number: ", takeParseInt(parseInt)
|
echo "another number: ", takeParseInt(parseInt)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ from that model.
|
||||||
# Prints the maximum integer from a list of integers
|
# Prints the maximum integer from a list of integers
|
||||||
# delimited by whitespace read from stdin.
|
# delimited by whitespace read from stdin.
|
||||||
let tokens = stdin.readLine.split
|
let tokens = stdin.readLine.split
|
||||||
echo tokens.each(parseInt).max, " is the maximum."
|
echo tokens.map(parseInt).max, " is the maximum."
|
||||||
|
|
||||||
|
|
||||||
Nimrod is efficient
|
Nimrod is efficient
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue