Sidef/Object/Lazy.pm untested

filter: Statementstmt 0.0% 0 / 195 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 318 CC 13 SCAR 16.8
CC
13
Cov
0%
_fast_reduce
12.0
reduce
12.0
first
12.0
CRAP 5.4
1 package Sidef::Object::Lazy;
2  
3 use utf8;
4 use 5.016;
5 ##use overload q{""} => \&to_a;
6 use Sidef::Types::Block::Block;
7 use Sidef::Types::Array::Array;
8 use Sidef::Types::Number::Number;
9  
10 sub new {
11 my (undef, %opt) = @_;
12 bless {
13 calls => [],
14 %opt,
15 },
16 __PACKAGE__;
17 }
18  
19 sub _xs {
20 my ($self, $callback) = @_;
21  
22 my $iter = $self->{obj}->iter;
23  
24 ITEM: for (; ;) {
25 my @arg = ($iter->run() // last);
26 foreach my $call (@{$self->{calls}}) {
27 @arg = $call->(@arg);
28 @arg || next ITEM;
29 }
30 last if $callback->(@arg);
31 }
32 }
33  
34 sub to_a {
35 my ($self) = @_;
36 my @arr;
37 $self->_xs(sub { push @arr, @_; 0 });
38 Sidef::Types::Array::Array->new(\@arr);
39 }
40  
41 sub each {
42 my ($self, $block) = @_;
43 $self->_xs(sub { $block->run(@_); 0 });
44 $self;
45 }
46  
47 sub _fast_reduce {
48 my ($self, $method, $result, $callback) = @_;
49  
50 my @list;
51 my $count = 0;
52 my $index = 0;
53  
54 $self->_xs(
55 sub {
56 CORE::push(@list, $callback->($index++, @_));
57  
58 if (++$count > 1e5) {
59 $count = 0;
60 $result = $result->$method(CORE::splice(@list));
61 }
62 0; # keep looping
63 }
64 );
65  
66 if (@list) {
67 $result = $result->$method(CORE::splice(@list));
68 }
69  
70 $result;
71 }
72  
73 sub reduce_by {
74 my ($self, $block, $result) = @_;
75  
76 $self->_xs(
77 sub {
78 if (defined($result)) {
79 $result = $block->run($result, @_);
80 }
81 else {
82 $result = $_[0];
83 }
84 0; # keep looping
85 }
86 );
87  
88 return $result;
89 }
90  
91 sub reduce {
92 my ($self, $operator, $result) = @_;
93  
94 if (ref($operator) eq 'Sidef::Types::Block::Block') {
95 goto &reduce_by;
96 }
97  
98 $operator = "$operator";
99 $self->_xs(
100 sub {
101 if (defined($result)) {
102 $result = $result->$operator(@_);
103 }
104 else {
105 $result = $_[0];
106 }
107 0; # keep looping
108 }
109 );
110 return $result;
111 }
112  
113 sub sum {
114 my ($self, $block) = @_;
115 $block //= Sidef::Types::Block::Block::IDENTITY;
116 $self->_fast_reduce('sum', Sidef::Types::Number::Number::ZERO, sub { $block->run($_[1]) });
117 }
118  
119 *sum_by = \∑
120  
121 sub prod {
122 my ($self, $block) = @_;
123 $block //= Sidef::Types::Block::Block::IDENTITY;
124 $self->_fast_reduce('prod', Sidef::Types::Number::Number::ONE, sub { $block->run($_[1]) });
125 }
126  
127 *prod_by = \∏
128  
129 sub sum_kv {
130 my ($self, $block) = @_;
131 $block //= Sidef::Types::Block::Block::IDENTITY;
132 $self->_fast_reduce(
133 'sum',
134 Sidef::Types::Number::Number::ZERO,
135 sub {
136 $block->run(Sidef::Types::Number::Number::_set_int($_[0]), $_[1]);
137 }
138 );
139 }
140  
141 sub prod_kv {
142 my ($self, $block) = @_;
143 $block //= Sidef::Types::Block::Block::IDENTITY;
144 $self->_fast_reduce('prod', Sidef::Types::Number::Number::ONE, sub { $block->run(Sidef::Types::Number::Number::_set_int($_[0]), $_[1]) });
145 }
146  
147 sub iter {
148 my ($self) = @_;
149  
150 my $iter = $self->{obj}->iter;
151  
152 Sidef::Types::Block::Block->new(
153 code => sub {
154 ITEM: {
155 my $item = $iter->run() // return undef;
156 my @arg = ($item);
157 foreach my $call (@{$self->{calls}}) {
158 @arg = $call->(@arg);
159 @arg || redo ITEM;
160 }
161 $arg[0];
162 }
163 }
164 );
165 }
166  
167 sub first {
168 my ($self, $n) = @_;
169  
170 if (!defined($n)) {
171 my @arr;
172 $self->_xs(sub { push(@arr, @_); 1; });
173 return $arr[0];
174 }
175  
176 if (ref($n) eq 'Sidef::Types::Block::Block') {
177 return $self->first_by($n);
178 }
179  
180 $n = CORE::int($n);
181 $n > 0 || return Sidef::Types::Array::Array->new([]);
182  
183 my @arr;
184  
185 $self->_xs(
186 sub {
187 push @arr, @_;
188 @arr >= $n;
189 }
190 );
191  
192 Sidef::Types::Array::Array->new(\@arr);
193 }
194  
195 sub nth {
196 my ($self, $n) = @_;
197  
198 my @arr;
199 $n = CORE::int($n);
200 $n > 0 || return undef;
201  
202 $self->_xs(
203 sub {
204 push @arr, @_;
205 @arr >= $n;
206 }
207 );
208  
209 $arr[$n - 1];
210 }
211  
212 sub first_by {
213 my ($self, $block) = @_;
214 $self->grep($block)->first(1)->[0];
215 }
216  
217 #
218 ## Functional methods
219 #
220  
221 sub grep {
222 my ($self, $block) = @_;
223 __PACKAGE__->new(
224 obj => $self->{obj},
225 calls => [
226 @{$self->{calls}},
227 sub {
228 $block->run($_[0]) ? $_[0] : ();
229 },
230 ],
231 );
232 }
233  
234 *select = \&grep;
235  
236 sub while {
237 my ($self, $block) = @_;
238  
239 my @arr;
240  
241 $self->_xs(
242 sub {
243 $block->run(@_) ? do { push(@arr, @_); 0 } : 1;
244 }
245 );
246  
247 Sidef::Types::Array::Array->new(\@arr);
248 }
249  
250 sub map {
251 my ($self, $block) = @_;
252 __PACKAGE__->new(
253 obj => $self->{obj},
254 calls => [
255 @{$self->{calls}},
256 sub {
257 $block->run($_[0]);
258 }
259 ],
260 );
261 }
262  
263 *collect = \↦
264  
265 sub drop {
266 my ($self, $n) = @_;
267 $n = CORE::int($n);
268 my $count = 0;
269 __PACKAGE__->new(
270 obj => $self->{obj},
271 calls => [
272 @{$self->{calls}},
273 sub {
274 if ($count < $n) { ++$count; return (); }
275 ($_[0]);
276 },
277 ],
278 );
279 }
280  
281 sub uniq {
282 my ($self) = @_;
283 my %seen;
284 __PACKAGE__->new(
285 obj => $self->{obj},
286 calls => [
287 @{$self->{calls}},
288 sub {
289 my $key = "$_[0]";
290 $seen{$key}++ ? () : ($_[0]);
291 },
292 ],
293 );
294 }
295  
296 sub zip {
297 my ($self, $other) = @_;
298 my $iter1 = $self->iter;
299 my $iter2 = $other->iter;
300 my @pairs;
301 for (; ;) {
302 my $A = $iter1->run() // last;
303 my $B = $iter2->run() // last;
304 push @pairs, Sidef::Types::Array::Array->new([$A, $B]);
305 }
306 Sidef::Types::Array::Array->new(\@pairs);
307 }
308  
309 sub take {
310 my ($self, $n) = @_;
311 $n = CORE::int($n);
312 my $orig = $self->{obj};
313 my $remaining = $n;
314 my $limited = bless {
315 obj => $orig,
316 n => $n,
317 },
318 'Sidef::Object::Lazy::_Take';
319  
320 {
321 no strict 'refs';
322 *{'Sidef::Object::Lazy::_Take::iter'} = sub {
323 my ($lim) = @_;
324 my $inner = $lim->{obj}->iter;
325 my $remaining = $lim->{n};
326 Sidef::Types::Block::Block->new(
327 code => sub {
328 $remaining > 0 or return undef;
329 --$remaining;
330 $inner->run();
331 }
332 );
333 };
334 }
335  
336 __PACKAGE__->new(obj => $limited, calls => [@{$self->{calls}}]);
337 }
338  
339 sub lazy {
340 my ($self) = @_;
341 $self;
342 }
343  
344 1;