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

gather+while only takes last value tested #594

Closed
p6rt opened this issue Jan 11, 2009 · 10 comments
Closed

gather+while only takes last value tested #594

p6rt opened this issue Jan 11, 2009 · 10 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Jan 11, 2009

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

Searchable as RT62178$

@p6rt
Copy link
Author

p6rt commented Jan 2, 2009

From mah@everybody.org

I was working on Euler problem 2
(http://projecteuler.net/index.php?section=problems&id=2), trying to set
up a Fibonacci class when I discovered the following strange behavior.
The code to my solution works under Pugs and produces the following
output​:

  2,8,34,144,610,2584,10946,46368,196418,832040,3524578
  4613732

Under rakudo, I get the following output​:

  Use of uninitialized value
  (repeated several times — the warning comes from the first line in the next() method)
  5702887,5702887,5702887,5702887,5702887,5702887,5702887,5702887,5702887,5702887,5702887
  62731757

Here's the source​:

-=-=- cut -=-=-
#!perl6

class Fibonacci {
  has @​!list is rw = (0, 1);

  method next() {
  @​!list[2] = [+] @​!list;
  shift @​!list;
  return @​!list[1];
  }
}

my $fibber = Fibonacci.new;
my $f;

my @​r = gather {
  $f = $fibber.next;
  while($f < 4000000) {
  take $f if $f % 2 == 0;
  $f = $fibber.next;
  }
}
say @​r.join(",");
say [+] @​r;

@p6rt
Copy link
Author

p6rt commented Jan 11, 2009

From jswitzer@gmail.com

When mixing gather with a while loop, only the last element tested by the
while predicate seems to actually be gathered. I created a test to mix
gather and while in S04-statements/gather.t​:

  my @​list = gather {
  my $v = 1;
  while($v <= 10) {
  take $v if $v % 2 == 0;
  $v++;
  }
  };
  is ~@​list, "2 4 6 8 10", "gather with nested while";

At this point, @​list only contains [11, 11, 11, 11, 11]. If the while loop
is replaced with a for loop (C<for(1..10) -> $f>), it properly returns [2,
4, 6, 8, 10].

Tested on​: revision 35352 built on parrot 0.8.2-devel for
x86_64-linux-thread-multi.

Thanks,

Jason "s1n" Switzer

@p6rt
Copy link
Author

p6rt commented Mar 4, 2009

From amahabal@gmail.com

<ecode>

my @​y = gather {
  my $x = 0;
  for 1..5 {
  $x++;
  take $x;
  }
};
say @​y;

</ecode>

produces 55555.
--
-- Abhijit

@p6rt
Copy link
Author

p6rt commented Mar 4, 2009

From @moritz

On Tue Mar 03 23​:09​:55 2009, abhijit wrote​:

<ecode>

my @​y = gather {
my $x = 0;
for 1..5 {
$x++;
take $x;
}
};
say @​y;

</ecode>

Thanks for your report.
We already have a test for that in t/spec/S04-statements/gather.t (the
last test in there).

Cheers,
Moritz

@p6rt
Copy link
Author

p6rt commented Mar 4, 2009

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

@p6rt
Copy link
Author

p6rt commented Mar 7, 2009

From @bacek

On Sat Jan 10 17​:30​:23 2009, s1n wrote​:

When mixing gather with a while loop, only the last element tested by the
while predicate seems to actually be gathered. I created a test to mix
gather and while in S04-statements/gather.t​:

Patch to clone argument of "take" available at

http://github.com/bacek/rakudo/commit/a87de35bf576ac612f97b6d01fbcc7b7a8ee72cf

--
Bacek

@p6rt
Copy link
Author

p6rt commented Mar 7, 2009

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

@p6rt
Copy link
Author

p6rt commented Aug 8, 2009

From @colomon

rakudo​: my @​a = gather loop (my $i = 1; $i < 10; $i++) { take $i }; @​a.perl.say;
p6eval​: rakudo 7717c4​: OUTPUT«[10, 10, 10, 10, 10, 10, 10, 10, 10]␤»

--
Solomon Foster​: colomon@​gmail.com
HarmonyWare, Inc​: http://www.harmonyware.com

@p6rt
Copy link
Author

p6rt commented Jun 26, 2010

From @moritz

Miraculously the tests now pass, so I'm closing this ticket. Feel free
to open again if it resurfaces again.

@p6rt
Copy link
Author

p6rt commented Jun 26, 2010

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

@p6rt p6rt closed this as completed Jun 26, 2010
@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