-
Notifications
You must be signed in to change notification settings - Fork 135
/
gather.t
198 lines (166 loc) · 3.74 KB
/
gather.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use v6;
use Test;
plan 21;
# L<S04/The C<gather> statement prefix/>
# Standard gather
{
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");
};
# Nested gathers, two levels
{
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)";
}
# Nested gathers, three 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)";
}
# take on lists, multiple takes per loop
{
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";
}
# gather scopes dynamiclly, not lexically
{
my $dynamic_take = sub { take 7 };
my @outer = gather {
$dynamic_take();
take 1;
};
is ~@outer, "7 1", "gather scopes dynamically, not lexically";
}
# take on array-ref
{
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";
}
# gather statement prefix
{
my @out = gather for 1..5 {
take $_;
};
is ~@out, "1 2 3 4 5", "gather as a statement_prefix";
}
# lazy gather
{
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';
}
#?rakudo skip 'nom regression (loops)'
{
my sub grep-div(@a, $n) {
gather for @a {
take $_ if $_ %% $n;
}
}
my @evens := grep-div((1...*), 2);
is ~grep-div(@evens, 3)[^16], ~grep-div((1...100), 6), "Nested identical gathers";
}
# RT #77036
{
class E {
has $.n is rw;
has $.v;
method Str() {~self.v }
};
my E $x .= new(:v(1));
$x.n = E.new(:v(2));
is (gather { my $i = $x; while $i.defined { take $i; $i = $i.n } }).join("|"), '1|2', 'Elements in gather/take stringify correctly';
}
# RT #78026, RT #77302
{
sub foo {
my @a = (1,2,3,4,5);
gather {
my $val ;
while @a {
$val = @a.shift();
take $val;
}
}
};
is foo().join, '12345', 'decontainerization happens (1)';
is (<a b c d e> Zxx 0,1,0,1,0).Str, 'b d',
'decontainerization happens (2)';
}
# Method form of take
{
my @outer = gather {
my @l = (1, 2, 3);
5.take;
@l.take;
5.take;
};
is ~@outer, "5 1 2 3 5", "method form of take works.";
}
# vim: ft=perl6