line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Iterable; |
2
|
|
|
|
|
|
|
|
3
|
65
|
|
|
65
|
|
150850
|
use Catmandu::Sane; |
|
65
|
|
|
|
|
191
|
|
|
65
|
|
|
|
|
525
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
65
|
|
|
|
|
5509
|
use Catmandu::Util qw( |
8
|
|
|
|
|
|
|
is_number |
9
|
|
|
|
|
|
|
is_value |
10
|
|
|
|
|
|
|
is_string |
11
|
|
|
|
|
|
|
is_array_ref |
12
|
|
|
|
|
|
|
is_hash_ref |
13
|
|
|
|
|
|
|
is_regex_ref |
14
|
|
|
|
|
|
|
is_same |
15
|
|
|
|
|
|
|
check_positive |
16
|
65
|
|
|
65
|
|
537
|
); |
|
65
|
|
|
|
|
158
|
|
17
|
65
|
|
|
65
|
|
505
|
use Time::HiRes qw(gettimeofday tv_interval); |
|
65
|
|
|
|
|
155
|
|
|
65
|
|
|
|
|
736
|
|
18
|
65
|
|
|
65
|
|
44977
|
use Hash::Util::FieldHash qw(fieldhash); |
|
65
|
|
|
|
|
61210
|
|
|
65
|
|
|
|
|
3984
|
|
19
|
65
|
|
|
65
|
|
500
|
use Role::Tiny; |
|
65
|
|
|
|
|
161
|
|
|
65
|
|
|
|
|
557
|
|
20
|
65
|
|
|
65
|
|
15090
|
use namespace::clean; |
|
65
|
|
|
|
|
178
|
|
|
65
|
|
|
|
|
444
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# delay loading these because of circular dependency |
23
|
|
|
|
|
|
|
require Catmandu::Iterator; |
24
|
|
|
|
|
|
|
require Catmandu::ArrayIterator; |
25
|
|
|
|
|
|
|
require Catmandu; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
requires 'generator'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
{ |
30
|
|
|
|
|
|
|
# can't use Moo attribute because of circular dependency |
31
|
|
|
|
|
|
|
fieldhash my %_generators; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub next { |
34
|
9
|
|
|
9
|
1
|
2280
|
my ($self) = @_; |
35
|
9
|
|
66
|
|
|
75
|
($_generators{$self} ||= $self->generator)->(); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub rewind { |
39
|
4
|
|
|
4
|
1
|
557
|
my ($self) = @_; |
40
|
4
|
|
|
|
|
13
|
$_generators{$self} = $self->generator; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub to_array { |
45
|
88
|
|
|
88
|
1
|
11434
|
my ($self) = @_; |
46
|
88
|
|
|
|
|
961
|
my $next = $self->generator; |
47
|
88
|
|
|
|
|
255
|
my @a; |
48
|
|
|
|
|
|
|
my $data; |
49
|
88
|
|
|
|
|
217
|
while (defined($data = $next->())) { |
50
|
914
|
|
|
|
|
2708
|
push @a, $data; |
51
|
|
|
|
|
|
|
} |
52
|
88
|
|
|
|
|
1806
|
\@a; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub count { |
56
|
15
|
|
|
15
|
1
|
583
|
my ($self) = @_; |
57
|
15
|
|
|
|
|
68
|
my $next = $self->generator; |
58
|
15
|
|
|
|
|
59
|
my $n = 0; |
59
|
15
|
|
|
|
|
42
|
while ($next->()) { |
60
|
1026
|
|
|
|
|
2146
|
$n++; |
61
|
|
|
|
|
|
|
} |
62
|
15
|
|
|
|
|
187
|
$n; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub add_to { |
66
|
1
|
|
|
1
|
1
|
448
|
my ($self, $exporter) = @_; |
67
|
1
|
|
|
|
|
5
|
$exporter->add_many($self); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub slice { |
71
|
6
|
|
|
6
|
1
|
36
|
my ($self, $start, $total) = @_; |
72
|
6
|
|
100
|
|
|
22
|
$start //= 0; |
73
|
|
|
|
|
|
|
Catmandu::Iterator->new( |
74
|
|
|
|
|
|
|
sub { |
75
|
|
|
|
|
|
|
sub { |
76
|
13
|
100
|
|
|
|
35
|
if (defined $total) { |
77
|
9
|
100
|
|
|
|
32
|
$total || return; |
78
|
|
|
|
|
|
|
} |
79
|
9
|
|
|
|
|
53
|
state $next = $self->generator; |
80
|
9
|
|
|
|
|
38
|
state $data; |
81
|
9
|
|
|
|
|
22
|
while (defined($data = $next->())) { |
82
|
12
|
100
|
|
|
|
54
|
if ($start > 0) { |
83
|
4
|
|
|
|
|
7
|
$start--; |
84
|
4
|
|
|
|
|
11
|
next; |
85
|
|
|
|
|
|
|
} |
86
|
8
|
100
|
|
|
|
20
|
if (defined $total) { |
87
|
5
|
|
|
|
|
9
|
$total--; |
88
|
|
|
|
|
|
|
} |
89
|
8
|
|
|
|
|
36
|
return $data; |
90
|
|
|
|
|
|
|
} |
91
|
1
|
|
|
|
|
9
|
return; |
92
|
|
|
|
|
|
|
} |
93
|
6
|
|
|
6
|
|
30
|
} |
94
|
6
|
|
|
|
|
59
|
); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub each { |
98
|
5
|
|
|
5
|
1
|
84
|
my ($self, $sub) = @_; |
99
|
5
|
|
|
|
|
82
|
my $next = $self->generator; |
100
|
5
|
|
|
|
|
19
|
my $n = 0; |
101
|
5
|
|
|
|
|
10
|
my $data; |
102
|
5
|
|
|
|
|
16
|
while (defined($data = $next->())) { |
103
|
9
|
|
|
|
|
41
|
$sub->($data); |
104
|
8
|
|
|
|
|
20
|
$n++; |
105
|
|
|
|
|
|
|
} |
106
|
4
|
|
|
|
|
28
|
$n; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub each_until { |
110
|
1
|
|
|
1
|
0
|
5
|
my ($self, $sub) = @_; |
111
|
1
|
|
|
|
|
3
|
my $next = $self->generator; |
112
|
1
|
|
|
|
|
3
|
my $n = 0; |
113
|
1
|
|
|
|
|
1
|
my $data; |
114
|
1
|
|
|
|
|
16
|
while (defined($data = $next->())) { |
115
|
2
|
100
|
|
|
|
7
|
$sub->($data) || last; |
116
|
1
|
|
|
|
|
654
|
$n++; |
117
|
|
|
|
|
|
|
} |
118
|
1
|
|
|
|
|
652
|
$n; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub tap { |
122
|
10
|
|
|
10
|
1
|
816
|
my ($self, $sub) = @_; |
123
|
|
|
|
|
|
|
Catmandu::Iterator->new( |
124
|
|
|
|
|
|
|
sub { |
125
|
|
|
|
|
|
|
sub { |
126
|
30
|
|
|
|
|
103
|
state $next = $self->generator; |
127
|
30
|
|
|
|
|
78
|
state $data; |
128
|
30
|
100
|
|
|
|
75
|
if (defined($data = $next->())) { |
129
|
20
|
|
|
|
|
94
|
$sub->($data); |
130
|
20
|
|
|
|
|
82
|
return $data; |
131
|
|
|
|
|
|
|
} |
132
|
10
|
|
|
|
|
60
|
return; |
133
|
|
|
|
|
|
|
} |
134
|
10
|
|
|
10
|
|
67
|
} |
135
|
10
|
|
|
|
|
108
|
); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub every { |
139
|
3
|
|
|
3
|
1
|
918
|
my ($self, $n, $sub) = @_; |
140
|
3
|
|
|
|
|
14
|
check_positive($n); |
141
|
|
|
|
|
|
|
Catmandu::Iterator->new( |
142
|
|
|
|
|
|
|
sub { |
143
|
|
|
|
|
|
|
sub { |
144
|
22
|
|
|
|
|
32
|
state $next = $self->generator; |
145
|
22
|
|
|
|
|
41
|
state $data; |
146
|
22
|
|
|
|
|
27
|
state $i = 0; |
147
|
22
|
100
|
|
|
|
35
|
if (defined($data = $next->())) { |
148
|
20
|
100
|
|
|
|
78
|
if (++$i == $n) { |
149
|
8
|
|
|
|
|
19
|
$sub->($data); |
150
|
8
|
|
|
|
|
20
|
$i = 0; |
151
|
|
|
|
|
|
|
} |
152
|
20
|
|
|
|
|
38
|
return $data; |
153
|
|
|
|
|
|
|
} |
154
|
2
|
|
|
|
|
9
|
return; |
155
|
|
|
|
|
|
|
} |
156
|
2
|
|
|
2
|
|
11
|
} |
157
|
2
|
|
|
|
|
21
|
); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub any { |
161
|
8
|
|
|
8
|
1
|
20
|
my ($self, $sub) = @_; |
162
|
8
|
|
|
|
|
27
|
my $next = $self->generator; |
163
|
8
|
|
|
|
|
57
|
my $data; |
164
|
8
|
|
|
|
|
19
|
while (defined($data = $next->())) { |
165
|
21
|
100
|
|
|
|
572
|
$sub->($data) && return 1; |
166
|
|
|
|
|
|
|
} |
167
|
4
|
|
|
|
|
285
|
return 0; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub many { |
171
|
2
|
|
|
2
|
1
|
10
|
my ($self, $sub) = @_; |
172
|
2
|
|
|
|
|
11
|
my $next = $self->generator; |
173
|
2
|
|
|
|
|
19
|
my $n = 0; |
174
|
2
|
|
|
|
|
4
|
my $data; |
175
|
2
|
|
|
|
|
8
|
while (defined($data = $next->())) { |
176
|
5
|
100
|
100
|
|
|
44
|
$sub->($data) && ++$n > 1 && return 1; |
177
|
|
|
|
|
|
|
} |
178
|
1
|
|
|
|
|
12
|
return 0; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub all { |
182
|
3
|
|
|
3
|
1
|
16
|
my ($self, $sub) = @_; |
183
|
3
|
|
|
|
|
10
|
my $next = $self->generator; |
184
|
3
|
|
|
|
|
26
|
my $data; |
185
|
3
|
|
|
|
|
10
|
while (defined($data = $next->())) { |
186
|
5
|
100
|
|
|
|
41
|
$sub->($data) || return 0; |
187
|
|
|
|
|
|
|
} |
188
|
2
|
|
|
|
|
45
|
return 1; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub map { |
192
|
28
|
|
|
28
|
1
|
109
|
my ($self, $sub) = @_; |
193
|
|
|
|
|
|
|
Catmandu::Iterator->new( |
194
|
|
|
|
|
|
|
sub { |
195
|
|
|
|
|
|
|
sub { |
196
|
807
|
|
|
|
|
1530
|
state $next = $self->generator; |
197
|
807
|
|
|
|
|
1188
|
state @buff; |
198
|
807
|
100
|
100
|
|
|
2449
|
@buff = $sub->($next->() // return) unless @buff; |
199
|
781
|
|
|
|
|
2749
|
shift @buff; |
200
|
|
|
|
|
|
|
} |
201
|
27
|
|
|
27
|
|
152
|
} |
202
|
28
|
|
|
|
|
264
|
); |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub fix { |
206
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
207
|
1
|
|
|
|
|
10
|
my $fixer = Catmandu->fixer(@_); |
208
|
1
|
|
|
3
|
|
1782
|
$self->map(sub {$fixer->fix(shift)}); |
|
3
|
|
|
|
|
34
|
|
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub reduce { |
212
|
26
|
|
|
26
|
1
|
157
|
my $self = shift; |
213
|
26
|
|
|
|
|
58
|
my $memo_set = @_ > 1; |
214
|
26
|
|
|
|
|
48
|
my $sub = pop; |
215
|
26
|
|
|
|
|
39
|
my $memo = shift; |
216
|
26
|
|
|
|
|
273
|
my $next = $self->generator; |
217
|
26
|
|
|
|
|
145
|
my $data; |
218
|
26
|
|
|
|
|
69
|
while (defined($data = $next->())) { |
219
|
95
|
100
|
|
|
|
291
|
if ($memo_set) { |
220
|
94
|
|
|
|
|
232
|
$memo = $sub->($memo, $data); |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
else { |
223
|
1
|
|
|
|
|
2
|
$memo = $data; |
224
|
1
|
|
|
|
|
3
|
$memo_set = 1; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
} |
227
|
26
|
|
|
|
|
605
|
$memo; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub first { |
231
|
12
|
|
|
12
|
1
|
195
|
$_[0]->generator->(); |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sub rest { |
235
|
2
|
|
|
2
|
1
|
819
|
$_[0]->slice(1); |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub take { |
239
|
2
|
|
|
2
|
1
|
7
|
$_[0]->slice(0, $_[1]); |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
{ |
243
|
|
|
|
|
|
|
my $to_sub = sub { |
244
|
|
|
|
|
|
|
my ($arg1, $arg2) = @_; |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
if (is_string($arg1)) { |
247
|
|
|
|
|
|
|
if (is_regex_ref($arg2)) { |
248
|
|
|
|
|
|
|
return sub { |
249
|
|
|
|
|
|
|
is_hash_ref($_[0]) || return 0; |
250
|
|
|
|
|
|
|
my $val = $_[0]->{$arg1}; |
251
|
|
|
|
|
|
|
is_value($val) && $val =~ $arg2; |
252
|
|
|
|
|
|
|
}; |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
if (is_array_ref($arg2)) { |
255
|
|
|
|
|
|
|
return sub { |
256
|
|
|
|
|
|
|
is_hash_ref($_[0]) || return 0; |
257
|
|
|
|
|
|
|
is_value(my $val = $_[0]->{$arg1}) || return 0; |
258
|
|
|
|
|
|
|
for my $v (@$arg2) { |
259
|
|
|
|
|
|
|
return 1 if $val eq $v; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
0; |
262
|
|
|
|
|
|
|
}; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
return sub { |
265
|
|
|
|
|
|
|
is_hash_ref($_[0]) || return 0; |
266
|
|
|
|
|
|
|
my $val = $_[0]->{$arg1}; |
267
|
|
|
|
|
|
|
is_value($val) && $val eq $arg2; |
268
|
|
|
|
|
|
|
}; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
if (is_regex_ref($arg1)) { |
272
|
|
|
|
|
|
|
return sub { |
273
|
|
|
|
|
|
|
my $val = $_[0]; |
274
|
|
|
|
|
|
|
is_value($val) && $val =~ $arg1; |
275
|
|
|
|
|
|
|
}; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
$arg1; |
279
|
|
|
|
|
|
|
}; |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
sub detect { |
282
|
4
|
|
|
4
|
1
|
10
|
my $self = shift; |
283
|
4
|
|
|
|
|
12
|
my $sub = $to_sub->(@_); |
284
|
4
|
|
|
|
|
12
|
my $next = $self->generator; |
285
|
4
|
|
|
|
|
36
|
my $data; |
286
|
4
|
|
|
|
|
9
|
while (defined($data = $next->())) { |
287
|
8
|
100
|
|
|
|
48
|
$sub->($data) && return $data; |
288
|
|
|
|
|
|
|
} |
289
|
1
|
|
|
|
|
12
|
return; |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
sub select { |
293
|
10
|
|
|
10
|
1
|
29
|
my $self = shift; |
294
|
10
|
|
|
|
|
29
|
my $sub = $to_sub->(@_); |
295
|
|
|
|
|
|
|
Catmandu::Iterator->new( |
296
|
|
|
|
|
|
|
sub { |
297
|
|
|
|
|
|
|
sub { |
298
|
27
|
|
|
|
|
82
|
state $next = $self->generator; |
299
|
27
|
|
|
|
|
89
|
state $data; |
300
|
27
|
|
|
|
|
54
|
while (defined($data = $next->())) { |
301
|
31
|
100
|
|
|
|
153
|
$sub->($data) && return $data; |
302
|
|
|
|
|
|
|
} |
303
|
10
|
|
|
|
|
53
|
return; |
304
|
|
|
|
|
|
|
} |
305
|
10
|
|
|
10
|
|
42
|
} |
306
|
10
|
|
|
|
|
67
|
); |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
|
309
|
4
|
|
|
4
|
1
|
15
|
sub grep {goto &select} |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
sub reject { |
312
|
13
|
|
|
13
|
1
|
38
|
my $self = shift; |
313
|
13
|
|
|
|
|
52
|
my $sub = $to_sub->(@_); |
314
|
|
|
|
|
|
|
Catmandu::Iterator->new( |
315
|
|
|
|
|
|
|
sub { |
316
|
|
|
|
|
|
|
sub { |
317
|
36
|
|
|
|
|
93
|
state $next = $self->generator; |
318
|
36
|
|
|
|
|
90
|
state $data; |
319
|
36
|
|
|
|
|
120
|
while (defined($data = $next->())) { |
320
|
35
|
100
|
|
|
|
124
|
$sub->($data) || return $data; |
321
|
|
|
|
|
|
|
} |
322
|
11
|
|
|
|
|
54
|
return; |
323
|
|
|
|
|
|
|
} |
324
|
12
|
|
|
12
|
|
293
|
} |
325
|
13
|
|
|
|
|
86
|
); |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
}; |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
sub sorted { |
330
|
3
|
|
|
3
|
1
|
10
|
my ($self, $cmp) = @_; |
331
|
3
|
100
|
|
|
|
16
|
if (!defined $cmp) { |
|
|
100
|
|
|
|
|
|
332
|
1
|
|
|
|
|
3
|
Catmandu::ArrayIterator->new([sort @{$self->to_array}]); |
|
1
|
|
|
|
|
3
|
|
333
|
|
|
|
|
|
|
} |
334
|
|
|
|
|
|
|
elsif (ref $cmp) { |
335
|
|
|
|
|
|
|
Catmandu::ArrayIterator->new( |
336
|
1
|
|
|
|
|
4
|
[sort {$cmp->($a, $b)} @{$self->to_array}]); |
|
3
|
|
|
|
|
20
|
|
|
1
|
|
|
|
|
5
|
|
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
else { |
339
|
|
|
|
|
|
|
# TODO: use Schwartzian transform for more complex key |
340
|
|
|
|
|
|
|
Catmandu::ArrayIterator->new( |
341
|
1
|
|
|
|
|
3
|
[sort {$a->{$cmp} cmp $b->{$cmp}} @{$self->to_array}]); |
|
2
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
3
|
|
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
sub pluck { |
346
|
2
|
|
|
2
|
1
|
22
|
my ($self, $key) = @_; |
347
|
|
|
|
|
|
|
$self->map( |
348
|
|
|
|
|
|
|
sub { |
349
|
6
|
|
|
6
|
|
41
|
$_[0]->{$key}; |
350
|
|
|
|
|
|
|
} |
351
|
2
|
|
|
|
|
15
|
); |
352
|
|
|
|
|
|
|
} |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
sub invoke { |
355
|
5
|
|
|
5
|
1
|
76
|
my ($self, $method, @args) = @_; |
356
|
|
|
|
|
|
|
$self->map( |
357
|
|
|
|
|
|
|
sub { |
358
|
16
|
|
|
16
|
|
783
|
$_[0]->$method(@args); |
359
|
|
|
|
|
|
|
} |
360
|
5
|
|
|
|
|
41
|
); |
361
|
|
|
|
|
|
|
} |
362
|
|
|
|
|
|
|
|
363
|
4
|
|
|
4
|
1
|
19
|
sub contains {goto &includes} |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
sub includes { |
366
|
6
|
|
|
6
|
1
|
15
|
my ($self, $data) = @_; |
367
|
|
|
|
|
|
|
$self->any( |
368
|
|
|
|
|
|
|
sub { |
369
|
17
|
|
|
17
|
|
43
|
is_same($data, $_[0]); |
370
|
|
|
|
|
|
|
} |
371
|
6
|
|
|
|
|
34
|
); |
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
sub group { |
375
|
3
|
|
|
3
|
1
|
8
|
my ($self, $size) = @_; |
376
|
|
|
|
|
|
|
Catmandu::Iterator->new( |
377
|
|
|
|
|
|
|
sub { |
378
|
|
|
|
|
|
|
sub { |
379
|
17
|
|
|
|
|
27
|
state $next = $self->generator; |
380
|
|
|
|
|
|
|
|
381
|
17
|
|
|
|
|
48
|
my $group = []; |
382
|
|
|
|
|
|
|
|
383
|
17
|
|
|
|
|
36
|
for (my $i = 0; $i < $size; $i++) { |
384
|
24
|
|
100
|
|
|
68
|
push @$group, $next->() // last; |
385
|
|
|
|
|
|
|
} |
386
|
|
|
|
|
|
|
|
387
|
17
|
100
|
|
|
|
90
|
unless (@$group) { |
388
|
3
|
|
|
|
|
10
|
return; |
389
|
|
|
|
|
|
|
} |
390
|
|
|
|
|
|
|
|
391
|
14
|
|
|
|
|
37
|
Catmandu::ArrayIterator->new($group); |
392
|
|
|
|
|
|
|
} |
393
|
3
|
|
|
3
|
|
12
|
} |
394
|
3
|
|
|
|
|
20
|
); |
395
|
|
|
|
|
|
|
} |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
sub interleave { |
398
|
4
|
|
|
4
|
1
|
75
|
my @iterators = @_; |
399
|
|
|
|
|
|
|
Catmandu::Iterator->new( |
400
|
|
|
|
|
|
|
sub { |
401
|
|
|
|
|
|
|
sub { |
402
|
26
|
|
|
|
|
32
|
state @generators; |
403
|
26
|
|
|
|
|
40
|
state $n = @iterators; |
404
|
26
|
|
|
|
|
32
|
state $i = 0; |
405
|
26
|
|
|
|
|
42
|
while ($n) { |
406
|
30
|
100
|
|
|
|
57
|
$i = 0 if $i == $n; |
407
|
30
|
|
66
|
|
|
65
|
my $next = $generators[$i] ||= $iterators[$i]->generator; |
408
|
30
|
100
|
|
|
|
109
|
if (defined(my $data = $next->())) { |
409
|
22
|
|
|
|
|
76
|
$i++; |
410
|
22
|
|
|
|
|
47
|
return $data; |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
else { |
413
|
8
|
|
|
|
|
41
|
splice @generators, $i, 1; |
414
|
8
|
|
|
|
|
26
|
$n--; |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
} |
417
|
4
|
|
|
|
|
8
|
return; |
418
|
|
|
|
|
|
|
} |
419
|
4
|
|
|
4
|
|
16
|
} |
420
|
4
|
|
|
|
|
27
|
); |
421
|
|
|
|
|
|
|
} |
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
sub max { |
424
|
5
|
|
|
5
|
1
|
15
|
my ($self, $sub) = @_; |
425
|
|
|
|
|
|
|
$self->reduce( |
426
|
|
|
|
|
|
|
undef, |
427
|
|
|
|
|
|
|
sub { |
428
|
12
|
|
|
12
|
|
24
|
my ($memo, $data) = @_; |
429
|
12
|
100
|
|
|
|
27
|
my $val = defined $sub ? $sub->($data) : $data; |
430
|
12
|
50
|
100
|
|
|
60
|
return $val > $memo ? $val : $memo |
|
|
100
|
|
|
|
|
|
431
|
|
|
|
|
|
|
if is_number($memo) && is_number($val); |
432
|
7
|
100
|
|
|
|
18
|
return $memo if is_number($memo); |
433
|
5
|
100
|
|
|
|
19
|
return $val if is_number($val); |
434
|
3
|
|
|
|
|
7
|
return; |
435
|
|
|
|
|
|
|
} |
436
|
5
|
|
|
|
|
30
|
); |
437
|
|
|
|
|
|
|
} |
438
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
sub min { |
440
|
5
|
|
|
5
|
1
|
15
|
my ($self, $sub) = @_; |
441
|
|
|
|
|
|
|
$_[0]->reduce( |
442
|
|
|
|
|
|
|
undef, |
443
|
|
|
|
|
|
|
sub { |
444
|
12
|
|
|
12
|
|
24
|
my ($memo, $data) = @_; |
445
|
12
|
100
|
|
|
|
27
|
my $val = defined $sub ? $sub->($data) : $data; |
446
|
12
|
50
|
100
|
|
|
81
|
return $val < $memo ? $val : $memo |
|
|
100
|
|
|
|
|
|
447
|
|
|
|
|
|
|
if is_number($memo) && is_number($val); |
448
|
7
|
100
|
|
|
|
21
|
return $memo if is_number($memo); |
449
|
5
|
100
|
|
|
|
17
|
return $val if is_number($val); |
450
|
3
|
|
|
|
|
9
|
return; |
451
|
|
|
|
|
|
|
} |
452
|
5
|
|
|
|
|
36
|
); |
453
|
|
|
|
|
|
|
} |
454
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
sub benchmark { |
456
|
6
|
|
|
6
|
1
|
15
|
my ($self) = @_; |
457
|
|
|
|
|
|
|
$self->tap( |
458
|
|
|
|
|
|
|
sub { |
459
|
14
|
|
|
14
|
|
26
|
state $n = 0; |
460
|
14
|
|
|
|
|
58
|
state $t = [gettimeofday]; |
461
|
14
|
50
|
|
|
|
69
|
if (++$n % 100 == 0) { |
462
|
0
|
|
|
|
|
0
|
printf STDERR "added %9d (%d/sec)\n", $n, |
463
|
|
|
|
|
|
|
$n / tv_interval($t); |
464
|
|
|
|
|
|
|
} |
465
|
|
|
|
|
|
|
} |
466
|
6
|
|
|
|
|
50
|
); |
467
|
|
|
|
|
|
|
} |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
sub format { |
470
|
6
|
|
|
6
|
1
|
29
|
my ($self, %opts) = @_; |
471
|
6
|
|
50
|
|
|
54
|
$opts{header} //= 1; |
472
|
6
|
|
50
|
|
|
40
|
$opts{col_sep} //= " | "; |
473
|
6
|
50
|
|
|
|
22
|
my @cols = $opts{cols} ? @{$opts{cols}} : (); |
|
6
|
|
|
|
|
24
|
|
474
|
6
|
|
|
|
|
30
|
my @col_lengths = map length, @cols; |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
my $rows = $self->map( |
477
|
|
|
|
|
|
|
sub { |
478
|
717
|
|
|
717
|
|
17885
|
my $data = $_[0]; |
479
|
717
|
|
|
|
|
1590
|
my $row = []; |
480
|
717
|
|
|
|
|
2198
|
for (my $i = 0; $i < @cols; $i++) { |
481
|
2151
|
|
100
|
|
|
6006
|
my $col = $data->{$cols[$i]} // ""; |
482
|
2151
|
|
|
|
|
3624
|
my $len = length $col; |
483
|
2151
|
100
|
|
|
|
4020
|
$col_lengths[$i] = $len if $len > $col_lengths[$i]; |
484
|
2151
|
|
|
|
|
5644
|
push @$row, $col; |
485
|
|
|
|
|
|
|
} |
486
|
717
|
|
|
|
|
1883
|
$row; |
487
|
|
|
|
|
|
|
} |
488
|
6
|
|
|
|
|
88
|
)->to_array; |
489
|
|
|
|
|
|
|
|
490
|
6
|
|
|
|
|
89
|
my @indices = 0 .. @cols - 1; |
491
|
|
|
|
|
|
|
my $pattern |
492
|
6
|
|
|
|
|
28
|
= join($opts{col_sep}, map {"%-$col_lengths[$_]s"} @indices) . "\n"; |
|
18
|
|
|
|
|
86
|
|
493
|
|
|
|
|
|
|
|
494
|
6
|
50
|
|
|
|
29
|
if ($opts{header}) { |
495
|
6
|
|
|
|
|
58
|
printf $pattern, @cols; |
496
|
6
|
|
|
|
|
206
|
my $sep = $opts{col_sep}; |
497
|
6
|
|
|
|
|
46
|
$sep =~ s/[^|]/-/g; |
498
|
6
|
|
|
|
|
21
|
print join($sep, map {'-' x $col_lengths[$_]} @indices) . "\n"; |
|
18
|
|
|
|
|
69
|
|
499
|
|
|
|
|
|
|
} |
500
|
6
|
|
|
|
|
103
|
for my $row (@$rows) { |
501
|
717
|
|
|
|
|
14592
|
printf $pattern, @$row; |
502
|
|
|
|
|
|
|
} |
503
|
|
|
|
|
|
|
} |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
sub stop_if { |
506
|
2
|
|
|
2
|
1
|
6
|
my ($self, $sub) = @_; |
507
|
|
|
|
|
|
|
Catmandu::Iterator->new( |
508
|
|
|
|
|
|
|
sub { |
509
|
|
|
|
|
|
|
sub { |
510
|
5
|
|
|
|
|
9
|
state $next = $self->generator; |
511
|
5
|
|
50
|
|
|
26
|
my $data = $next->() // return; |
512
|
5
|
100
|
|
|
|
25
|
$sub->($data) && return; |
513
|
3
|
|
|
|
|
17
|
$data; |
514
|
|
|
|
|
|
|
} |
515
|
2
|
|
|
2
|
|
8
|
} |
516
|
2
|
|
|
|
|
11
|
); |
517
|
|
|
|
|
|
|
} |
518
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
sub run { |
520
|
5
|
|
|
5
|
1
|
13
|
my ($self) = @_; |
521
|
5
|
|
|
|
|
12
|
my $next = $self->generator; |
522
|
5
|
|
|
|
|
25
|
my $run = 0; |
523
|
5
|
|
|
|
|
12
|
while (defined($next->())) { |
524
|
24
|
|
100
|
|
|
57
|
$run ||= 1; |
525
|
|
|
|
|
|
|
} |
526
|
5
|
|
|
|
|
39
|
$run; |
527
|
|
|
|
|
|
|
} |
528
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
1; |
530
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
__END__ |
532
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
=pod |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
=head1 NAME |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
Catmandu::Iterable - Base role for all iterable Catmandu classes |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
=head1 SYNOPSIS |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
# Create an example Iterable using the Catmandu::Importer::Mock class |
542
|
|
|
|
|
|
|
my $it = Catmandu::Importer::Mock->new(size => 10); |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
my $array_ref = $it->to_array; |
545
|
|
|
|
|
|
|
my $num = $it->count; |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
# Loop functions |
548
|
|
|
|
|
|
|
$it->each(sub { print shift->{n} }); |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
my $item = $it->first; |
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
$it->rest |
553
|
|
|
|
|
|
|
->each(sub { print shift->{n} }); |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
$it->slice(3,2) |
556
|
|
|
|
|
|
|
->each(sub { print shift->{n} }); |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
$it->take(5) |
559
|
|
|
|
|
|
|
->each(sub { print shift->{n} }); |
560
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
$it->group(5) |
562
|
|
|
|
|
|
|
->each(sub { printf "group of %d items\n" , shift->count}); |
563
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
$it->tap(\&logme)->tap(\&printme)->tap(\&mailme) |
565
|
|
|
|
|
|
|
->each(sub { print shift->{n} }); |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
my $titles = $it->pluck('title')->to_array; |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
# Select and loop |
570
|
|
|
|
|
|
|
my $item = $it->detect(sub { shift->{n} > 5 }); |
571
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
$it->select(sub { shift->{n} > 5}) |
573
|
|
|
|
|
|
|
->each(sub { print shift->{n} }); |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
$it->reject(sub { shift->{n} > 5}) |
576
|
|
|
|
|
|
|
->each(sub { print shift->{n} }); |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
# Boolean |
579
|
|
|
|
|
|
|
if ($it->any(sub { shift->{n} > 5}) { |
580
|
|
|
|
|
|
|
.. at least one n > 5 .. |
581
|
|
|
|
|
|
|
} |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
if ($it->many(sub { shift->{n} > 5}) { |
584
|
|
|
|
|
|
|
.. at least two n > 5 .. |
585
|
|
|
|
|
|
|
} |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
if ($it->all(sub { shift->{n} > 5}) { |
588
|
|
|
|
|
|
|
.. all n > 5 .. |
589
|
|
|
|
|
|
|
} |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
# Modify and summary |
592
|
|
|
|
|
|
|
my $it2 = $it->map(sub { shift->{n} * 2 }); |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
my $sum = $it2->reduce(0,sub { |
595
|
|
|
|
|
|
|
my ($prev,$this) = @_; |
596
|
|
|
|
|
|
|
$prev + $this; |
597
|
|
|
|
|
|
|
}); |
598
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
my $it3 = $it->group(2)->invoke('to_array'); |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
# Calculate maximum of 'n' field |
602
|
|
|
|
|
|
|
my $max = $it->max(sub { |
603
|
|
|
|
|
|
|
shift->{n}; |
604
|
|
|
|
|
|
|
}); |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
# Calculate minimum of 'n' field |
607
|
|
|
|
|
|
|
my $in = $it->min(sub { |
608
|
|
|
|
|
|
|
shift->{n}; |
609
|
|
|
|
|
|
|
}); |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
=head1 DESCRIPTION |
612
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
The Catmandu::Iterable class provides many list methods to Iterators such as Importers and |
614
|
|
|
|
|
|
|
Exporters. Most of the methods are lazy if the underlying datastream supports it. Beware of |
615
|
|
|
|
|
|
|
idempotence: many iterators contain state information and calls will give different results on |
616
|
|
|
|
|
|
|
a second invocation. |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
=head1 METHODS |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
=head2 to_array |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
Return all the items in the iterator as an array ref. |
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
=head2 count |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
Return the count of all the items in the iterator. |
627
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
=head2 add_to |
629
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
Add all items in the iterator to a L<Catmandu::Exporter>. Returns a true value |
631
|
|
|
|
|
|
|
when the exportwas successful or undef on error. |
632
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
=head3 LOOPING |
634
|
|
|
|
|
|
|
|
635
|
|
|
|
|
|
|
=head2 each(\&callback) |
636
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
For each item in the iterator execute the callback function with the item as |
638
|
|
|
|
|
|
|
first argument. Returns the number of items in the iterator. |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
=head2 first |
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
Return the first item from the iterator. |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
=head2 rest |
645
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
Returns an iterator containing everything except the first item. |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
=head2 slice($index,$length) |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
Returns an new iterator starting at the item at C<$index> returning at most <$length> items. |
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
=head2 take($num) |
653
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
Returns an iterator with the first C<$num> items. |
655
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
=head2 group($num) |
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
Splits the iterator into new iterators each containing C<$num> items. |
659
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
$it->group(500)->each(sub { |
661
|
|
|
|
|
|
|
my $group_it = $_[0]; |
662
|
|
|
|
|
|
|
$group_it->each(sub { |
663
|
|
|
|
|
|
|
my $item = $_[0]; |
664
|
|
|
|
|
|
|
# ... |
665
|
|
|
|
|
|
|
}); |
666
|
|
|
|
|
|
|
}); |
667
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
Note that the group iterators load their items in memory. The last group |
669
|
|
|
|
|
|
|
iterator will contain less than C<$num> item unless the item count is divisible |
670
|
|
|
|
|
|
|
by C<$num>. |
671
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
=head2 interleave(@iterators) |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
Returns an iterator which returns the first item of each iterator then the |
675
|
|
|
|
|
|
|
second of each and so on. |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
=head2 contains($data) |
678
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
Alias for C<includes>. |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
=head2 includes($data) |
682
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
return true if any item in the collection is deeply equal to C<$data>. |
684
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
=head2 tap(\&callback) |
686
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
Returns a copy of the iterator and executing callback on each item. This method works |
688
|
|
|
|
|
|
|
like the Unix C<tee> command. Use this command to peek into an iterable while it is |
689
|
|
|
|
|
|
|
processing results. E.g. you are writing code to process an iterable and wrote |
690
|
|
|
|
|
|
|
something like: |
691
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
$it->each(sub { |
693
|
|
|
|
|
|
|
# Very complicated routine |
694
|
|
|
|
|
|
|
... |
695
|
|
|
|
|
|
|
}); |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
Now you would like to benchmark this piece of code (how fast are we processing). |
698
|
|
|
|
|
|
|
This can be done by tapping into the iterator and calling a 'benchmark' subroutine |
699
|
|
|
|
|
|
|
in your program that for instance counts the number of items divided by the |
700
|
|
|
|
|
|
|
execution time. |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
$it->tap(\&benchmark)->each(sub { |
703
|
|
|
|
|
|
|
# Very complicated routine |
704
|
|
|
|
|
|
|
... |
705
|
|
|
|
|
|
|
}); |
706
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
sub benchmark { |
708
|
|
|
|
|
|
|
my $item = shift; |
709
|
|
|
|
|
|
|
$start ||= time; |
710
|
|
|
|
|
|
|
$count++; |
711
|
|
|
|
|
|
|
|
712
|
|
|
|
|
|
|
printf "%d recs/sec\n" , $count/(time - $start + 1) if $count % 100 == 0; |
713
|
|
|
|
|
|
|
} |
714
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
Note that the C<benchmark> method already implements this common case. |
716
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
=head2 every($num, \&callback) |
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
Similar to C<tap>, but only calls the callback every C<$num> times. Useful for |
720
|
|
|
|
|
|
|
benchmarking and sampling. |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
=head2 detect(\&callback) |
723
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
Returns the first item for which callback returns a true value. |
725
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
=head2 detect(qr/..../) |
727
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
If the iterator contains STRING values, then return the first item which matches the |
729
|
|
|
|
|
|
|
regex. |
730
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
=head2 detect($key => $val) |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
If the iterator contains HASH values, then return the first item where the value of |
734
|
|
|
|
|
|
|
C<$key> is equal to C<$val>. |
735
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
=head2 detect($key => qr/..../) |
737
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
If the iterator contains HASH values, then return the first item where the value of |
739
|
|
|
|
|
|
|
C<$key> matches the regex. |
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
=head2 detect($key => [$val, ...]) |
742
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
If the iterator contains HASH values, then return the first item where the value of |
744
|
|
|
|
|
|
|
C<$key> is equal to any of the values given. |
745
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
=head2 pluck($key) |
747
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
Return an iterator that only contains the values of the given C<$key>. |
749
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
=head2 select(\&callback) |
751
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
Returns an iterator containing only items item for which the callback returns a true value. |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
=head2 select(qr/..../) |
755
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
If the iterator contains STRING values, then return each item which matches the regex. |
757
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
=head2 select($key => $val) |
759
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
If the iterator contains HASH values, then return each item where the value of |
761
|
|
|
|
|
|
|
C<$key> is equal to C<$val>. |
762
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
=head2 select($key => qr/..../) |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
If the iterator contains HASH values, then return each item where the value of C<$key> |
766
|
|
|
|
|
|
|
matches the regex. |
767
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
=head2 select($key => [$val, ...]) |
769
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
If the iterator contains HASH values, then return each item where the value of |
771
|
|
|
|
|
|
|
C<$key> is equal to any of the vals given. |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
=head2 grep( ... ) |
774
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
Alias for C<select( ... )>. |
776
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
=head2 reject(\&callback) |
778
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
Returns an iterator containing each item for which callback returns a false value. |
780
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
=head2 reject(qr/..../) |
782
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
If the iterator contains STRING values, then reject every item except those |
784
|
|
|
|
|
|
|
matching the regex. |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
=head2 reject($key => qr/..../) |
787
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
If the iterator contains HASH values, then reject every item for where the value of C<$key> |
789
|
|
|
|
|
|
|
DOESN'T match the regex. |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
=head2 reject($key => $val) |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
If the iterator contains HASH values, then return each item where the value of |
794
|
|
|
|
|
|
|
C<$key> is NOT equal to C<$val>. |
795
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
=head2 reject($key => [$val, ...]) |
797
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
If the iterator contains HASH values, then return each item where the value of |
799
|
|
|
|
|
|
|
C<$key> is NOT equal to any of the values given. |
800
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
=head2 sorted |
802
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
Returns an iterator with items sorted lexically. Note that sorting requires |
804
|
|
|
|
|
|
|
memory because all items are buffered in a L<Catmandu::ArrayIterator>. |
805
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
=head2 sorted(\&callback) |
807
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
Returns an iterator with items sorted by a callback. The callback is expected to |
809
|
|
|
|
|
|
|
returns an integer less than, equal to, or greater than C<0>. The following code |
810
|
|
|
|
|
|
|
snippets result in equal arrays: |
811
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
$iterator->sorted(\&callback)->to_array |
813
|
|
|
|
|
|
|
[ sort \&callback @{ $iterator->to_array } ] |
814
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
=head2 sorted($key) |
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
Returns an iterator with items lexically sorted by a key. This is equivalent to |
818
|
|
|
|
|
|
|
sorting with the following callback: |
819
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
$iterator->sorted(sub { $_[0]->{$key} cmp $_[1]->{$key} }) |
821
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
=head3 EXTERNAL ITERATOR |
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
L<Catmandu::Iterable> behaves like an internal iterator. C<next> and C<rewind> |
825
|
|
|
|
|
|
|
allow you to use it like an external iterator. |
826
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
=head2 next |
828
|
|
|
|
|
|
|
|
829
|
|
|
|
|
|
|
Each call to C<next> will return the next item until the iterator is exhausted, |
830
|
|
|
|
|
|
|
then it will keep returning C<undef>. |
831
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
while (my $data = $it->next) { |
833
|
|
|
|
|
|
|
# do stuff |
834
|
|
|
|
|
|
|
} |
835
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
$it->next; # returns undef |
837
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
=head2 rewind |
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
Rewind the external iterator to the first item. |
841
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
$it->next; # => {n => 1} |
843
|
|
|
|
|
|
|
$it->next; # => {n => 2} |
844
|
|
|
|
|
|
|
$it->next; # => {n => 3} |
845
|
|
|
|
|
|
|
$it->rewind |
846
|
|
|
|
|
|
|
$it->next; # => {n => 1} |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
Note the the iterator must support this behavior. Many importers are not |
849
|
|
|
|
|
|
|
rewindable. |
850
|
|
|
|
|
|
|
|
851
|
|
|
|
|
|
|
=head3 BOOLEAN FUNCTIONS |
852
|
|
|
|
|
|
|
|
853
|
|
|
|
|
|
|
=head2 any(\&callback) |
854
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
Returns true if at least one item generates a true value when executing callback. |
856
|
|
|
|
|
|
|
|
857
|
|
|
|
|
|
|
=head2 many(\&callback) |
858
|
|
|
|
|
|
|
|
859
|
|
|
|
|
|
|
Alias for C<many>. |
860
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
=head2 many(\&callback) |
862
|
|
|
|
|
|
|
|
863
|
|
|
|
|
|
|
Returns true if at least two items generate a true value when executing callback. |
864
|
|
|
|
|
|
|
|
865
|
|
|
|
|
|
|
=head2 all(\&callback) |
866
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
Returns true if all the items generate a true value when executing callback. |
868
|
|
|
|
|
|
|
|
869
|
|
|
|
|
|
|
=head3 MAP & REDUCE |
870
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
=head2 map(\&callback) |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
Returns a new iterator containing for each item the result of the callback. If |
874
|
|
|
|
|
|
|
the callback returns multiple or no items, the resulting iterator will grow or |
875
|
|
|
|
|
|
|
shrink. |
876
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
=head2 fix(...) |
878
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
Apply a L<Catmandu::Fix> to each item and return the result as new iterator. |
880
|
|
|
|
|
|
|
|
881
|
|
|
|
|
|
|
=head2 reduce([$start],\&callback) |
882
|
|
|
|
|
|
|
|
883
|
|
|
|
|
|
|
For each item in the iterator execute C<&callback($prev,$item)> where C<$prev> is the |
884
|
|
|
|
|
|
|
optional C<$start> value or the result of the previous call to callback. Returns the |
885
|
|
|
|
|
|
|
final result of the callback function. |
886
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
=head2 invoke($name) |
888
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
Returns an interator were the method C<$name> is called on every object in the iterable. |
890
|
|
|
|
|
|
|
This is a shortcut for C<$it->map(sub { $_[0]->$name })>. |
891
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
=head2 max() |
893
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
Returns the maximum of an iterator containing only numbers. |
895
|
|
|
|
|
|
|
|
896
|
|
|
|
|
|
|
=head2 max(\&callback) |
897
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
Returns the maximum of the numbers returned by executing callback. |
899
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
=head2 min() |
901
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
Returns the minimum of an iterator containing only numbers. |
903
|
|
|
|
|
|
|
|
904
|
|
|
|
|
|
|
=head2 min(\&callback) |
905
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
Returns the minimum of the numbers returned by executing callback. |
907
|
|
|
|
|
|
|
|
908
|
|
|
|
|
|
|
=head2 benchmark() |
909
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
Prints the number of records processed per second to STDERR. |
911
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
=head2 format(cols => ['key', ...], col_sep => ' | ', header => 1|0) |
913
|
|
|
|
|
|
|
|
914
|
|
|
|
|
|
|
Print the iterator data formatted as a spreadsheet like table. Note that this |
915
|
|
|
|
|
|
|
method will load the whole dataset in memory to calculate column widths. See |
916
|
|
|
|
|
|
|
also L<Catmandu::Exporter::Table> for a more elaborated method of printing |
917
|
|
|
|
|
|
|
iterators in tabular form. |
918
|
|
|
|
|
|
|
|
919
|
|
|
|
|
|
|
=head2 stop_if(\&callback) |
920
|
|
|
|
|
|
|
|
921
|
|
|
|
|
|
|
Returns a new iterator thats stops processing if the callback returns false. |
922
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
# stop after encountering 3 frobnitzes |
924
|
|
|
|
|
|
|
my $frobnitzes = 0; |
925
|
|
|
|
|
|
|
$iterator->stop_if(sub { |
926
|
|
|
|
|
|
|
my $rec = shift; |
927
|
|
|
|
|
|
|
$frobnitzes++ if $rec->{title} =~ /frobnitz/; |
928
|
|
|
|
|
|
|
$frobnitzes > 3; |
929
|
|
|
|
|
|
|
})->each(sub { |
930
|
|
|
|
|
|
|
my $rec = shift; |
931
|
|
|
|
|
|
|
... |
932
|
|
|
|
|
|
|
}); |
933
|
|
|
|
|
|
|
|
934
|
|
|
|
|
|
|
=head2 run |
935
|
|
|
|
|
|
|
|
936
|
|
|
|
|
|
|
Simply invokes the iterator and returns 1 if any records were processed, 0 otherwise. |
937
|
|
|
|
|
|
|
|
938
|
|
|
|
|
|
|
$it = $it->tap(sub { |
939
|
|
|
|
|
|
|
# do something |
940
|
|
|
|
|
|
|
}); |
941
|
|
|
|
|
|
|
$it = $it->tap(sub { |
942
|
|
|
|
|
|
|
# do another thing |
943
|
|
|
|
|
|
|
}); |
944
|
|
|
|
|
|
|
$it->run |
945
|
|
|
|
|
|
|
|
946
|
|
|
|
|
|
|
print 'not empty' if $it->run; |
947
|
|
|
|
|
|
|
|
948
|
|
|
|
|
|
|
=head1 SEE ALSO |
949
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
L<Catmandu::Iterator>. |
951
|
|
|
|
|
|
|
|
952
|
|
|
|
|
|
|
=cut |