-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes #7187 sequtils.toSeq produces the sequence from the iterator twice #8586
Conversation
lib/pure/collections/sequtils.nim
Outdated
@@ -497,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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's with the symbol name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
lib/pure/collections/sequtils.nim
Outdated
@@ -25,6 +25,22 @@ import macros | |||
when not defined(nimhygiene): | |||
{.pragma: dirty.} | |||
|
|||
|
|||
macro evalOnce(v, exp: untyped, letAssigneable: static[bool]): untyped = | |||
expectKind(v, nnkIdent) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this do different from a single 'let' statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
3ff4949
to
dd32002
Compare
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s2
is correct here as test case for #8580 since implementation of mapIt
now uses s2, not t.
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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simplifies test case, reusing identity
and counter
, as done in other test cases in this file
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added corner test case similar to the one just above
inc i | ||
result | ||
block: | ||
evalOnceAs(iter2, iter, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #8584 for why we need evalOnceAs
@Varriount PTAL |
@Araq could we please merge this? I have other PR's coming up depending on this |
…rator twice (nim-lang#8586) * cleanups refs nim-lang#8584 * fixes nim-lang#7187
NOTE: this PR depends on #8584 which should be merged first