Recommend mapIt in some cases (#20347)

* Recommend `mapIt` in some cases

* Remove runnableExample
This commit is contained in:
Amjad Ben Hedhili 2022-09-19 22:43:46 +01:00 • committed by GitHub
commit 7023176dcb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)