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 9, 2018
1 parent f3cfd04 commit 3ff4949
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,10 @@ template toSeq*(iter: untyped): untyped =
## assert odd_numbers == @[1, 3, 5, 7, 9]

when compiles(iter.len):
evalOnce(t_D20180808T230638, iter, letAssigneable = true)
var i = 0
var result = newSeq[type(iter)](iter.len)
for x in iter:
var result = newSeq[type(iter)](t_D20180808T230638.len)
for x in t_D20180808T230638:
result[i] = x
inc i
result
Expand Down Expand Up @@ -1012,6 +1013,16 @@ when isMainModule:
result = true)
assert odd_numbers == @[1, 3, 5, 7, 9]

block:
# tests https://github.com/nim-lang/Nim/issues/7187
var counter = 0
proc getInput():auto =
counter.inc
@[1, 2, 3]
let ret = toSeq(getInput().filter(proc (x: int): bool = x < 3))
doAssert ret == @[1, 2]
doAssert counter == 1

block: # foldl tests
let
numbers = @[5, 9, 11]
Expand Down Expand Up @@ -1068,13 +1079,13 @@ when isMainModule:
doAssert openArray[int]([1,2]).mapIt(it) == @[1,2]

block:
# tests https://github.com/nim-lang/Nim/issues/7187:
# make sure argument evaluated only once, analog to
var counter = 0
proc getInput():auto =
counter.inc
[1, 2]
doAssert getInput().mapIt(it*2).mapIt(it*10) == @[20, 40]
# make sure argument evaluated only once, analog to
# https://github.com/nim-lang/Nim/issues/7187 test case
doAssert counter == 1

block: # mapIt with invalid RHS for `let` (#8566)
Expand Down

0 comments on commit 3ff4949

Please sign in to comment.