Recommend mapIt in some cases (#20347)
* Recommend `mapIt` in some cases * Remove runnableExample
This commit is contained in:
parent
7a756bfaef
commit
7023176dcb
1 changed files with 4 additions and 6 deletions
|
|
@ -391,6 +391,10 @@ macro collect*(init, body: untyped): untyped {.since: (1, 1).} =
|
||||||
|
|
||||||
macro collect*(body: untyped): untyped {.since: (1, 5).} =
|
macro collect*(body: untyped): untyped {.since: (1, 5).} =
|
||||||
## Same as `collect` but without an `init` parameter.
|
## Same as `collect` but without an `init` parameter.
|
||||||
|
##
|
||||||
|
## **See also:**
|
||||||
|
## * `sequtils.toSeq proc<sequtils.html#toSeq.t%2Cuntyped>`_
|
||||||
|
## * `sequtils.mapIt template<sequtils.html#mapIt.t%2Ctyped%2Cuntyped>`_
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
import std/[sets, tables]
|
import std/[sets, tables]
|
||||||
let data = @["bird", "word"]
|
let data = @["bird", "word"]
|
||||||
|
|
@ -411,10 +415,4 @@ macro collect*(body: untyped): untyped {.since: (1, 5).} =
|
||||||
for i, d in data.pairs: {i: d}
|
for i, d in data.pairs: {i: d}
|
||||||
assert m == {0: "bird", 1: "word"}.toTable
|
assert m == {0: "bird", 1: "word"}.toTable
|
||||||
|
|
||||||
# avoid `collect` when `sequtils.toSeq` suffices:
|
|
||||||
assert collect(for i in 1..3: i*i) == @[1, 4, 9] # ok in this case
|
|
||||||
assert collect(for i in 1..3: i) == @[1, 2, 3] # overkill in this case
|
|
||||||
from std/sequtils import toSeq
|
|
||||||
assert toSeq(1..3) == @[1, 2, 3] # simpler
|
|
||||||
|
|
||||||
result = collectImpl(nil, body)
|
result = collectImpl(nil, body)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue