use v6;
use Test;
plan 17;
{
my @a;
my $i;
@a := gather {
$i = 1;
for (1 .. 5) -> $j {
take $j;
}
};
ok(!$i, "not yet gathered");
is(+@a, 5, "5 elements gathered");
ok($i, "gather code executed");
is(@a[0], 1, "first elem taken");
is(@a[*-1], 5, "last elem taken");
};
{
my @outer = gather {
for 1..3 -> $i {
my @inner = gather {
take $_ for 1..3;
};
take "$i:" ~ @inner.join(',');
}
};
is ~@outer, "1:1,2,3 2:1,2,3 3:1,2,3", "nested gather works (two levels)";
}
{
my @outer = gather {
for 1..2 -> $i {
my @inner = gather {
for 1..2 -> $j {
my @inner_inner = gather {
take $_ for 1..2;
};
take "$j:" ~ @inner_inner.join(',');
}
};
take "$i:" ~ @inner.join(';');
}
};
is ~@outer, "1:1:1,2;2:1,2 2:1:1,2;2:1,2", "nested gather works (three levels)";
}
{
my @outer = gather {
my @l = (1, 2, 3);
take 5;
take @l;
take 5;
};
is ~@outer, "5 1 2 3 5", "take on lists and multiple takes work";
}
{
my $dynamic_take = sub { take 7 };
my @outer = gather {
$dynamic_take();
take 1;
};
is ~@outer, "7 1", "gather scopes dynamically, not lexically";
}
{
my @list = gather { take [1,2,3]; take [4,5,6];};
my @list2 = ([1,2,3],[4,5,6]);
is @list.perl, @list2.perl , "gather array-refs";
}
{
my @out = gather for 1..5 {
take $_;
};
is ~@out, "1 2 3 4 5", "gather as a statement_prefix";
}
{
my $count = 0;
my @list := gather {
for 1 .. 10 -> $a {
take $a;
$count++
}
};
my $result = @list[2];
is($count, 2, "gather is lazy");
}
{
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";
}
{
my @list = gather {
loop (my $v = 1; $v <= 10; $v++)
{
take $v if $v % 2 == 0;
}
};
is ~@list, "2 4 6 8 10", "gather with nested loop";
}
{
is (gather { take 1, 2, 3; take 4, 5, 6; }).elems, 2,
'take with multiple arguments produces one item each';
is (gather { take 1, 2, 3; take 4, 5, 6; }).flat.elems, 6,
'take with multiple arguments .flat tens out';
}
{
my sub grep-div(@a, $n) {
gather for @a {
take $_ if $_ %% $n;
}
}
my @evens := grep-div((1...*), 2);
is ~grep-div(@evens, 3).munch(16), ~grep-div((1...100), 6), "Nested identical gathers";
}