Skip to content

Commit

Permalink
fixes #7187 sequtils.toSeq produces the sequence from the iterator tw…
Browse files Browse the repository at this point in the history
…ice (#8586)

* cleanups refs #8584
* fixes #7187
  • Loading branch information
timotheecour authored and Araq committed Aug 16, 2018
1 parent 3ca8ebe commit af03754
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 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 Expand Up @@ -1092,12 +1102,18 @@ when isMainModule:
doAssert foo1(openArray[int]([identity(1),identity(2)])) == @[10,20]
doAssert counter == 2

# Corner cases (openArray litterals should not be common)
template foo2(x: openArray[int]): seq[int] = x.mapIt(it * 10)
counter = 0
doAssert foo2(openArray[int]([identity(1),identity(2)])) == @[10,20]
# TODO: this fails; not sure how to fix this case
# doAssert counter == 2

counter = 0
doAssert openArray[int]([identity(1), identity(2)]).mapIt(it) == @[1,2]
# ditto
# doAssert counter == 2

block: # mapIt empty test, see https://github.com/nim-lang/Nim/pull/8584#pullrequestreview-144723468
# NOTE: `[].mapIt(it)` is illegal, just as `let a = @[]` is (lacks type
# of elements)
Expand All @@ -1106,16 +1122,12 @@ when isMainModule:
doAssert newSeq[int](0).mapIt(it) == @[]

block: # mapIt redifinition check, see https://github.com/nim-lang/Nim/issues/8580
let t = [1,2].mapIt(it)
doAssert t == @[1,2]
let s2 = [1,2].mapIt(it)
doAssert s2 == @[1,2]

block:
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
counter = 0
doAssert [1,2].identity().mapIt(it*2).mapIt(it*10) == @[20, 40]
# https://github.com/nim-lang/Nim/issues/7187 test case
doAssert counter == 1

Expand Down

0 comments on commit af03754

Please sign in to comment.