Skip to content
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

Merged
merged 2 commits into from
Aug 16, 2018

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Aug 9, 2018

NOTE: this PR depends on #8584 which should be merged first

@timotheecour timotheecour changed the title Pr fix 7187 to seq twice fixes #7187 sequtils.toSeq produces the sequence from the iterator twice Aug 9, 2018
@@ -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)
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will fix once #8584 is merged (was a workaround for #8580 but since then found a better way via block)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -25,6 +25,22 @@ import macros
when not defined(nimhygiene):
{.pragma: dirty.}


macro evalOnce(v, exp: untyped, letAssigneable: static[bool]): untyped =
expectKind(v, nnkIdent)
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explained in #8580 (this diff will disappear once #8580 is merged)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@timotheecour timotheecour changed the title fixes #7187 sequtils.toSeq produces the sequence from the iterator twice [WIP:pending on #8584] fixes #7187 sequtils.toSeq produces the sequence from the iterator twice Aug 11, 2018
@timotheecour timotheecour force-pushed the pr_fix_7187_toSeq_twice branch from 3ff4949 to dd32002 Compare August 16, 2018 01:06
@@ -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)
Copy link
Member Author

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]
Copy link
Member Author

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
Copy link
Member Author

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)
Copy link
Member Author

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

@timotheecour timotheecour changed the title [WIP:pending on #8584] fixes #7187 sequtils.toSeq produces the sequence from the iterator twice fixes #7187 sequtils.toSeq produces the sequence from the iterator twice Aug 16, 2018
@timotheecour
Copy link
Member Author

@Varriount PTAL

@timotheecour
Copy link
Member Author

@Araq could we please merge this? I have other PR's coming up depending on this

@Araq Araq merged commit af03754 into nim-lang:devel Aug 16, 2018
@timotheecour timotheecour deleted the pr_fix_7187_toSeq_twice branch August 16, 2018 22:06
PMunch pushed a commit to PMunch/Nim that referenced this pull request Aug 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sequtils.toSeq produces the sequence from the iterator twice if compiles(iter.len) == true
3 participants