Is there a way to make the collect macro to create seq by default, without specifying it explicitly as collect(newSet):?
I tried simple code, but it won't work:
import sets, sugar
let data = @["bird", "word"]
template collect(body) =
collect(newSeq, body)
discard collect:
for i, d in data.pairs:
if i mod 2 == 0: d
P.S.
I noticed some problems with the collect macro, maybe they already marked as problems in issues.
import sets, sugar
let data = @["bird", "word"]
echo collect(newSeq):
for i, d in data.pairs:
if i mod 2 == 0: d
import sets, sugar
let data = @["bird", "word"]
discard newSeq.collect:
for i, d in data.pairs:
if i mod 2 == 0: d
discard initHashSet.collect:
for d in data.items: {d}
This works fine:
import sugar
let data = @["bird", "word"]
echo (collect(newSeq,
for i, d in data.pairs:
if i mod 2 == 0: d))
I believe collect in the development branch can infer the desired collection type
Nice!
the collect without newSeq (and newSeq.collect) work in devel, not 1.4.2 as does:
discard newSeq.collect:
for i,d in data.pairs:
if i mod 2 == 0: d
echo newSeq.collect(
for i,d in data.pairs:
if i mod 2 == 0: d)
echo (block:newSeq.collect:
for i, d in data.pairs:
if i mod 2 == 0: d)
wrapping collect with (block: has been the most reliable way to get it to work with corner cases or complex loop bodies, but it's been getting much better. my pet peeve is that collect(newString) doesn't work. i'll have to make a PR