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

Constant Seq in a loop not reinitialized #5186

Closed
p6rt opened this issue Mar 20, 2016 · 5 comments
Closed

Constant Seq in a loop not reinitialized #5186

p6rt opened this issue Mar 20, 2016 · 5 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Mar 20, 2016

Migrated from rt.perl.org#127749 (status was 'resolved')

Searchable as RT127749$

@p6rt
Copy link
Author

p6rt commented Mar 20, 2016

From @awwaiid

Here is an example that works as expected​:

  > for ^2 { my $n = 5; say [+] (^$n X ^$n) }
  50
  50

If we use constants to construct the Seq, it is not re-created on the
second loop iteration​:

  > for ^2 { say [+] (^5 X ^5) }
  50
  This Seq has already been iterated, and its values consumed
  (you might solve this by adding .cache on usages of the Seq, or
  by assigning the Seq into an array)
  in block <unit> at <unknown file> line 1

In my particular case after building the Seq I was applying map and grep to
it which would change on each iteration -- it yells at me for the
constant-Seq re-use even though I am doing different things with it each
time.

  > for ^2 -> $n { say [+] (^5 X ^5).map(* + $n) }
  50
  This Seq has already been iterated, and its values consumed
  (you might solve this by adding .cache on usages of the Seq, or
  by assigning the Seq into an array)
  in block <unit> at <unknown file> line 1

I don't feel that the suggested .cache is appropriate, but maybe I'm wrong?
Seems like if I wanted this Seq to be shared I should have to define it
before the block.

Note that if I manually unrolled this loop I wouldn't get the error (since
the Seq would be written out twice, so not considered the same Seq).

@p6rt
Copy link
Author

p6rt commented Jul 21, 2016

From @zoffixznet

Still present in Rakudo version 2016.07.1-19-g8dff9df built on MoarVM version 2016.07-3-gc01472d

$ ./perl6-m -e 'for ^2 { say [+] (^5 X ^5) }'
50
This Seq has already been iterated, and its values consumed
(you might solve this by adding .cache on usages of the Seq, or
by assigning the Seq into an array)
  in block <unit> at -e line 1

--
Cheers,
ZZ | https://twitter.com/zoffix

@p6rt
Copy link
Author

p6rt commented Jul 21, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Jul 21, 2016

From @jnthn

On Sun Mar 20 08​:30​:28 2016, awwaiid wrote​:

Here is an example that works as expected​:

> for ^2 \{ my $n = 5; say \[\+\] \(^$n X ^$n\) \}
50
50

If we use constants to construct the Seq, it is not re-created on the
second loop iteration​:

> for ^2 \{ say \[\+\] \(^5 X ^5\) \}
50
This Seq has already been iterated, and its values consumed
\(you might solve this by adding \.cache on usages of the Seq, or
by assigning the Seq into an array\)
  in block \<unit> at \<unknown file> line 1

In my particular case after building the Seq I was applying map and grep to
it which would change on each iteration -- it yells at me for the
constant-Seq re-use even though I am doing different things with it each
time.

Yeah, it was a constant folding issue. I've now made sure we don't try to do that for things that produce a Seq, which fixes the issue. Test added to S03-metaops/cross.t.

@p6rt
Copy link
Author

p6rt commented Jul 21, 2016

@jnthn - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Jul 21, 2016
@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant