Sidef/Object/Enumerator.pm untested

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 206 CC 14 SCAR 21.3
CC
14
Cov
0%
first
20.0
nth
20.0
while
6.0
CRAP 8.5
1 package Sidef::Object::Enumerator;
2  
3 use utf8;
4 use 5.016;
5 use Sidef::Types::Block::Block;
6  
7 ##use overload q{""} => \&to_a;
8  
9 sub new {
10 my (undef, $block) = @_;
11 bless {block => $block}, __PACKAGE__;
12 }
13  
14 *call = \&new;
15  
16 sub first {
17 my ($self, $n, $block) = @_;
18  
19 $n = CORE::int($n);
20  
21 my @arr;
22 my $count = 0;
23  
24 $self->{block}->run(
25 Sidef::Types::Block::Block->new(
26 code => sub {
27 if (defined($block) ? $block->run(@_) : 1) {
28 push @arr, @_;
29 if (++$count >= $n) {
30 goto RETURN;
31 }
32 }
33 }
34 )
35 );
36  
37 RETURN: Sidef::Types::Array::Array->new(\@arr);
38 }
39  
40 sub nth {
41 my ($self, $n, $block) = @_;
42  
43 $n = CORE::int($n);
44  
45 my @arr;
46 my $count = 0;
47  
48 $self->{block}->run(
49 Sidef::Types::Block::Block->new(
50 code => sub {
51 if (defined($block) ? $block->run(@_) : 1) {
52 if (++$count >= $n) {
53 push @arr, @_;
54 goto RETURN;
55 }
56 }
57 }
58 )
59 );
60  
61 RETURN: $arr[0];
62 }
63  
64 sub while {
65 my ($self, $block) = @_;
66  
67 my @arr;
68  
69 $self->{block}->run(
70 Sidef::Types::Block::Block->new(
71 code => sub {
72 if ($block->run(@_)) {
73 push @arr, @_;
74 }
75 else {
76 goto RETURN;
77 }
78 }
79 )
80 );
81  
82 RETURN: Sidef::Types::Array::Array->new(\@arr);
83 }
84  
85 sub drop_while {
86 my ($self, $block) = @_;
87 my @arr;
88 my $dropping = 1;
89 $self->{block}->run(
90 Sidef::Types::Block::Block->new(
91 code => sub {
92 if ($dropping) {
93 $block->run(@_) and return;
94 $dropping = 0;
95 }
96 push @arr, @_;
97 }
98 )
99 );
100 Sidef::Types::Array::Array->new(\@arr);
101 }
102  
103 sub to_a {
104 my ($self) = @_;
105  
106 my @arr;
107 $self->{block}->run(
108 Sidef::Types::Block::Block->new(
109 code => sub {
110 push @arr, @_;
111 },
112 )
113 );
114  
115 Sidef::Types::Array::Array->new(\@arr);
116 }
117  
118 sub each {
119 my ($self, $block) = @_;
120 $self->{block}->run($block);
121 $self;
122 }
123  
124 sub map {
125 my ($self, $block) = @_;
126  
127 my @arr;
128 $self->{block}->run(
129 Sidef::Types::Block::Block->new(
130 code => sub {
131 push @arr, map { $block->run($_) } @_;
132 },
133 )
134 );
135  
136 Sidef::Types::Array::Array->new(\@arr);
137 }
138  
139 *collect = \↦
140  
141 sub grep {
142 my ($self, $block) = @_;
143  
144 my @arr;
145 $self->{block}->run(
146 Sidef::Types::Block::Block->new(
147 code => sub {
148 push @arr, grep { $block->run($_) } @_;
149 },
150 )
151 );
152  
153 Sidef::Types::Array::Array->new(\@arr);
154 }
155  
156 *select = \&grep;
157  
158 sub reduce {
159 my ($self, $block, $initial) = @_;
160  
161 my $acc = $initial;
162 $self->{block}->run(
163 Sidef::Types::Block::Block->new(
164 code => sub {
165 $acc = defined($acc) ? $block->run($acc, @_) : $_[0];
166 }
167 )
168 );
169  
170 $acc;
171 }
172  
173 sub count {
174 my ($self, $block) = @_;
175  
176 my $count = 0;
177  
178 $self->{block}->run(
179 Sidef::Types::Block::Block->new(
180 code => sub {
181 ++$count if $block->run(@_);
182 },
183 )
184 );
185  
186 Sidef::Types::Number::Number::_set_int($count);
187 }
188  
189 *count_by = \&count;
190  
191 sub length {
192 my ($self) = @_;
193  
194 my $count = 0;
195 $self->{block}->run(
196 Sidef::Types::Block::Block->new(
197 code => sub {
198 ++$count;
199 },
200 )
201 );
202  
203 Sidef::Types::Number::Number::_set_int($count);
204 }
205  
206 *len = \&length; # alias
207 *size = \&length;
208  
209 sub sum {
210 my ($self, $block) = @_;
211 $block //= Sidef::Types::Block::Block::IDENTITY;
212 my $total;
213 $self->{block}->run(
214 Sidef::Types::Block::Block->new(
215 code => sub {
216 my $v = $block->run(@_);
217 $total = defined($total) ? $total->add($v) : $v;
218 }
219 )
220 );
221 $total // Sidef::Types::Number::Number::ZERO;
222 }
223  
224 sub min {
225 my ($self, $block) = @_;
226 $block //= Sidef::Types::Block::Block::IDENTITY;
227 my ($best, $best_v);
228 $self->{block}->run(
229 Sidef::Types::Block::Block->new(
230 code => sub {
231 my $v = $block->run(@_);
232 if (!defined($best_v) || CORE::int($v cmp $best_v) < 0) {
233 ($best, $best_v) = ($_[0], $v);
234 }
235 }
236 )
237 );
238 $best;
239 }
240  
241 sub max {
242 my ($self, $block) = @_;
243 $block //= Sidef::Types::Block::Block::IDENTITY;
244 my ($best, $best_v);
245 $self->{block}->run(
246 Sidef::Types::Block::Block->new(
247 code => sub {
248 my $v = $block->run(@_);
249 if (!defined($best_v) || CORE::int($v cmp $best_v) > 0) {
250 ($best, $best_v) = ($_[0], $v);
251 }
252 }
253 )
254 );
255 $best;
256 }
257  
258 #<<<
259 #~ #
260 #~ ## AUTOLOAD
261 #~ #
262  
263 #~ sub DESTROY { }
264  
265 #~ our $AUTOLOAD;
266  
267 #~ sub AUTOLOAD {
268 #~ my ($self, @arg) = @_;
269 #~ my ($method) = ($AUTOLOAD =~ /^.*[^:]::(.*)$/);
270 #~ $self->to_a->$method(@arg);
271 #~ }
272 #>>>
273  
274 1