lib/FP/Stream.pm

filter: Statementstmt 96.3% 758 / 787 Branchbran 67.5% 135 / 200 Conditioncond 46.7% 29 / 62 MC/DCmcdc 12.5% 5 / 40 Subroutinesub 97.8% 232 / 237 Podpod 0.0% 0 / 45 totaltotal 84.5% 1159 / 1371 CC 128 SCAR 9.9
CC
128
Cov
88%
stream_drop
20.0
stream_mixed_fold_right
12.0
F
8.1
CRAP 2.7
« meta/FunctionalPerl/ModuleList.pm ↑ Summary lib/PXML/Util.pm »
1 #
2 # Copyright (c) 2013-2021 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::Stream - lazily generated, singly linked (purely functional) lists
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Equal 'is_equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use FP::Stream ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 is stream_length(stream_iota 101, 5), 5;
20 #is stream_length(stream_iota 0, 5000000), 5000000;
21  
22 1 ▶ use FP::Lazy; # force
Subroutine BEGIN: called CC=1 SCAR=0.0
23 is force( stream_fold_right sub { my ($n,$rest) = @_;
24 $n + force $rest },
25 0,
26 stream_iota 0, 5),
27 10;
28  
29 Alternatively (and probably preferably), use methods:
30  
31 is stream_iota(101, 5)->length, 5;
32 # note that length needs to walk the whole stream
33  
34 is stream_iota->map(sub{ $_[0]*$_[0]})->take(5)->sum,
35 0+1+4+9+16;
36  
37 is stream_iota(0, 5)->fold(sub { my ($n,$rest) = @_;
38 $n + $rest },
39 0),
40 10;
41  
42 NOTE that the method calls are forcing evaluation of the object (the
43 first cell of the input stream), since that's the only way to know the
44 type to be dispatched on. This is unlike the non-generic functions,
45 some of which (like cons) don't force evaluation of their arguments.
46  
47 =head1 DESCRIPTION
48  
49 `FP::Stream`s (from now on called "streams") are the same as
50 `FP::List`s (from now on called "lists") except that their cells are
51 generated on demand. If your code has got a list in a particular point
52 in time, all the items in the list are calculated already, and the
53 list takes as much space in memory to represent all of the values. A
54 stream on the other hand, at the beginning consists of only a single
55 unevaluated promise, which is an object from `FP::Lazy` which
56 represents a to-be done calculation to produce, surprise, a
57 `FP::List::Null` (end of list) value to indicate the end or a
58 `FP::List::Pair` which holds the first value and the remainder of the
59 sequence--itself again a stream, or more precisely a promise to either
60 a null or pair value.
61  
62 So, like a list, a stream is a recursive data structure, with the only
63 difference that the rest field in pairs is a promise. It also
64 automatically "inherits" all of the list methods, since calling a
65 method on a promise is forcing the promise and then is delegated to
66 the generated value; although delegation goes to the method with
67 "stream_" prefixed, if it exists--this is to allow for overrides if a
68 method on a stream needs to behave differently. For example `map` is
69 overridden with `stream_map` which produces the resulting linked list
70 lazily:
71  
72 1 ▶ use FP::Div 'inc'; use FP::List 'list';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 is ref( list(30, 60)->map(\&inc) ),
75 'FP::List::Pair';
76 is ref( list(30, 60)->stream_map(\&inc) ),
77 'FP::Lazy::Promise';
78 is ref( stream(30, 60)->map(\&inc) ),
79 'FP::Lazy::Promise';
80 is ref( stream(30, 60)->stream_map(\&inc) ),
81 'FP::Lazy::Promise';
82  
83 is ref(force( stream(30, 60)->map(\&inc) )),
84 'FP::List::Pair';
85  
86 All of the examples evaluate to the same values--the result isn't
87 different, it's just calculated lazily (on demand) in some cases. The
88 `equal` calls in the following are all true even though the second
89 argument is always an eager list (whereas the first argument is a
90 stream in 3 out of 4 cases) because `equal` automatically forces the
91 evaluation of promises it encounters.
92  
93 1 ▶ use FP::Equal 'equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 ok equal(list(30, 60)->map(\&inc), list(31, 61));
96 ok equal(list(30, 60)->stream_map(\&inc), list(31, 61));
97 ok equal(stream(30, 60)->map(\&inc), list(31, 61));
98 ok equal(stream(30, 60)->stream_map(\&inc), list(31, 61));
99  
100 =head1 SEE ALSO
101  
102 The general functional perl documentation.
103  
104 L<FP::List>, L<FP::Lazy>
105  
106 =head1 NOTE
107  
108 This is alpha software! Read the status section in the package README
109 or on the L<website|http://functional-perl.org/>.
110  
111 =cut
112  
113 package FP::Stream;
114 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 41 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
117 41 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 41 our @EXPORT = qw(
120 is_null
121 Keep
122 Weakened
123 stream_iota
124 stream_range
125 stream_step_range
126 stream_length
127 stream_append
128 stream_map
129 stream_map_with_tail
130 stream_filter
131 stream_filter_with_tail
132 stream_fold
133 stream_foldr1
134 stream_fold_right
135 stream_state_fold_right
136 stream__array_fold_right
137 stream__string_fold_right
138 stream__subarray_fold_right stream__subarray_fold_right_reverse
139 stream_sum
140 array_to_stream stream
141 subarray_to_stream subarray_to_stream_reverse
142 string_to_stream
143 stream_to_string
144 stream_strings_join
145 stream_for_each
146 stream_drop
147 stream_take
148 stream_take_while
149 stream_slice
150 stream_drop_while
151 stream_ref
152 stream_zip2
153 stream_zip
154 stream_zip_with
155 stream_to_array
156 stream_to_purearray
157 stream_to_list
158 stream_sort
159 stream_group
160 stream_mixed_flatten
161 stream_mixed_fold_right
162 stream_mixed_state_fold_right
163 stream_any
164 stream_show
165 );
166 41 our @EXPORT_OK = qw(
167 F weaken
168 cons car cdr first rest
169 stream_cartesian_product
170 stream_cartesian_product_2
171 );
172 41 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
173  
174 41 ▶use FP::Lazy;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 41 ▶use FP::List ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
176 41 ▶use FP::Combinators qw(flip flip2of3 rot3right rot3left);
Subroutine BEGIN: called CC=1 SCAR=0.0
177 41 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 41 ▶use FP::Weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
179 41 ▶use FP::Predicates 'is_natural0';
Subroutine BEGIN: called CC=1 SCAR=0.0
180 41 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
181 41 ▶use FP::fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 41 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 41 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 41 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
185  
186 42 ▶sub stream_iota {
Subroutine stream_iota: called CC=4 SCAR=13.9
Pod undocumented
187 42 ▶ @_ <= 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ <= 2 or fp_croak_arity(2) 42 0 42
188 42 my ($maybe_start, $maybe_n) = @_;
189 42 ▶ my $start = $maybe_start // 0;
Condition100%2/2
$maybe_start // 0
l!l
2319
MC/DC100%1/1
$maybe_start // 0
$maybe_start
Truth table100%2/2
A
A= $maybe_start
Aresult
11
00
190 42 ▶ if (defined $maybe_n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_n) { } 12 30 42
191 12 my $end = $start + $maybe_n;
192 12 my $rec;
193 55 ▶ $rec = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
194 55 my ($i) = @_;
195 55 my $rec = $rec;
196 54 ▶ lazy {
Subroutine __ANON__: called CC=2 SCAR=6.9
197 54 ▶ if ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) { } 43 11 54
198 43 cons($i, &$rec($i + 1))
199 } else {
200 11 null
201 }
202 }
203 12 };
204 12 @_ = ($start);
205 12 goto &{ Weakened $rec};
206 } else {
207 30 my $rec;
208 364 ▶ $rec = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
209 364 my ($i) = @_;
210 364 my $rec = $rec;
211 334 ▶ lazy {
Subroutine __ANON__: called CC=1 SCAR=0.0
212 334 cons($i, &$rec($i + 1))
213 }
214 30 };
215 30 @_ = ($start);
216 30 goto &{ Weakened $rec};
217 }
218 }
219  
220 # Like perl's `..`
221 3 ▶sub stream_range {
Subroutine stream_range: called CC=4 SCAR=14.9
Pod undocumented
222 3 ▶ @_ <= 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ <= 2 or fp_croak_arity(2) 3 0 3
223 3 my ($maybe_start, $maybe_end) = @_;
224 3 ▶ my $start = $maybe_start // 0;
Condition50%1/2
$maybe_start // 0
l!l
30
MC/DC0%0/1
$maybe_start // 0
$maybe_start
Truth table50%1/2
A
A= $maybe_start
Aresult
11
00
225 3 ▶ stream_iota($start, defined $maybe_end ? $maybe_end + 1 - $start : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $maybe_end ? : 3 0 3
226 }
227  
228 1 ▶TEST { stream_range(2, 4)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
229 41 [2, 3, 4];
230 1 ▶TEST { stream_range(2, 2)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
231 41 [2];
232 1 ▶TEST { stream_range(2, 1)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
233 41 [];
234  
235 4 ▶sub stream_step_range {
Subroutine stream_step_range: called CC=5 SCAR=16.1
Pod undocumented
236 4 ▶ (@_ >= 1 and @_ <= 3) or fp_croak_arity "1-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 3 or fp_croak_arity("1-3") 4 0 4
Condition33%1/3
@_ >= 1 and @_ <= 3
!ll&&!rl&&r
004
MC/DC0%0/2
@_ >= 1 and @_ <= 3
@_ >= 1 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 3
ABresult
0X0
100
111
237 4 my ($step, $maybe_start, $maybe_end) = @_;
238 4 ▶ my $start = $maybe_start // 0;
Condition100%2/2
$maybe_start // 0
l!l
31
MC/DC100%1/1
$maybe_start // 0
$maybe_start
Truth table100%2/2
A
A= $maybe_start
Aresult
11
00
239 4 my $inverse = $step < 0;
240 4 ▶ if (defined $maybe_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_end) { } 3 1 4
241 3 my $end = $maybe_end;
242 3 my $rec;
243 11 ▶ $rec = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
244 11 my ($i) = @_;
245 11 my $rec = $rec;
246 11 ▶ lazy {
Subroutine __ANON__: called CC=3 SCAR=11.0
247 11 ▶ if ($inverse ? $i >= $end : $i <= $end) {
Branch: 2 branches
BranchTrueFalseTotal
if ($inverse ? $i >= $end : $i <= $end) { } 8 3 11
$inverse ? : 4 7 11
248 8 cons($i, &$rec($i + $step))
249 } else {
250 3 null
251 }
252 }
253 3 };
254 3 @_ = ($start);
255 3 goto &{ Weakened $rec};
256 } else {
257 1 my $rec;
258 5 ▶ $rec = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
259 5 my ($i) = @_;
260 5 my $rec = $rec;
261 4 ▶ lazy {
Subroutine __ANON__: called CC=1 SCAR=0.0
262 4 cons($i, &$rec($i + $step))
263 }
264 1 };
265 1 @_ = ($start);
266 1 goto &{ Weakened $rec};
267 }
268 }
269  
270 1 ▶TEST { stream_step_range(-1)->take(4)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
271 41 [0, -1, -2, -3];
272 1 ▶TEST { stream_step_range(2, 3, 6)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
273 41 [3, 5];
274 1 ▶TEST { stream_step_range(2, 3, 7)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
275 41 [3, 5, 7];
276 1 ▶TEST { stream_step_range(-1, 3, 1)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
277 41 [3, 2, 1];
278  
279 sub stream_length;
280 41 *stream_length = FP::List::make_length(1);
281  
282 41 *FP::List::List::stream_length = \&stream_length;
283  
284 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
285 1 my $s = stream_iota->take(10);
286 1 my $l = $s->length;
287 1 [$l, $s]
288 }
289 41 [10, undef];
290  
291 # left fold, sometimes called `foldl` (or with changes, `reduce`,
292 # which is defined in FP::Abstract::Sequence)
293  
294 # All functional languages seem to agree that right fold should pass
295 # the state argument as the second argument (to the right) to the
296 # build function (i.e. f(element,state)). But they don't when it comes
297 # to left fold: Scheme (SRFI-1) still calls f(element,state), whereas
298 # Haskell and Ocaml call f(state,element), perhaps according to the
299 # logic that both the walking order (which element of the list should
300 # be combined with the original state first?) and the argument order
301 # should be flipped, perhaps to try make them visually accordant. But
302 # then Ocaml also flips the start and input argument order in
303 # `fold_left`, so this one is different anyway. And Clojure calls it
304 # `reduce`, and has a special case when only two arguments are
305 # provided. Kinda hopeless?
306  
307 # The author of this library decided to stay with the Scheme
308 # tradition: it seems to make sense to keep the argument order the
309 # same (as determined by the type of the arguments) for both kinds of
310 # fold. If nothing more this allows `*cons` to be passed as $fn
311 # directly to construct a list.
312  
313 11 ▶sub stream_fold {
Subroutine stream_fold: called CC=3 SCAR=11.0
Pod undocumented
314 11 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 11 0 11
315 11 my ($fn, $start, $l) = @_;
316 11 weaken $_[2];
317 11 my $v;
318 LP: {
319 11 $l = force $l;
320 61 ▶ if (is_pair $l) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_pair($l)) 50 11 61
321 50 ($v, $l) = first_and_rest $l;
322 50 $start = &$fn($v, $start);
323 50 redo LP;
324 }
325 }
326 $start
327 11 }
328  
329 41 *FP::List::List::stream_fold = rot3left \&stream_fold;
330  
331 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
332 1 ▶ stream_fold sub { $_[0] + $_[1] }, 5, stream_iota(10, 2)
Subroutine __ANON__: called CC=1 SCAR=0.0
333 }
334 41 5 + 10 + 11;
335  
336 # and actually argument order dependent tests:
337  
338 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
339 1 ▶ stream_fold sub { [@_] }, 0, stream(1, 2)
Subroutine __ANON__: called CC=1 SCAR=0.0
340 }
341 41 [2, [1, 0]];
342  
343 1 ▶TEST { stream_fold(\&cons, null, stream(1, 2))->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
344 41 [2, 1];
345  
346 3 ▶sub stream_sum {
Subroutine stream_sum: called CC=2 SCAR=7.0
Pod undocumented
347 3 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3 0 3
348 3 my ($s) = @_;
349 3 weaken $_[0];
350 3 ▶ stream_fold(sub { $_[0] + $_[1] }, 0, $s)
Subroutine __ANON__: called CC=1 SCAR=0.0
351 }
352  
353 # XXX sum is in FP::Abstract::Sequence now, although not weakening.. soo ?
354 41 *FP::List::List::stream_sum = \&stream_sum;
355  
356 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
357 1 ▶ stream_iota->map(sub { $_[0] * $_[0] })->take(5)->sum
Subroutine __ANON__: called CC=1 SCAR=0.0
358 }
359 41 30;
360  
361 # leak test: XXX make this into a LEAK_TEST or so form(s), and env var, ok?
362 #TEST{ stream_iota->rest->take(100000)->sum }
363 # 5000050000;
364  
365 32 ▶sub stream_append2 {
Subroutine stream_append2: called CC=2 SCAR=6.9
Pod undocumented
366 32 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 32 0 32
367 32 my ($l1, $l2) = @_;
368 32 weaken $_[0];
369 32 weaken $_[1];
370 31 ▶ lazyT {
Subroutine __ANON__: called CC=2 SCAR=6.9
371 31 $l1 = force $l1;
372 31 ▶ is_null($l1) ? $l2 : cons(car($l1), stream_append(cdr($l1), $l2))
Branch: 1 branch
BranchTrueFalseTotal
is_null($l1) ? : 8 23 31
373 }
374 32 "FP::List::List"
375 }
376  
377 32 ▶sub stream_append {
Subroutine stream_append: called CC=5 SCAR=16.1
Pod undocumented
378 32 ▶ if (@_ == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 0) { } 1 31 32
elsif (@_ == 1) { } 1 30 31
elsif (@_ == 2) { } 28 2 30
379 1 null
380 } elsif (@_ == 1) {
381 1 $_[0]
382 } elsif (@_ == 2) {
383 28 goto \&stream_append2
384 } else {
385 2 my $ss = list(reverse @_);
386 2 weaken $_ for @_;
387  
388 # ^ WHEN is this even needed, I think it is when calling a
389 # stream consumer afterwards, right?
390 $ss->rest->fold(
391 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
392 4 my ($s, $rest) = @_;
393 4 ▶ lazyT {
Subroutine __ANON__: called CC=1 SCAR=0.0
394 4 stream_append2($s, $rest)
395 }
396 4 "FP::List::List"
397 },
398 2 $ss->first
399 )
400 }
401 }
402  
403 41 *FP::List::List::stream_append = \&stream_append;
404  
405 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
406 1 stream_to_string(stream_append string_to_stream("Hello"),
407 string_to_stream(" World"))
408 }
409 41 'Hello World';
410  
411 # F is defined further down, thus call with parens
412 1 ▶TEST { F(stream_append list(qw(a b)), list(qw(c d)), list(qw(e f))) }
Subroutine __ANON__: called CC=1 SCAR=0.0
413 1 ▶GIVES { list('a', 'b', 'c', 'd', 'e', 'f') };
Subroutine __ANON__: called CC=1 SCAR=0.0
414 1 ▶TEST { F(stream_append list(qw(a b)), list(qw(c d))) }
Subroutine __ANON__: called CC=1 SCAR=0.0
415 1 ▶GIVES { list('a', 'b', 'c', 'd') };
Subroutine __ANON__: called CC=1 SCAR=0.0
416 1 ▶TEST { F(stream_append list(qw(a b))) }
Subroutine __ANON__: called CC=1 SCAR=0.0
417 1 ▶GIVES { list('a', 'b') };
Subroutine __ANON__: called CC=1 SCAR=0.0
418 1 ▶TEST { F(stream_append) }
Subroutine __ANON__: called CC=1 SCAR=0.0
419 41 list();
420  
421 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
422 1 my @a;
423 my $s = stream_append(
424 1 ▶ lazy { push @a, "a"; list(qw(a b)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
425 1 ▶ lazy { push @a, "b"; list(qw(c d)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
426 1 ▶ lazy { push @a, "c"; list(qw(e f)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
427 1 );
428 1 my @r;
429 1 F(Keep($s)->take(1));
430 1 push @r, ["take 1", @a];
431 1 F(Keep($s)->take(2));
432 1 push @r, ["take 2", @a];
433 1 F(Keep($s)->take(3));
434 1 push @r, ["take 3", @a];
435 1 F(Keep($s)->take(4));
436 1 push @r, ["take 4", @a];
437 1 F(Keep($s)->take(5));
438 1 push @r, ["take 5", @a];
439 1 F(Keep($s)->take(6));
440 1 push @r, ["take 6", @a];
441 \@r
442 1 }
443 [
444 41 ["take 1", "a"],
445 ["take 2", "a"],
446 ["take 3", "a", "b"],
447 ["take 4", "a", "b"],
448 ["take 5", "a", "b", "c"],
449 ["take 6", "a", "b", "c"]
450 ];
451  
452 483 ▶sub stream_map {
Subroutine stream_map: called CC=2 SCAR=7.0
Pod undocumented
453 483 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 483 0 483
454 483 my ($fn, $l) = @_;
455 483 weaken $_[1];
456 437 ▶ lazyT {
Subroutine __ANON__: called CC=2 SCAR=6.9
457 437 $l = force $l;
458 437 ▶ is_null($l) ? null : cons(&$fn(car $l), stream_map($fn, cdr $l))
Branch: 1 branch
BranchTrueFalseTotal
is_null($l) ? : 66 371 437
459 }
460 483 "FP::List::List"
461 }
462  
463 41 *FP::List::List::stream_map = flip \&stream_map;
464  
465 4 ▶sub stream_map_with_tail {
Subroutine stream_map_with_tail: called CC=2 SCAR=7.0
Pod undocumented
466 4 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 4 0 4
467 4 my ($fn, $l, $tail) = @_;
468 4 weaken $_[1];
469 4 ▶ lazyT {
Subroutine __ANON__: called CC=2 SCAR=6.9
470 4 $l = force $l;
471 4 ▶ is_null($l)
Branch: 1 branch
BranchTrueFalseTotal
is_null($l) ? : 1 3 4
472 ? $tail
473 : cons(&$fn(car $l), stream_map_with_tail($fn, cdr($l), $tail))
474 }
475 4 "FP::Abstract::Sequence"
476 }
477  
478 41 *FP::List::List::stream_map_with_tail = flip2of3 \&stream_map_with_tail;
479  
480 # 2-ary (possibly slightly faster) version of stream_zip
481 4 ▶sub stream_zip2 {
Subroutine stream_zip2: called CC=4 SCAR=14.1
Pod undocumented
482 4 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 4 0 4
483 4 my ($l, $m) = @_;
484 8 ▶ do { weaken $_ if is_promise $_ }
Branch: 1 branch
BranchTrueFalseTotal
if is_promise($_) 8 0 8
485 4 for @_; #needed?
486 4 ▶ lazyT {
Subroutine __ANON__: called CC=3 SCAR=11.0
487 4 $l = force $l;
488 4 $m = force $m;
489 4 ▶ (is_null $l or is_null $m)
Branch: 1 branch
BranchTrueFalseTotal
is_null($l) || is_null($m) ? : 1 3 4
Condition66%2/3
is_null($l) or is_null($m)
l!l&&r!l&&!r
013
MC/DC50%1/2
is_null($l) or is_null($m)
is_null($l) is_null($m)
Truth table66%2/3
A or B
A= is_null($l)B= is_null($m)
ABresult
1X1
011
000
490 ? null
491 : cons([car($l), car($m)], stream_zip2(cdr($l), cdr($m)))
492 }
493 4 "FP::List::List"
494 }
495  
496 41 *FP::List::List::stream_zip2 = \&stream_zip2;
497  
498 # n-ary version of stream_zip2
499 3 ▶sub stream_zip {
Subroutine stream_zip: called CC=3 SCAR=11.0
Pod undocumented
500 3 my @ps = @_;
501 6 ▶ do { weaken $_ if is_promise $_ }
Branch: 1 branch
BranchTrueFalseTotal
if is_promise($_) 0 6 6
502 3 for @_; #needed?
503 3 ▶ lazyT {
Subroutine __ANON__: called CC=2 SCAR=6.9
504 my @vs = map {
505 3 my $v = force $_;
506 5 ▶ is_null($v) ? return null : $v
Branch: 1 branch
BranchTrueFalseTotal
is_null($v) ? : 2 3 5
507 } @ps;
508 1 my $a = [map { car $_ } @vs];
509 1 my $b = stream_zip(map { cdr $_ } @vs);
510 1 cons($a, $b)
511 }
512 3 "FP::List::List"
513 }
514  
515 41 *FP::List::List::stream_zip = \&stream_zip; # XX fall back on zip2
516 # for 2 arguments?
517  
518 5222 ▶sub stream_zip_with {
Subroutine stream_zip_with: called CC=1 SCAR=0.2
Pod undocumented
519 5222 my ($f, $l1, $l2) = @_;
520 5222 weaken $_[1];
521 5222 weaken $_[2];
522 5110 ▶ lazyT {
Subroutine __ANON__: called CC=3 SCAR=12.5
523 5110 my $l1 = force $l1;
524 5110 my $l2 = force $l2;
525 5110 ▶ (is_null $l1 or is_null $l2) ? null : cons &$f(car($l1), car($l2)),
Branch: 1 branch
BranchTrueFalseTotal
is_null($l1) || is_null($l2) ? : 0 5110 5110
Condition33%1/3
is_null($l1) or is_null($l2)
l!l&&r!l&&!r
005110
MC/DC0%0/2
is_null($l1) or is_null($l2)
is_null($l1) is_null($l2)
Truth table33%1/3
A or B
A= is_null($l1)B= is_null($l2)
ABresult
1X1
011
000
526 stream_zip_with($f, cdr($l1), cdr($l2))
527 }
528 5222 "FP::List::List"
529 }
530  
531 41 *FP::List::List::stream_zip_with = flip2of3 \&stream_zip_with;
532  
533 sub stream_filter;
534 41 *stream_filter = FP::List::make_filter(1);
535 41 *FP::List::List::stream_filter = flip \&stream_filter;
536  
537 sub stream_filter_with_tail;
538 41 *stream_filter_with_tail = FP::List::make_filter_with_tail(1);
539 41 *FP::List::List::stream_filter_with_tail = flip2of3 \&stream_filter_with_tail;
540  
541 # http://hackage.haskell.org/package/base-4.7.0.2/docs/Prelude.html#v:foldr1
542  
543 # foldr1 is a variant of foldr that has no starting value argument,
544 # and thus must be applied to non-empty lists.
545  
546 6 ▶sub stream_foldr1 {
Subroutine stream_foldr1: called CC=2 SCAR=8.1
Pod undocumented
547 6 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 6 0 6
548 6 my ($fn, $l) = @_;
549 6 weaken $_[1];
550 4 ▶ lazy {
Subroutine __ANON__: called CC=3 SCAR=15.1
551 4 $l = force $l;
552 4 ▶ if (is_pair $l) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_pair($l)) { } 4 0 4
elsif (is_null($l)) { } 0 0 0
553 4 &$fn(car($l), stream_foldr1($fn, cdr($l)))
554 } elsif (is_null $l) {
555 0 die "foldr1: reached end of list"
556 } else {
557 0 die "improper list: $l"
558 }
559 }
560 6 }
561  
562 41 *FP::List::List::stream_foldr1 = flip \&stream_foldr1;
563  
564 6 ▶sub stream_fold_right {
Subroutine stream_fold_right: called CC=2 SCAR=7.1
Pod undocumented
565 6 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 6 0 6
566 6 my ($fn, $start, $l) = @_;
567 6 weaken $_[2];
568 6 ▶ lazy {
Subroutine __ANON__: called CC=3 SCAR=11.3
569 6 $l = force $l;
570 6 ▶ if (is_pair $l) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_pair($l)) { } 5 1 6
elsif (is_null($l)) { } 1 0 1
571 5 &$fn(car($l), stream_fold_right($fn, $start, cdr $l))
572 } elsif (is_null $l) {
573 1 $start
574 } else {
575 0 die "improper list: $l"
576 }
577 }
578 6 }
579  
580 41 *FP::List::List::stream_fold_right = rot3left \&stream_fold_right;
581  
582 41 *FP::List::List::stream_preferred_fold = \&FP::List::List::stream_fold_right;
583  
584 98 ▶sub make_stream__fold_right {
Subroutine make_stream__fold_right: called CC=1 SCAR=0.0
Pod undocumented
585 98 my ($length, $ref, $start, $d, $whileP) = @_;
586  
587 214 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
588 214 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 214 0 214
589 214 my ($fn, $tail, $a) = @_;
590 214 my $len = &$length($a);
591 214 my $rec;
592 765 ▶ $rec = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
593 765 my ($i) = @_;
594 765 my $rec = $rec;
595 734 ▶ lazy {
Subroutine __ANON__: called CC=2 SCAR=6.9
596 734 ▶ if (&$whileP($i, $len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$whileP($i, $len)) { } 551 183 734
597 551 &$fn(&$ref($a, $i), &$rec($i + $d))
598 } else {
599 183 $tail
600 }
601 }
602 214 };
603 214 &{ Weakened $rec}($start)
604 }
605 98 }
606  
607 708 ▶our $lt = sub { $_[0] < $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
608 0 ▶our $gt = sub { $_[0] > $_[1] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
609 200 ▶our $array_length = sub { scalar @{ $_[0] } };
Subroutine __ANON__: called CC=1 SCAR=0.0
610 463 ▶our $array_ref = sub { $_[0][$_[1]] };
Subroutine __ANON__: called CC=1 SCAR=0.0
611 14 ▶our $string_length = sub { length $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
612 88 ▶our $string_ref = sub { substr $_[0], $_[1], 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
613  
614 sub stream__array_fold_right;
615 41 *stream__array_fold_right
616 = make_stream__fold_right($array_length, $array_ref, 0, 1, $lt);
617  
618 # XX export these array functions as methods to ARRAY library
619  
620 sub stream__string_fold_right;
621 41 *stream__string_fold_right
622 = make_stream__fold_right($string_length, $string_ref, 0, 1, $lt);
623  
624 9 ▶sub stream__subarray_fold_right {
Subroutine stream__subarray_fold_right: called CC=3 SCAR=11.0
Pod undocumented
625 9 ▶ @_ == 5 or fp_croak_arity 5;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 5 or fp_croak_arity(5) 9 0 9
626 9 my ($fn, $tail, $a, $start, $maybe_end) = @_;
627 make_stream__fold_right($array_length, $array_ref, $start, 1,
628 9 ▶ defined $maybe_end ? sub { $_[0] < $_[1] and $_[0] < $maybe_end } : $lt)
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
defined $maybe_end ? : 3 6 9
$_[0] < $_[1] and $_[0] < $maybe_end 9 1 10
629 ->($fn, $tail, $a);
630 }
631  
632 7 ▶sub stream__subarray_fold_right_reverse {
Subroutine stream__subarray_fold_right_reverse: called CC=3 SCAR=11.2
Pod undocumented
633 7 ▶ @_ == 5 or fp_croak_arity 5;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 5 or fp_croak_arity(5) 7 0 7
634 7 my ($fn, $tail, $a, $start, $maybe_end) = @_;
635 make_stream__fold_right($array_length, $array_ref, $start, -1,
636 defined $maybe_end
637 5 ▶ ? sub { $_[0] >= 0 and $_[0] > $maybe_end }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= 0 and $_[0] > $maybe_end 5 0 5
638 7 ▶ : sub { $_[0] >= 0 })->($fn, $tail, $a);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined $maybe_end ? : 2 5 7
639 }
640  
641 97 ▶sub array_to_stream {
Subroutine array_to_stream: called CC=4 SCAR=17.1
Pod undocumented
642 97 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 97 0 97
Condition33%1/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
0097
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
643 97 my ($a, $maybe_tail) = @_;
644 97 ▶ stream__array_fold_right(\&cons, $maybe_tail // null, $a)
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
0970
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
645 }
646  
647 55 ▶sub stream {
Subroutine stream: called CC=1 SCAR=0.0
Pod undocumented
648 55 array_to_stream [@_]
649 }
650  
651 7 ▶sub subarray_to_stream {
Subroutine subarray_to_stream: called CC=4 SCAR=17.1
Pod undocumented
652 7 ▶ @_ >= 2 and @_ <= 4 or fp_croak_arity "2-4";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 4 or fp_croak_arity("2-4") 7 0 7
Condition33%1/3
@_ >= 2 and @_ <= 4
!ll&&!rl&&r
007
MC/DC0%0/2
@_ >= 2 and @_ <= 4
@_ >= 2 @_ <= 4
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 4
ABresult
0X0
100
111
653 7 my ($a, $start, $maybe_end, $maybe_tail) = @_;
654 7 ▶ stream__subarray_fold_right(\&cons, $maybe_tail // null,
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
070
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
655 $a, $start, $maybe_end)
656 }
657  
658 5 ▶sub subarray_to_stream_reverse {
Subroutine subarray_to_stream_reverse: called CC=4 SCAR=17.1
Pod undocumented
659 5 ▶ @_ >= 2 and @_ <= 4 or fp_croak_arity "2-4";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 4 or fp_croak_arity("2-4") 5 0 5
Condition33%1/3
@_ >= 2 and @_ <= 4
!ll&&!rl&&r
005
MC/DC0%0/2
@_ >= 2 and @_ <= 4
@_ >= 2 @_ <= 4
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 4
ABresult
0X0
100
111
660 5 my ($a, $start, $maybe_end, $maybe_tail) = @_;
661 5 ▶ stream__subarray_fold_right_reverse(\&cons, $maybe_tail // null,
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
050
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
662 $a, $start, $maybe_end)
663 }
664  
665 14 ▶sub string_to_stream {
Subroutine string_to_stream: called CC=4 SCAR=17.1
Pod undocumented
666 14 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 14 0 14
Condition33%1/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
0014
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
667 14 my ($str, $maybe_tail) = @_;
668 14 ▶ stream__string_fold_right(\&cons, $maybe_tail // null, $str)
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
0140
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
669 }
670  
671 15 ▶sub stream_to_string {
Subroutine stream_to_string: called CC=3 SCAR=11.0
Pod undocumented
672 15 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 15 0 15
673 15 my ($l) = @_;
674 15 weaken $_[0];
675 15 my $str = "";
676 15 ▶ while (($l = force $l), !is_null $l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l = force($l), !is_null($l)) 65 15 80
677 65 $str .= car $l;
678 65 $l = cdr $l;
679 }
680 $str
681 15 }
682  
683 41 *FP::List::List::stream_string = \&stream_to_string;
684  
685 1 ▶TEST { stream("Ha", "ll", "o")->string } "Hallo";
Subroutine __ANON__: called CC=1 SCAR=0.0
686  
687 5 ▶sub stream_strings_join {
Subroutine stream_strings_join: called CC=2 SCAR=7.0
Pod undocumented
688 5 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 5 0 5
689 5 my ($l, $val) = @_;
690 5 weaken $_[0];
691  
692 # XX can't I just use list_strings_join? no, the other way round
693 # would work.
694  
695 # depend on FP::Array. Lazily, for depencency cycle?
696 5 require FP::Array;
697 5 FP::Array::array_strings_join(stream_to_array($l), $val);
698 }
699  
700 41 *FP::List::List::stream_strings_join = \&stream_strings_join;
701  
702 1 ▶TEST { stream(1, 2, 3)->strings_join("-") }
Subroutine __ANON__: called CC=1 SCAR=0.0
703 41 "1-2-3";
704  
705 sub stream_for_each;
706 41 *stream_for_each = FP::List::make_for_each(1, 0);
707 sub stream_for_each_with_islast;
708 41 *stream_for_each_with_islast = FP::List::make_for_each(1, 1);
709  
710 41 *FP::List::List::stream_for_each = flip \&stream_for_each;
711  
712 0 ▶sub stream_drop {
Subroutine stream_drop: not called CC=4 SCAR=30.0
Pod undocumented
713 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
714 0 my ($s, $n) = @_;
715 0 weaken $_[0];
716 0 ▶ while ($n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) 0 0 0
717 0 $s = force $s;
718 0 ▶ die "stream too short" if is_null $s;
Branch: 1 branch
BranchTrueFalseTotal
if is_null($s) 0 0 0
719 0 $s = cdr $s;
720 0 $n--
721 }
722 $s
723 0 }
724  
725 41 *FP::List::List::stream_drop = \&stream_drop;
726  
727 281 ▶sub stream_take {
Subroutine stream_take: called CC=2 SCAR=7.0
Pod undocumented
728 281 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 281 0 281
729 281 my ($s, $n) = @_;
730 281 weaken $_[0];
731 280 ▶ lazyT {
Subroutine __ANON__: called CC=3 SCAR=11.0
732 280 ▶ if ($n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) { } 253 27 280
733 253 $s = force $s;
734 253 ▶ is_null($s) ? $s : cons(car($s), stream_take(cdr($s), $n - 1));
Branch: 1 branch
BranchTrueFalseTotal
is_null($s) ? : 0 253 253
735 } else {
736 27 null
737 }
738 }
739 281 "FP::List::List"
740 }
741  
742 41 *FP::List::List::stream_take = \&stream_take;
743  
744 79 ▶sub stream_take_while {
Subroutine stream_take_while: called CC=2 SCAR=6.9
Pod undocumented
745 79 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 79 0 79
746 79 my ($fn, $s) = @_;
747 79 weaken $_[1];
748 72 ▶ lazyT {
Subroutine __ANON__: called CC=3 SCAR=11.0
749 72 $s = force $s;
750 72 ▶ if (is_null $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($s)) { } 2 70 72
751 2 null
752 } else {
753 70 my $a = car $s;
754 70 ▶ if (&$fn($a)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$fn($a)) { } 69 1 70
755 69 cons $a, stream_take_while($fn, cdr $s)
756 } else {
757 1 null
758 }
759 }
760 }
761 79 "FP::List::List"
762 }
763  
764 41 *FP::List::List::stream_take_while = flip \&stream_take_while;
765  
766 3 ▶sub stream_slice {
Subroutine stream_slice: called CC=2 SCAR=7.0
Pod undocumented
767 3 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 3 0 3
768 3 my ($start, $end) = @_;
769 3 weaken $_[0];
770 3 weaken $_[1];
771 3 $end = force $end;
772 3 my $rec;
773 6 ▶ $rec = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
774 6 my ($s) = @_;
775 6 weaken $_[0];
776 6 my $rec = $rec;
777 6 ▶ lazyT {
Subroutine __ANON__: called CC=3 SCAR=11.2
778 6 $s = force $s;
779 6 ▶ if (is_null $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($s)) { } 0 6 6
780 0 $s # null
781 } else {
782 6 ▶ if ($s eq $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq $end) { } 3 3 6
783 3 null
784 } else {
785 3 cons car($s), &$rec(cdr $s)
786 }
787 }
788 }
789 6 "FP::List::List"
790 3 };
791 3 @_ = ($start);
792 3 goto &{ Weakened $rec};
793 }
794  
795 41 *FP::List::List::stream_slice = \&stream_slice;
796  
797 # maybe call it `cut_at` instead?
798  
799 2 ▶sub stream_drop_while {
Subroutine stream_drop_while: called CC=2 SCAR=6.9
Pod undocumented
800 2 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 2 0 2
801 2 my ($pred, $s) = @_;
802 2 weaken $_[1];
803 2 ▶ lazyT {
Subroutine __ANON__: called CC=3 SCAR=11.0
804 LP: {
805 2 $s = force $s;
806 17 ▶ if (!is_null $s and &$pred(car $s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not is_null($s) and &$pred(car($s))) { } 15 2 17
Condition100%3/3
not is_null($s) and &$pred(car($s))
!ll&&!rl&&r
1115
MC/DC100%2/2
not is_null($s) and &$pred(car($s))
not is_null($s) &$pred(car($s))
Truth table100%3/3
not(A) and B
A= not is_null($s)B= &$pred(car($s))
ABresult
0X0
100
111
807 15 $s = cdr $s;
808 15 redo LP;
809 } else {
810 2 $s
811 }
812 }
813 }
814 2 "FP::Abstract::Sequence"
815 }
816  
817 41 *FP::List::List::stream_drop_while = flip \&stream_drop_while;
818  
819 sub stream_ref;
820 41 *stream_ref = FP::List::make_ref(1);
821 41 *FP::List::List::stream_ref = \&stream_ref;
822  
823 8 ▶sub exn (&) {
Subroutine exn: called CC=3 SCAR=12.0
Pod undocumented
824 8 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 8 0 8
825 8 my ($thunk) = @_;
826 0 ▶ eval { &$thunk(); '' } // do { $@ =~ /(.*?) at/; $1 }
Condition33%1/3
eval { do { &$thunk(); "" } } // do { $@ =~ /(.*?) at/; $1 }
l!l&&r!l&&!r
080
MC/DC0%0/2
eval { do { &$thunk(); "" } } // do { $@ =~ /(.*?) at/; $1 }
eval { do { &$thunk(); "" } } do { $@ =~ /(.*?) at/; $1 }
Truth table33%1/3
A // B
A= eval { do { &$thunk(); "" } }B= do { $@ =~ /(.*?) at/; $1 }
ABresult
1X1
011
000
827 }
828  
829 82 ▶sub t_ref {
Subroutine t_ref: called CC=1 SCAR=0.0
Pod undocumented
830 82 my ($list, $cons, $liststream) = @_;
831 2 ▶ TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
832 2 my $l = &$list(qw(a b));
833 2 my $il = &$cons("x", "y");
834 [
835 2 ▶ Keep($l)->ref(0), Keep($l)->ref(1), exn { Keep($l)->ref(-1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
836 2 ▶ exn { Keep($l)->ref(0.1) }, exn { Keep($l)->ref(2) },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
837 Keep($il)->ref(0),
838 2 ▶ exn { Keep($il)->ref(1) }
Subroutine __ANON__: called CC=1 SCAR=0.0
839 2 ]
840 }
841 [
842 82 "a", "b",
843 "invalid index: -1",
844 "invalid index: '0.1'",
845 "requested element 2 of $liststream of length 2",
846 "x", "improper $liststream"
847 ];
848 }
849  
850 41 t_ref \&list, \&cons, "list";
851 1 ▶t_ref \&stream, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
852 1 my ($a, $r) = @_;
853 1 ▶ lazy { cons $a, $r }
Subroutine __ANON__: called CC=1 SCAR=0.0
854 1 }, "stream";
855  
856 1 ▶TEST { list_ref cons(0, stream(1, 2, 3)), 2 } 2;
Subroutine __ANON__: called CC=1 SCAR=0.0
857 1 ▶TEST { stream_ref cons(0, stream(1, 2, 3)), 2 } 2;
Subroutine __ANON__: called CC=1 SCAR=0.0
858  
859 # force everything deeply
860 304 ▶sub F {
Subroutine F: called CC=8 SCAR=20.9
Pod undocumented
861 304 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 304 0 304
862 304 my ($v) = @_;
863  
864 #weaken $_[0]; since I usually use it interactively, and should
865 # only be good for short sequences, better don't
866 304 ▶ if (is_promise $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_promise($v)) { } 67 237 304
867 67 F(force $v);
868 } else {
869 237 ▶ if (length(my $r = ref $v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $r = ref $v)) { } 143 94 237
870 143 ▶ if (is_pair $v) {
Branch: 4 branches
BranchTrueFalseTotal
if (is_pair($v)) { } 103 40 143
elsif (is_null($v)) { } 35 5 40
elsif ($r eq 'ARRAY') { } 3 2 5
elsif (UNIVERSAL::isa($v, 'ARRAY')) { } 2 0 2
871 103 cons(F(car $v), F(cdr $v))
872 } elsif (is_null $v) {
873 35 $v
874 } elsif ($r eq "ARRAY") {
875 3 [map { F($_) } @$v]
876 } elsif (UNIVERSAL::isa($v, "ARRAY")) {
877 2 bless [map { F($_) } @$v], ref $v
878 } else {
879 0 $v
880 }
881 } else {
882 94 $v
883 }
884 }
885 }
886  
887 59 ▶sub stream_to_array {
Subroutine stream_to_array: called CC=3 SCAR=11.0
Pod undocumented
888 59 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 59 0 59
889 59 my ($l) = @_;
890 59 weaken $_[0];
891 59 my $res = [];
892 59 my $i = 0;
893 59 $l = force $l;
894 59 ▶ while (!is_null $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_null($l)) 205 58 263
895 205 my $v = car $l;
896 205 $$res[$i] = $v;
897 205 $l = force cdr $l;
898 204 $i++;
899 }
900 $res
901 58 }
902  
903 41 *FP::List::List::stream_array = \&stream_to_array;
904  
905 7 ▶sub stream_to_purearray {
Subroutine stream_to_purearray: called CC=1 SCAR=0.0
Pod undocumented
906 7 my ($l) = @_;
907 7 weaken $_[0];
908 7 my $a = stream_to_array $l;
909 7 require FP::PureArray;
910 7 FP::PureArray::array_to_purearray($a)
911 }
912  
913 41 *FP::List::List::stream_purearray = \&stream_to_purearray;
914  
915 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
916 3 ▶ stream(1, 3, 4)->purearray->map (sub { $_[0]**2 })
Subroutine __ANON__: called CC=1 SCAR=0.0
917 1 }
918 41 bless [1, 9, 16], "FP::_::PureArray";
919  
920 10 ▶sub stream_to_list {
Subroutine stream_to_list: called CC=2 SCAR=7.0
Pod undocumented
921 10 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 10 0 10
922 10 my ($l) = @_;
923 10 weaken $_[0];
924 10 array_to_list stream_to_array $l
925 }
926  
927 41 *FP::List::List::stream_list = \&stream_to_list;
928  
929 4 ▶sub stream_sort {
Subroutine stream_sort: called CC=3 SCAR=11.4
Pod undocumented
930 4 ▶ @_ == 1 or @_ == 2 or fp_croak_arity "1 or 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or fp_croak_arity("1 or 2") 4 0 4
Condition66%2/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
130
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
931 4 my ($l, $maybe_cmp) = @_;
932 4 stream_to_purearray($l)->sort ($maybe_cmp)
933 }
934  
935 41 *FP::List::List::stream_sort = \&stream_sort;
936  
937 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
938 1 require FP::Ops;
939 1 stream(5, 3, 8, 4)->sort (\&FP::Ops::real_cmp)->array
940 }
941 41 [3, 4, 5, 8];
942  
943 1 ▶TEST { ref(stream(5, 3, 8, 4)->sort (\&FP::Ops::real_cmp)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
944 41 'FP::_::PureArray'; # XX ok? Need to `->stream` if a stream is needed
945  
946 1 ▶TEST { stream(5, 3, 10, 8, 4)->sort (\&FP::Ops::real_cmp)->stream->car }
Subroutine __ANON__: called CC=1 SCAR=0.0
947 41 3;
948  
949 # but then PureArray has `first`, too, if that's all you need.
950  
951 1 ▶TEST { stream(5, 3, 10, 8, 4)->sort->stream->car }
Subroutine __ANON__: called CC=1 SCAR=0.0
952 41 10; # the default is string sort
953  
954 # add a lazy merge sort instead/in addition?
955  
956 sub stream_group;
957 41 *stream_group = FP::List::make_group(1);
958  
959 1 ▶sub FP::List::List::stream_group {
Subroutine stream_group: called CC=3 SCAR=11.8
960 1 __
961 'group($self, $equal, $maybe_tail): build groups of subsequent items that are $equal.';
962 1 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 1 0 1
Condition33%1/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
001
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
963 1 my ($self, $equal, $maybe_tail) = @_;
964 1 weaken $_[0];
965 1 stream_group($equal, $self, $maybe_tail)
966 }
967  
968 sub stream_split;
969 41 *stream_split = FP::List::make_split(1);
970  
971 139 ▶sub stream_mixed_flatten {
Subroutine stream_mixed_flatten: called CC=5 SCAR=20.1
Pod undocumented
972 139 ▶ @_ >= 1 and @_ <= 3 or fp_croak_arity "1-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 3 or fp_croak_arity("1-3") 139 0 139
Condition33%1/3
@_ >= 1 and @_ <= 3
!ll&&!rl&&r
00139
MC/DC0%0/2
@_ >= 1 and @_ <= 3
@_ >= 1 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 3
ABresult
0X0
100
111
973 139 my ($v, $maybe_tail, $maybe_delay) = @_;
974  
975 #XXX needed, no? weaken $_[0] if ref $_[0];
976 139 ▶ mixed_flatten($v, $maybe_tail // null, $maybe_delay || \&lazyLight)
Condition40%2/5
$maybe_tail // &null()
l!l&&r!l&&!r
01390
$maybe_delay || \&FP::Stream::lazyLight
l!l
0139
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
MC/DC0%0/1
$maybe_delay || \&FP::Stream::lazyLight
$maybe_delay
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
Truth table50%1/2
A
A= $maybe_delay
Aresult
11
00
977 }
978  
979 41 *FP::List::List::stream_mixed_flatten = \&stream_mixed_flatten;
980  
981 30 ▶sub stream_any {
Subroutine stream_any: called CC=6 SCAR=18.3
Pod undocumented
982 30 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2 or 3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2 or 3") 30 0 30
Condition66%2/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
13170
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table66%2/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
983 30 my ($pred, $l, $prev_false) = @_;
984 30 weaken $_[1];
985 30 $l = force $l;
986 30 ▶ if (is_pair $l) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_pair($l)) { } 20 10 30
elsif (is_null($l)) { } 10 0 10
987 20 my $v = $pred->(car $l);
988 20 ▶ $v or do {
Branch: 1 branch
BranchTrueFalseTotal
$v or do { my $r = cdr($l); stream_any($pred, $r, $v) } 3 17 20
989 17 my $r = cdr $l;
990 17 stream_any($pred, $r, $v)
991 }
992 } elsif (is_null $l) {
993 10 $prev_false
994 } else {
995 0 die "improper list: $l"
996 }
997 }
998  
999 41 *FP::List::List::stream_any = flip \&stream_any;
1000  
1001 # (meant as a debugging tool: turn stream to string)
1002 0 ▶sub stream_show {
Subroutine stream_show: not called CC=2 SCAR=17.9
Pod undocumented
1003 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
1004 0 my ($s) = @_;
1005 0 join("", map {" '$_'\n"} @{ stream_to_array $s })
1006 }
1007  
1008 41 *FP::List::List::stream_show = \&stream_show;
1009  
1010 # A stateful fold: collect state while going to the end (starting from
1011 # the left). Then it's basically a fold_right.
1012  
1013 103 ▶sub stream_state_fold_right {
Subroutine stream_state_fold_right: called CC=2 SCAR=6.9
Pod undocumented
1014 103 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 103 0 103
1015 103 my ($fn, $stateupfn, $s) = @_;
1016 101 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1017 101 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 101 0 101
1018 101 my ($statedown) = @_;
1019 41 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1020 101 FORCE $s;
1021 101 ▶ if (is_null $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($s)) { } 35 66 101
1022 35 @_ = ($statedown);
1023 35 goto &$stateupfn
1024 } else {
1025 66 my ($v, $s) = $s->first_and_rest;
1026 66 @_ = ($v, $statedown, stream_state_fold_right($fn, $stateupfn, $s));
1027 66 goto &$fn;
1028 }
1029 }
1030 103 }
1031  
1032 41 *FP::List::List::stream_state_fold_right = rot3left \&stream_state_fold_right;
1033  
1034 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1035 stream_state_fold_right(
1036 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1037 2 my ($v, $statedown, $restfn) = @_;
1038 2 [$v, &$restfn($statedown . ".")]
1039 },
1040 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1041 1 my ($statedown) = @_;
1042 1 $statedown . "end"
1043 },
1044 1 stream(3, 4)
1045 )->("start")
1046 }
1047 41 [3, [4, "start..end"]];
1048  
1049 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1050 stream_state_fold_right(
1051 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1052 2 my ($v, $statedown, $restfn) = @_;
1053 2 cons $v, &$restfn($statedown)
1054 },
1055 1 ▶ sub { $_[0] }, # \&identity
Subroutine __ANON__: called CC=1 SCAR=0.0
1056 1 stream(3, 4)
1057 )->(list 5, 6)->array
1058 }
1059 41 [3, 4, 5, 6];
1060  
1061 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1062 stream_state_fold_right(
1063 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1064 2 my ($v, $statedown, $restfn) = @_;
1065 2 cons $v, &$restfn(cons $v, $statedown)
1066 },
1067 1 ▶ sub { $_[0] }, # \&identity
Subroutine __ANON__: called CC=1 SCAR=0.0
1068 1 stream(3, 4)
1069 )->(list 5, 6)->array
1070 }
1071 41 [3, 4, 4, 3, 5, 6];
1072  
1073 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1074 stream_state_fold_right(
1075 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1076 4 my ($v, $statedown, $restfn) = @_;
1077 3 ▶ lazy {
Subroutine __ANON__: called CC=1 SCAR=0.0
1078 3 cons [$v, $statedown], &$restfn($statedown + 1)
1079 }
1080 4 },
1081 undef, # \&identity, but never reached
1082 1 stream_iota
1083 )->(10)->take(3)->array
1084 }
1085 41 [[0, 10], [1, 11], [2, 12]];
1086  
1087 # modified test from above
1088 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1089 stream_iota->state_fold_right(
1090 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1091 4 my ($v, $statedown, $restfn) = @_;
1092 3 ▶ lazy {
Subroutine __ANON__: called CC=1 SCAR=0.0
1093 3 cons [$v, $statedown], &$restfn($statedown + 1)
1094 }
1095 4 },
1096 undef, # \&identity, but never reached
1097 1 )->(10)->take(3)->array
1098 }
1099 41 [[0, 10], [1, 11], [2, 12]];
1100  
1101 # are these warranted?: (or should they be deprecated right upon
1102 # introduction?)
1103  
1104 0 ▶sub stream_mixed_fold_right {
Subroutine stream_mixed_fold_right: not called CC=3 SCAR=24.8
Pod undocumented
1105 0 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 0 0 0
1106 0 my ($fn, $state, $v) = @_;
1107 0 ▶ weaken $_[2] if ref $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[2] 0 0 0
1108 0 @_ = ($fn, $state, stream_mixed_flatten $v);
1109 0 goto \&stream_fold_right
1110 }
1111  
1112 41 *FP::List::List::stream_mixed_fold_right = rot3left \&stream_mixed_fold_right;
1113  
1114 32 ▶sub stream_mixed_state_fold_right {
Subroutine stream_mixed_state_fold_right: called CC=3 SCAR=11.3
Pod undocumented
1115 32 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 32 0 32
1116 32 my ($fn, $statefn, $v) = @_;
1117 32 ▶ weaken $_[2] if ref $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[2] 32 0 32
1118 32 @_ = ($fn, $statefn, stream_mixed_flatten $v);
1119 32 goto \&stream_state_fold_right
1120 }
1121  
1122 41 *FP::List::List::stream_mixed_state_fold_right
1123 = rot3left \&stream_mixed_state_fold_right;
1124  
1125 # 'cross product'
1126  
1127 9 ▶sub stream_cartesian_product_2 {
Subroutine stream_cartesian_product_2: called CC=2 SCAR=6.9
Pod undocumented
1128 9 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 9 0 9
1129 9 my ($a, $orig_b) = @_;
1130 9 weaken $_[0];
1131 9 weaken $_[1];
1132 9 my $rec;
1133 69 ▶ $rec = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1134 69 my ($a, $b) = @_;
1135 69 my $rec = $rec;
1136 69 ▶ lazyT {
Subroutine __ANON__: called CC=3 SCAR=11.0
1137 69 ▶ if (is_null $a) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_null($a)) { } 9 60 69
elsif (is_null($b)) { } 18 42 60
1138 9 null
1139 } elsif (is_null $b) {
1140 18 &$rec(cdr($a), $orig_b);
1141 } else {
1142 42 cons(cons(car($a), car($b)), &$rec($a, cdr $b))
1143 }
1144 }
1145 69 "FP::List::List"
1146 9 };
1147 9 Weakened($rec)->($a, $orig_b)
1148 }
1149  
1150 41 *FP::List::List::stream_cartesian_product_2 = \&stream_cartesian_product_2;
1151  
1152 1 ▶TEST { F stream_cartesian_product_2 list("A", "B"), list(list(1), list(2)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1153 41 list(list('A', 1), list('A', 2), list('B', 1), list('B', 2));
1154  
1155 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1156 1 F stream_cartesian_product_2 list("E", "F"),
1157 stream_cartesian_product_2 list("C", "D"),
1158 list(list("A"), list("B"))
1159 }
1160 41 list(
1161 list("E", "C", "A"),
1162 list("E", "C", "B"),
1163 list("E", "D", "A"),
1164 list("E", "D", "B"),
1165 list("F", "C", "A"),
1166 list("F", "C", "B"),
1167 list("F", "D", "A"),
1168 list("F", "D", "B")
1169 );
1170  
1171 11 ▶sub stream_cartesian_product {
Subroutine stream_cartesian_product: called CC=4 SCAR=14.1
Pod undocumented
1172 11 my @v = @_;
1173 11 weaken $_ for @_;
1174 11 ▶ if (!@v) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @v) { } 0 11 11
elsif (@v == 1) { } 5 6 11
1175 0 die "stream_cartesian_product: need at least 1 argument"
1176 } elsif (@v == 1) {
1177 5 stream_map \&list, $v[0]
1178 } else {
1179 6 my $first = shift @v;
1180 6 stream_cartesian_product_2($first, stream_cartesian_product(@v))
1181 }
1182 }
1183  
1184 41 *FP::List::List::stream_cartesian_product = \&stream_cartesian_product;
1185  
1186 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
1187 1 write_sexpr stream_cartesian_product list("A", "B"), list("C", "D"),
1188 list("E", "F")
1189 }
1190 41 '(("A" "C" "E") ("A" "C" "F") ("A" "D" "E") ("A" "D" "F") ("B" "C" "E")'
1191 . ' ("B" "C" "F") ("B" "D" "E") ("B" "D" "F"))';
1192 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
1193 1 write_sexpr stream_cartesian_product list("A", "B"), list("C", "D"),
1194 list("E")
1195 }
1196 41 '(("A" "C" "E") ("A" "D" "E") ("B" "C" "E") ("B" "D" "E"))';
1197 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
1198 1 write_sexpr stream_cartesian_product list("A", "B"), list("C", "D")
1199 }
1200 41 '(("A" "C") ("A" "D") ("B" "C") ("B" "D"))';
1201  
1202 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
1203 1 write_sexpr stream_cartesian_product list("A", "B")
1204 }
1205 41 '(("A") ("B"))';
1206  
1207 1 ▶TEST { F stream(1, 2)->cartesian_product(list 3, 4) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1208 41 list(list(1, 3), list(1, 4), list(2, 3), list(2, 4));
1209  
1210 # chunksOf from Haskell's Data.List.Split, but returns `purearray`s
1211 # instead of lists.
1212  
1213 164 ▶sub make_chunks_of {
Subroutine make_chunks_of: called CC=1 SCAR=0.0
Pod undocumented
1214 164 my ($strictly, $lazy) = @_;
1215 7 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1216 7 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 7 0 7
1217 7 my ($s, $chunklen) = @_;
1218 7 ▶ $chunklen >= 1 or croak "invalid chunklen: $chunklen";
Branch: 1 branch
BranchTrueFalseTotal
$chunklen >= 1 or croak("invalid chunklen: $chunklen") 7 0 7
1219 7 ▶ weaken $_[0] if $lazy; # although tie down is only chunk sized
Branch: 1 branch
BranchTrueFalseTotal
if $lazy 3 4 7
1220 7 require FP::PureArray;
1221 fix(
1222 19 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1223 19 my ($rec, $s) = @_;
1224 19 ▶ weaken $_[0] if $lazy; # although tie down is only chunk sized
Branch: 1 branch
BranchTrueFalseTotal
if $lazy 8 11 19
1225 19 ▶ lazy_if {
Subroutine __ANON__: called CC=5 SCAR=16.1
1226 19 ▶ return null if $s->is_null;
Branch: 1 branch
BranchTrueFalseTotal
if $s->is_null 5 14 19
1227 14 my @v;
1228 14 for my $i (1 .. $chunklen) {
1229 82 ▶ if ($s->is_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->is_null) { } 40 42 82
1230 40 ▶ die "premature end of input" if $strictly;
Branch: 1 branch
BranchTrueFalseTotal
if $strictly 2 38 40
1231 } else {
1232 42 push @v, $s->first;
1233 42 $s = $s->rest;
1234 }
1235 }
1236 12 cons FP::PureArray::array_to_purearray(\@v), &$rec($s)
1237 }
1238 19 $lazy
1239 }
1240 7 )->($s)
1241 }
1242 164 }
1243  
1244 # Do we still want functions?
1245 #*stream_chunks_of = flip make_chunks_of(0);
1246 #*stream_strictly_chunks_of = flip make_chunks_of(0);
1247  
1248 41 *FP::List::List::chunks_of = make_chunks_of(0, 0);
1249 41 *FP::List::List::strictly_chunks_of = make_chunks_of(1, 0);
1250 41 *FP::List::List::stream_chunks_of = make_chunks_of(0, 1);
1251 41 *FP::List::List::stream_strictly_chunks_of = make_chunks_of(1, 1);
1252  
1253 1 ▶TEST { list(qw(a b c d e f))->chunks_of(2) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1254 1 ▶GIVES {
Subroutine __ANON__: called CC=1 SCAR=0.0
1255 1 require FP::PureArray;
1256 1 import FP::PureArray;
1257 1 list(purearray('a', 'b'), purearray('c', 'd'), purearray('e', 'f'))
1258 41 };
1259 1 ▶TEST { stream(qw(a b c d e f))->chunks_of(3)->F }
Subroutine __ANON__: called CC=1 SCAR=0.0
1260 1 ▶GIVES {
Subroutine __ANON__: called CC=1 SCAR=0.0
1261 1 list(purearray('a', 'b', 'c'), purearray('d', 'e', 'f'))
1262 41 };
1263 1 ▶TEST { list(qw(a b c d e f))->chunks_of(4) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1264 1 ▶GIVES {
Subroutine __ANON__: called CC=1 SCAR=0.0
1265 1 list(purearray('a', 'b', 'c', 'd'), purearray('e', 'f'))
1266 41 };
1267 1 ▶TEST { list(qw(a b c d e f))->chunks_of(40) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1268 1 ▶GIVES {
Subroutine __ANON__: called CC=1 SCAR=0.0
1269 1 list(purearray('a', 'b', 'c', 'd', 'e', 'f'))
1270 41 };
1271 1 ▶TEST_EXCEPTION { list(qw(a b c d e f))->strictly_chunks_of(4)->F }
Subroutine __ANON__: called CC=1 SCAR=0.0
1272 41 'premature end of input';
1273  
1274 # ----- Tests ----------------------------------------------------------
1275  
1276 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1277 1 ▶ stream_any sub { $_[0] % 2 }, array_to_stream [2, 4, 8]
Subroutine __ANON__: called CC=1 SCAR=0.0
1278 }
1279 41 0;
1280 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=1.2
1281 0 ▶ stream_any sub { $_[0] % 2 }, array_to_stream []
Subroutine __ANON__: not called CC=1 SCAR=1.2
1282 }
1283 41 undef;
1284 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1285 1 ▶ stream_any sub { $_[0] % 2 }, array_to_stream [2, 5, 8]
Subroutine __ANON__: called CC=1 SCAR=0.0
1286 }
1287 41 1;
1288 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1289 1 ▶ stream_any sub { $_[0] % 2 }, array_to_stream [7]
Subroutine __ANON__: called CC=1 SCAR=0.0
1290 }
1291 41 1;
1292  
1293 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1294 1 my @v;
1295 3 ▶ stream_for_each sub { push @v, @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
1296 1 ▶ stream_map sub { my $v = shift; $v * $v }, array_to_stream [10, 11, 13];
Subroutine __ANON__: called CC=1 SCAR=0.0
1297 \@v
1298 1 }
1299 41 [100, 121, 169];
1300  
1301 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1302 1 my @v;
1303 5 ▶ stream_for_each sub { push @v, @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
1304 stream_map_with_tail(
1305 3 ▶ sub { my $v = shift; $v * $v },
Subroutine __ANON__: called CC=1 SCAR=0.0
1306 1 array_to_stream([10, 11, 13]),
1307 array_to_stream([1, 2])
1308 );
1309 \@v
1310 1 }
1311 41 [100, 121, 169, 1, 2];
1312  
1313 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1314 1 ▶ stream_to_array stream_filter sub { $_[0] % 2 }, stream_iota 0, 5;
Subroutine __ANON__: called CC=1 SCAR=0.0
1315 }
1316 41 [1, 3];
1317  
1318 # write_sexpr( stream_take( stream_iota (0, 1000000000), 2))
1319 # -> ("0" "1")
1320  
1321 1 ▶TEST { stream_to_array stream_zip cons(2, null), cons(1, null) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1322 41 [[2, 1]];
1323 1 ▶TEST { stream_to_array stream_zip cons(2, null), null }
Subroutine __ANON__: called CC=1 SCAR=0.0
1324 41 [];
1325  
1326 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1327 list_to_array F stream_zip2(
1328 1 ▶ stream_map(sub { $_[0] + 10 }, stream_iota(0, 5)),
Subroutine __ANON__: called CC=1 SCAR=0.0
1329 stream_iota(0, 3))
1330 }
1331 41 [[10, 0], [11, 1], [12, 2]];
1332  
1333 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1334 1 ▶ stream_to_array stream_take_while sub { my ($x) = @_; $x < 2 }, stream_iota
Subroutine __ANON__: called CC=1 SCAR=0.0
1335 }
1336 41 [0, 1];
1337  
1338 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1339 11 ▶ stream_to_array stream_take stream_drop_while(sub { $_[0] < 10 },
Subroutine __ANON__: called CC=1 SCAR=0.0
1340 1 stream_iota()), 3
1341 }
1342 41 [10, 11, 12];
1343  
1344 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1345 1 ▶ stream_to_array stream_drop_while(sub { $_[0] < 10 }, stream_iota(0, 5))
Subroutine __ANON__: called CC=1 SCAR=0.0
1346 }
1347 41 [];
1348  
1349 1 ▶TEST { join("", @{ stream_to_array(string_to_stream("You're great.")) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1350 41 'You\'re great.';
1351  
1352 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1353 1 stream_to_string stream__subarray_fold_right(
1354 \&cons,
1355 string_to_stream("World"),
1356 [split //, "Hello"],
1357 3, undef
1358 )
1359 }
1360 41 'loWorld';
1361  
1362 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1363 1 stream_to_string stream__subarray_fold_right(
1364 \&cons,
1365 string_to_stream("World"),
1366 [split //, "Hello"],
1367 3, 4
1368 )
1369 }
1370 41 'lWorld';
1371  
1372 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1373 1 stream_to_string stream__subarray_fold_right_reverse \&cons,
1374 cons("W", null), [split //, "Hello"], 1,
1375 undef
1376 }
1377 41 'eHW';
1378  
1379 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1380 1 stream_to_string stream__subarray_fold_right_reverse \&cons,
1381 cons("W", null), [split //, "Hello"], 2, 0
1382 }
1383 41 'leW'; # hmm really? exclusive lower boundary?
1384  
1385 1 ▶TEST { stream_to_string subarray_to_stream [split //, "Hello"], 1, 3 }
Subroutine __ANON__: called CC=1 SCAR=0.0
1386 41 'el';
1387  
1388 1 ▶TEST { stream_to_string subarray_to_stream [split //, "Hello"], 1, 99 }
Subroutine __ANON__: called CC=1 SCAR=0.0
1389 41 'ello';
1390  
1391 1 ▶TEST { stream_to_string subarray_to_stream [split //, "Hello"], 2 }
Subroutine __ANON__: called CC=1 SCAR=0.0
1392 41 'llo';
1393  
1394 1 ▶TEST { stream_to_string subarray_to_stream_reverse [split //, "Hello"], 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
1395 41 'eH';
1396  
1397 1 ▶TEST { stream_to_string subarray_to_stream_reverse [split //, "Hello"], 1, 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
1398 41 'e'
1399 ; # dito. BTW it's consistent at least, $start not being 'after the element'(?) either.
1400  
1401 1 ▶TEST { my $s = stream_iota; stream_to_array stream_slice $s, $s }
Subroutine __ANON__: called CC=1 SCAR=0.0
1402  
1403 # XX: warns about "Reference is already weak"
1404 41 [];
1405 1 ▶TEST { my $s = stream_iota; stream_to_array stream_slice $s, cdr $s }
Subroutine __ANON__: called CC=1 SCAR=0.0
1406 41 [0];
1407 1 ▶TEST { my $s = stream_iota; stream_to_array stream_slice cdr($s), cdddr $s }
Subroutine __ANON__: called CC=1 SCAR=0.0
1408 41 [1, 2];
1409  
1410 # OO interface:
1411  
1412 1 ▶TEST { string_to_stream("Hello")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
1413 41 "Hello";
1414  
1415 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1416 1 my $s = string_to_stream "Hello";
1417 1 my $ss = $s->force;
1418 1 $ss->string
1419 }
1420 41 "Hello";
1421  
1422 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1423 3 ▶ array_to_stream([1, 2, 3])->map(sub { $_[0] + 1 })
Subroutine __ANON__: called CC=1 SCAR=0.0
1424 1 ▶ ->fold(sub { $_[0] + $_[1] }, 0)
Subroutine __ANON__: called CC=1 SCAR=0.0
1425 }
1426 41 9;
1427  
1428 # variable life times:
1429  
1430 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1431 1 my $s = string_to_stream "Hello";
1432 1 my $ss = $s->force;
1433  
1434 # dispatching to list_to_string
1435 1 $ss->string;
1436 1 is_pair $ss
1437 }
1438 41 1;
1439  
1440 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1441 1 my $s = string_to_stream "Hello";
1442 1 stream_to_string $s;
1443 1 defined $s
1444 }
1445 41 '';
1446  
1447 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1448 1 my $s = string_to_stream "Hello";
1449 1 $s->stream_string;
1450 1 defined $s
1451 }
1452 41 '';
1453  
1454 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1455 1 my $s = string_to_stream "Hello";
1456  
1457 # still dispatching to stream_string thanks to hack in
1458 # Lazy.pm
1459 1 $s->string;
1460 1 defined $s
1461 }
1462 41 '';
1463  
1464 # Lazyness check, and be-careful reminder: (example from SYNOPSIS in
1465 # Lazy.pm)
1466  
1467 {
1468 41 my $tot;
1469 my $l;
1470 1 ▶ TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1471 1 $tot = 0;
1472 3 ▶ $l = stream_map sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1473 3 my ($x) = @_;
1474 3 $tot += $x;
1475 3 $x * $x
1476 1 }, list(5, 7, 8);
1477 1 $tot
1478 }
1479 41 0;
1480 1 ▶ TEST { [$l->first, $tot] }
Subroutine __ANON__: called CC=1 SCAR=0.0
1481 41 [25, 5];
1482 1 ▶ TEST { [$l->length, $tot] }
Subroutine __ANON__: called CC=1 SCAR=0.0
1483 41 [3, 20];
1484 }
1485  
1486 41 1
« meta/FunctionalPerl/ModuleList.pm ↑ Summary lib/PXML/Util.pm »