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

Inconsistent for loop semantics #237

Closed
p6rt opened this issue Aug 16, 2008 · 6 comments
Closed

Inconsistent for loop semantics #237

p6rt opened this issue Aug 16, 2008 · 6 comments

Comments

@p6rt
Copy link

p6rt commented Aug 16, 2008

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

Searchable as RT57960$

@p6rt
Copy link
Author

p6rt commented Aug 16, 2008

From @masak

r30258​:
$ ./perl6 -e '.say for [1,2,3]' # as expected
1
2
3

$ ./perl6 -e 'for [1,2,3] { .say }' # shouldn't that be the same?
1 2 3

$ ./perl6 -e 'for [1,2,3] -> $a { say $a }' # or this?
1 2 3

$ ./perl6 -e 'for [1,2,3].list { .say }' # how do I make it...
1 2 3

$ ./perl6 -e 'for @​([1,2,3]) { .say }' # ...loop over the elements?
1 2 3

$ ./perl6 -e 'for [1,2,3].clone { .say }' # aha! um...
1
2
3

All in all, it's difficult to actually loop on the contents of an
array at present. The semantics are not very consistent. I'm hoping
this is a bug.

@p6rt
Copy link
Author

p6rt commented Aug 17, 2008

From @masak

It was revision 28928 that introduced this difference between the for
statement and statement-modifying for. I'm not sure exactly what issue
it intended to fix, but if I undo that revision everything works
according to my (not necessarily correct) expectations.

$ svn log -r28928


r28928 | pmichaud | 2008-07-02 03​:37​:22 +0200 (Wed, 02 Jul 2008) | 4 lines

[rakudo]​:
Make sure the expression in a for statement is interpreted in
list context. cjfields++ for finding and reporting this.


$ svn di -r28927​:28928
Index​: languages/perl6/src/parser/actions.pm

--- languages/perl6/src/parser/actions.pm (revision 28927)
+++ languages/perl6/src/parser/actions.pm (revision 28928)
@​@​ -307,7 +307,7 @​@​
  my $block := $( $<pblock> );
  $block.blocktype('declaration');
  my $past := PAST​::Op.new(
- $( $&lt;EXPR> ),
+ PAST​::Op.new(​:name('list'), $($&lt;EXPR>)),
  $block,
  :pasttype($<sym>),
  :node( $/ )

@p6rt
Copy link
Author

p6rt commented Aug 18, 2008

From @pmichaud

On Fri, Aug 15, 2008 at 11​:15​:46PM -0700, Carl Mäsak wrote​:

r30258​:
$ ./perl6 -e '.say for [1,2,3]' # as expected
1
2
3

This one is actually incorrect in Rakudo -- at least according to
Pugs (and I agree with Pugs here). The correct output is "1 2 3\n".

Rakudo's output is likely incorrect for one or two reasons​:
(1) Rakudo doesn't yet properly parse statement modifiers after
listops (RT #​57352), and (2) I think that the C<for> statement
modifier in Rakudo is not properly placing its argument into
list context.

Until #​57352 is resolved, the better test (which Rakudo also
fails at the moment, because of (2) above) is​:

  $ ./perl6 -e '.say() for [1,2,3]'

$ ./perl6 -e 'for [1,2,3] { .say }' # shouldn't that be the same?
1 2 3

This one is correct (both Rakudo and Pugs).

$ ./perl6 -e 'for [1,2,3] -> $a { say $a }' # or this?
1 2 3

This one is also correct (Rakudo and Pugs).

$ ./perl6 -e 'for [1,2,3].list { .say }' # how do I make it...
1 2 3
$ ./perl6 -e 'for @​([1,2,3]) { .say }' # ...loop over the elements?
1 2 3

The reason that the output is "1 2 3\n" and not "1\n2\n3\n"
is that an array in list context remains a (scalar) array.
Thus the for loop is still iterating over a list of one element.

To loop over the elements it ought to be

  for [1,2,3].values { .say }

Rakudo currently has this wrong also -- I suspect that it's
having trouble with the .values method on arrays.

All in all, it's difficult to actually loop on the contents of an
array at present. The semantics are not very consistent. I'm hoping
this is a bug.

Yes, there are some bugs here -- several of the List methods
(.values, .keys, etc.) need some refactoring in Rakudo since it's
been decided that they're available for Any objects as well.

Pm

@p6rt
Copy link
Author

p6rt commented Aug 18, 2008

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

@p6rt
Copy link
Author

p6rt commented Aug 18, 2008

From @pmichaud

Now resolved in r32094. Some specific notes below​:

On Mon, Aug 18, 2008 at 03​:39​:18AM -0500, Patrick R. Michaud wrote​:

On Fri, Aug 15, 2008 at 11​:15​:46PM -0700, Carl Mäsak wrote​:

r30258​:
$ ./perl6 -e '.say for [1,2,3]' # as expected
1
2
3

This one is actually incorrect in Rakudo -- at least according to
Pugs (and I agree with Pugs here). The correct output is "1 2 3\n".

Rakudo's output is likely incorrect for one or two reasons​:
(1) Rakudo doesn't yet properly parse statement modifiers after
listops (RT #​57352), and (2) I think that the C<for> statement
modifier in Rakudo is not properly placing its argument into
list context.

Oops! C<.say> isn't a listop when it doesn't have a trailing
colon, so that wasn't really an issue here. But #​2 was an
issue, and the C<for> statement modifier in Rakudo is now properly
placing the subsequent expression into list context.

To loop over the elements it ought to be

for \[1,2,3\]\.values \{ \.say \}

This now works also. We still have a fair bit of refactoring
to do for the various list-like methods, but r32094 at least
helps the consistency a bit for this particular bug.

Thanks!

Pm

@p6rt
Copy link
Author

p6rt commented Aug 18, 2008

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

@p6rt p6rt closed this as completed Aug 18, 2008
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

No branches or pull requests

1 participant