Skip to content

Commit

Permalink
fix #7187 toSeq twice; adds test case
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Aug 16, 2018
1 parent a8a6191 commit dd32002
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,17 @@ template toSeq*(iter: untyped): untyped =
## result = true)
## assert odd_numbers == @[1, 3, 5, 7, 9]

# Note: see also `mapIt` for explanation of some of the implementation
# subtleties.
when compiles(iter.len):
var i = 0
var result = newSeq[type(iter)](iter.len)
for x in iter:
result[i] = x
inc i
result
block:
evalOnceAs(iter2, iter, true)
var result = newSeq[type(iter)](iter2.len)
var i = 0
for x in iter2:
result[i] = x
inc i
result
else:
var result: seq[type(iter)] = @[]
for x in iter:
Expand Down Expand Up @@ -1031,6 +1035,12 @@ when isMainModule:
result = true)
assert odd_numbers == @[1, 3, 5, 7, 9]

block:
# tests https://github.com/nim-lang/Nim/issues/7187
counter = 0
let ret = toSeq(@[1, 2, 3].identity().filter(proc (x: int): bool = x < 3))
doAssert ret == @[1, 2]
doAssert counter == 1
block: # foldl tests
let
numbers = @[5, 9, 11]
Expand Down

0 comments on commit dd32002

Please sign in to comment.