sub f()
{
gather
{
take gather { take 3; take 4; take 5; }
take gather { take 1; take 2; take 3; }
}
}
say f.sort
blows with
The iterator of this Seq is already in use/consumed by another Seq
(you might solve this by adding .cache on usages of the Seq, or
by assigning the Seq into an array)
in block <unit> at so.raku line 1
Whereas
sub f()
{
gather
{
take cache gather { take 3; take 4; take 5; }
take cache gather { take 1; take 2; take 3; }
}
}
say f.sort
Works as expected and produces ((1, 2 ,3), (3, 4, 5)). That's fine (thanks raiph). Now
sub MAIN( *@n )
{
@n = @n || ( 3, 4, 5, -1, 111, 27, 28, 29, 1, 2 );
chunks( @n, { $^left < $^right } )
.sort({ [*] |$_ })
.tail
.say;
}
sub chunks( Iterable $list, &condition )
{
my $windows = $list.rotor( 2 => -1, :partial ).iterator;
my $done = False;
gather until $done
{
my Mu $window := $windows.pull-one;
last if $window =:= IterationEnd;
take cache gather
{
my $condition-result;
while $window := $window // $windows.pull-one
{
$done = True and last if $window =:= IterationEnd;
take $window[0];
$condition-result = &condition( |$window )
if $window.elems == 2;
$window := Any;
last unless $condition-result;
}
}
}
}
This should produce (27 28 29), but it doesn't. It produces (111). But when I replace take cache gather by take eager gather it works as expected.
my chunks function has the same structure than the simpler example above, I reckon and yet there seems to be a difference in the behaviour.