lib/FP/List.pm

filter: Statementstmt 91.5% 1335 / 1459 Branchbran 65.4% 356 / 544 Conditioncond 53.3% 96 / 180 MC/DCmcdc 25.4% 28 / 110 Subroutinesub 91.8% 381 / 415 Podpod 0.0% 0 / 106 totaltotal 78.0% 2196 / 2814 CC 335 SCAR 14.7
CC
335
Cov
82%
list_filtermap
72.0
improper_filtermap
16.2
__ANON__
13.0
CRAP 4.3
« lib/FP/Weak.pm ↑ Summary blib/lib/FP/Trie/t.pm »
1 #
2 # Copyright (c) 2013-2023 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::List - singly linked (purely functional) lists
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Div qw(inc square); use FP::Ops qw(div); use FP::Equal 'is_equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use FP::Combinators qw(flip);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use FP::Predicates qw(is_pure);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶ use FP::List ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 my $l = cons("H", cons("e", cons("l", cons("l", cons("o", null)))));
23 is_equal $l, list("H", "e", "l", "l", "o");
24 is_equal list_to_string($l), "Hello";
25 ok is_pure $l;
26  
27 is_equal list(1,2,3)->map(sub{ $_[0] * $_[0] }),
28 list(1,4,9);
29 is_equal list(1,2,3)->map(\&square)->array,
30 [1,4,9];
31  
32 is list(qw(a b c))->first, "a";
33 is_equal list(qw(a b c))->rest,
34 list("b", "c");
35  
36 is list(1,2,3,4)->sum, 1+2+3+4;
37 is list(1,2,3,4)->product, 1*2*3*4;
38 is list(2,4,6)->reduce(flip \&div), 2/4/6;
39 is list(2,4,6)->reduce_right(flip \&div), 2/4/6;
40 # etc.
41  
42 # The `cons` function checks whether its second argument is an object
43 # with a `cons` method, if so, it invokes it, otherwise it creates an
44 # FP::List::Pair object holding both values (there's also a `pair`
45 # function that doesn't check for a method and always directly
46 # creates the pair)
47 is cons("a","b")->rest, "b";
48 is cons("a","b")->cdr, "b";
49 is list(5,6,7)->caddr, 7;
50  
51  
52 =head1 DESCRIPTION
53  
54 Purely functional (immutable) singly linked lists are interesting in
55 functional programs because they can be extended and walked directly
56 via recursion. They do not offer efficient random access (O(len)),
57 also there is a constant space overhead and access indirection
58 compared to arrays. They are most appropriate for maintaining smaller
59 but frequently updated chains, for example maintaining a link chain to
60 parent scopes while recursing into a tree datastructure (which, if
61 it's a pure data structure, doesn't have parent links built into it).
62  
63 FP::List does not enforce its pairs to only contain pairs or null in
64 their rest (cdr) position. Which means that they may end in something
65 else than a null (and operations encountering these will die with
66 "improper list"). The `show` function (or the `:s`
67 mode in `FP::Repl::Repl`) displays those as `improper_list`, e.g.:
68  
69 # a normal, 'proper', list:
70 is_equal cons(5, cons(6, cons(7, null))), list(5, 6, 7);
71  
72 # an 'improper' list:
73 is_equal cons(5, cons(6, 7)), improper_list(5, 6, 7);
74  
75 Note that destruction of linked lists in Perl requires space on the C
76 stack proportional to their length. You should either avoid dropping a
77 long linked list at once (dropping it one cell at a time intermixed
78 with doing any other operation avoids the issue), or will want to
79 increase the C stack size limit, lest your program will segfault.
80  
81  
82 =head1 PURITY
83  
84 `FP::List` cells are created to be immutable by default, which
85 enforces the functional purity of the API. This can be disabled by
86 setting `$FP::List::immutable` to false when creating lists; slots in
87 pairs can then be mutated. Only ever use this during development (),
88 if at all; if you need to update sequences in the middle efficiently ,
89 use another data structure (like L<FP::Vec>).
90  
91 In either case, `FP::List` implements `FP::Abstract::Pure` (`is_pure`
92 from `FP::Predicates` returns true).
93  
94  
95 =head1 NAMING
96  
97 Most functional programming languages are using either the `:` or `::`
98 operator to prepend an item to a list. The name `cons` comes from
99 lisps, where it's the basic (lisp = list processing!) "construction"
100 function.
101  
102 Cons cells (pairs) in lisps can also be used to build other data
103 structures than lists: they don't enforce the rest slot to be a pair
104 or null. Lisps traditionally use `car` and `cdr` as accessors for the
105 two fields, to respect this feature, and also because 'a' and 'd'
106 combine easily into composed names like `caddr`. This library offers
107 `car` and `cdr` as aliases to `first` and `rest`.
108  
109 Some languages call the accessors `head` and `tail`, but `tail` would
110 conflict with `Sub::Call::Tail`, hence those are not used here.
111  
112  
113 =head1 SEE ALSO
114  
115 Implements: L<FP::Abstract::Pure>, L<FP::Abstract::Sequence>,
116 L<FP::Abstract::Equal>, L<FP::Abstract::Show>
117  
118 L<FP::Stream>, L<FP::Array>, L<FP::PureArray>
119  
120 =head1 NOTE
121  
122 This is alpha software! Read the status section in the package README
123 or on the L<website|http://functional-perl.org/>.
124  
125 =cut
126  
127 package FP::List;
128 60 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 60 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
131 60 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 60 our @EXPORT = qw(
134 cons cons_ is_pair null is_null is_pair_of is_pair_or_null
135 list_of nonempty_list_of is_null_or_pair_of null_or_pair_of is_list
136 car cdr first rest
137 car_and_cdr first_and_rest perhaps_first_and_rest
138 list);
139 60 our @EXPORT_OK = qw(
140 pair improper_list improper_map improper_filtermap improper_last
141 first_set first_update
142 is_pair_noforce is_null_noforce
143 unsafe_cons unsafe_car unsafe_cdr
144 string_to_list list_length list_reverse list_reverse_with_tail
145 list_to_string list_to_array rlist_to_array
146 list_to_values rlist_to_values
147 write_sexpr
148 array_to_list array_to_list_reverse mixed_flatten
149 list_strings_join list_strings_join_reverse
150 list_filter list_map list_filtermap list_mapn list_map_with_islast
151 list_map_with_index_ list_map_with_index
152 list_fold list_fold_right list_to_perlstring
153 unfold unfold_right
154 list_pair_fold_right
155 list_butlast list_drop_while list_rtake_while list_take_while
156 list_rtake_while_and_rest list_take_while_and_rest
157 list_append
158 list_zip2
159 list_alist
160 list_last
161 list_every list_all list_any list_none
162 list_perhaps_find_tail list_perhaps_find
163 list_find_tail list_find
164 list_insertion_variants
165 list_merge
166 cartesian_product_2
167 cartesian_product
168 is_charlist ldie
169 cddr
170 cdddr
171 cddddr
172 cadr
173 caddr
174 cadddr
175 caddddr
176 c_r
177 list_ref
178 list_perhaps_one
179 list_sort
180 list_drop
181 list_take
182 list_slice
183 list_group
184 circularlist
185 weaklycircularlist
186 );
187 60 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
188  
189 60 ▶use 5.008; # for Internals::SvREADONLY
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 60 ▶use FP::Lazy;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 60 ▶use FP::Lazy qw(force_noeval);
Subroutine BEGIN: called CC=1 SCAR=0.0
193 60 ▶use Chj::xperlfunc qw(xprint xprintln);
Subroutine BEGIN: called CC=1 SCAR=0.0
194 60 ▶use FP::Combinators qw(flip flip2of3 rot3right rot3left);
Subroutine BEGIN: called CC=1 SCAR=0.0
195 60 ▶use FP::Optional qw(perhaps_to_maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
196 60 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 60 ▶use FP::Predicates qw(is_natural0 either is_natural complement is_even is_zero);
Subroutine BEGIN: called CC=1 SCAR=0.0
198 60 ▶use FP::Div qw(inc dec);
Subroutine BEGIN: called CC=1 SCAR=0.0
199 60 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
200 60 ▶use Scalar::Util qw(weaken blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
201 60 ▶use FP::Weak qw(Weakened);
Subroutine BEGIN: called CC=1 SCAR=0.0
202 60 ▶use FP::Interfaces;
Subroutine BEGIN: called CC=1 SCAR=0.0
203 60 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
204 60 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
205 60 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207 60 our $immutable = 1; # whether pairs are to be made immutable
208  
209 #use FP::Array 'array_fold_right'; can't, recursive dependency XX (see copy below)
210 #(Chj::xIOUtil triggers it)
211  
212 60 package FP::List::List {
213 60 ▶ use FP::Lazy;
Subroutine BEGIN: called CC=1 SCAR=0.0
214 60 ▶ use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
215  
216 60 ▶ use Chj::NamespaceCleanAbove;
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 60 *null = \&FP::List::null;
219  
220 12982 ▶ sub pair_namespace {"FP::List::Pair"}
Subroutine pair_namespace: called CC=1 SCAR=0.0
221  
222 12982 ▶ sub cons {
Subroutine cons: called CC=3 SCAR=11.1
223 12982 my $s = shift;
224 12982 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 12982 0 12982
225 12982 my @p = ($_[0], $s);
226  
227 # Now it gets ~ugly: for lazy code, $s can (now, since
228 # AUTOLOAD on them doesn't necessarily force them anymore) now
229 # be a promise with field 2 set.
230 # my $immediate_class = ref($s);
231 # bless \@p,
232 # UNIVERSAL::isa($immediate_class, "FP::Lazy::AnyPromise")
233 # ? $$s[2]
234 # : $immediate_class;
235 # /ugly.
236  
237 # OR, simply (since the above would void any chance of simply
238 # using `lazyT` in stream libraries since one couldn't know
239 # the type of cons cells statically)!:
240  
241 12982 bless \@p, $s->pair_namespace;
242  
243 12982 ▶ if ($immutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FP::List::immutable) 12982 0 12982
244 12982 Internals::SvREADONLY $p[0], 1;
245 12982 Internals::SvREADONLY $p[1], 1;
246 }
247 12982 Internals::SvREADONLY @p, 1;
248 \@p
249 12982 }
250  
251 # return this sequence as a list, i.e. identity
252 0 ▶ sub list {
Subroutine list: not called CC=2 SCAR=17.9
253 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
254 0 $_[0]
255 }
256  
257 6 ▶ sub stream {
Subroutine stream: called CC=2 SCAR=7.0
258 6 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 6 0 6
259 6 my ($l) = @_;
260 6 ▶ lazy {$l}
Subroutine __ANON__: called CC=1 SCAR=0.0
261 6 }
262  
263 4 ▶ sub strictlist {
Subroutine strictlist: called CC=2 SCAR=7.1
264 4 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 4 0 4
265 4 my $s = shift;
266 4 FP::StrictList::strictlist($s->values)
267 }
268  
269 # sub purearray: see below, \&FP::List::List::purearray
270  
271 6 ▶ sub mutablearray {
Subroutine mutablearray: called CC=2 SCAR=7.1
272 6 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 6 0 6
273 6 my $s = shift;
274 6 FP::_::MutableArray->new_from_array([$s->values])
275 }
276  
277 0 ▶ sub preferred_fold {
Subroutine preferred_fold: not called CC=1 SCAR=6.9
278 0 my $s = shift;
279 0 $s->fold(@_)
280 }
281  
282 60 _END_
283 }
284  
285 60 package FP::List::Null {
286 60 ▶ use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
287 60 our @ISA = qw(FP::List::List);
288  
289 81 ▶ sub is_null {1}
Subroutine is_null: called CC=1 SCAR=0.0
290  
291 23 ▶ sub length {
Subroutine length: called CC=1 SCAR=0.0
292 23 0
293 }
294  
295 300 ▶ my $mkexn = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
296 300 my ($method) = @_;
297 6 ▶ sub { die "can't take the $method of the empty list" }
Subroutine __ANON__: called CC=1 SCAR=0.0
298 300 };
299 60 *first = &$mkexn("first");
300 60 *second = &$mkexn("second");
301 60 *rest = &$mkexn("rest");
302 60 *first_and_rest = &$mkexn("first_and_rest");
303 60 *butlast = &$mkexn("butlast");
304 0 ▶ sub maybe_first {undef}
Subroutine maybe_first: not called CC=1 SCAR=6.9
305 0 ▶ sub maybe_rest {undef}
Subroutine maybe_rest: not called CC=1 SCAR=6.9
306 0 ▶ sub perhaps_first { () }
Subroutine perhaps_first: not called CC=1 SCAR=6.9
307 0 ▶ sub perhaps_rest { () }
Subroutine perhaps_rest: not called CC=1 SCAR=6.9
308 1 ▶ sub perhaps_first_and_rest { () }
Subroutine perhaps_first_and_rest: called CC=1 SCAR=0.0
309  
310 0 ▶ sub FP_Equal_equal {
Subroutine FP_Equal_equal: not called CC=1 SCAR=6.9
311 0 my ($a, $b) = @_;
312 0 FP::List::is_null($b)
313  
314 # XX well, this is, *currently*, guaranteed by FP::Equal,
315 # thus always 1
316 }
317  
318 # for FP::Show:
319 0 ▶ sub FP_Show_show {
Subroutine FP_Show_show: not called CC=1 SCAR=6.9
320 0 my ($s, $show) = @_;
321 0 "list()"
322 }
323  
324 }
325  
326 60 package FP::List::Pair {
327 60 ▶ use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
328 60 our @ISA = qw(FP::List::List);
329  
330 460 ▶ sub is_null {''}
Subroutine is_null: called CC=1 SCAR=0.0
331  
332 1552 ▶ sub car {
Subroutine car: called CC=2 SCAR=6.9
333  
334 # $_[0][0]
335 # nope, since lazyT, the argument can be a promise:
336 1552 ▶ (ref($_[0]) eq __PACKAGE__ ? $_[0] : FP::List::force $_[0])->[0]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 1421 131 1552
337 }
338 60 *first = \&car;
339  
340 60 *maybe_first = \&first;
341 60 *perhaps_first = \&first;
342  
343 60 *first_set = \&FP::List::first_set;
344 60 *first_update = \&FP::List::first_update;
345  
346 3273 ▶ sub cdr {
Subroutine cdr: called CC=2 SCAR=6.9
347  
348 # $_[0][1]
349 # nope, since lazyT, the argument can be a promise:
350 3273 ▶ (ref($_[0]) eq __PACKAGE__ ? $_[0] : FP::List::force $_[0])->[1]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 3219 54 3273
351 }
352 60 *rest = \&cdr;
353 60 *maybe_rest = \&rest;
354 60 *perhaps_rest = \&rest;
355  
356 1306 ▶ sub car_and_cdr {
Subroutine car_and_cdr: called CC=2 SCAR=6.9
357 1306 ▶ @{ ref($_[0]) eq __PACKAGE__ ? $_[0] : FP::List::force $_[0] }
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 1285 21 1306
358 }
359 60 *first_and_rest = \&car_and_cdr;
360 60 *perhaps_first_and_rest = \&car_and_cdr;
361  
362 0 ▶ sub cddr { $_[0]->cdr->cdr }
Subroutine cddr: not called CC=1 SCAR=6.9
363 0 ▶ sub cdddr { $_[0]->cdr->cdr->cdr }
Subroutine cdddr: not called CC=1 SCAR=6.9
364 0 ▶ sub cddddr { $_[0]->cdr->cdr->cdr->cdr }
Subroutine cddddr: not called CC=1 SCAR=6.9
365  
366 1 ▶ sub cadr { $_[0]->cdr->car }
Subroutine cadr: called CC=1 SCAR=0.0
367 60 *second = \&cadr;
368 2 ▶ sub caddr { $_[0]->cdr->cdr->car }
Subroutine caddr: called CC=1 SCAR=0.0
369 0 ▶ sub cadddr { $_[0]->cdr->cdr->cdr->car }
Subroutine cadddr: not called CC=1 SCAR=6.9
370 0 ▶ sub caddddr { $_[0]->cdr->cdr->cdr->cdr->car }
Subroutine caddddr: not called CC=1 SCAR=6.9
371  
372 # Re `c_r`:
373 # Use AUTOLOAD to autogenerate instead? But be careful about the
374 # overhead of the then necessary DESTROY method.
375  
376 611 ▶ sub FP_Equal_equal {
Subroutine FP_Equal_equal: called CC=3 SCAR=11.0
377 611 my ($a, $b) = @_;
378 60 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
379 (
380 FP::List::is_pair($b)
381 and FP::Equal::equal($a->car, $b->car)
382 611 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
FP::List::is_pair($b) and FP::Equal::equal($a->car, $b->car) and do { @_ = ($a->cdr, $b->cdr); goto \&FP::Equal::equal } 603 8 611
Condition66%2/3
FP::List::is_pair($b) and FP::Equal::equal($a->car, $b->car)
!ll&&!rl&&r
08603
MC/DC50%1/2
FP::List::is_pair($b) and FP::Equal::equal($a->car, $b->car)
FP::List::is_pair($b) FP::Equal::equal($a->car, $b->car)
Truth table66%2/3
A and B
A= FP::List::is_pair($b)B= FP::Equal::equal($a->car, $b->car)
ABresult
0X0
100
111
383 603 @_ = ($a->cdr, $b->cdr);
384 603 goto \&FP::Equal::equal
385 }
386 )
387 }
388  
389 23 ▶ sub FP_Show_show {
Subroutine FP_Show_show: called CC=3 SCAR=11.0
390 23 my ($s, $show) = @_;
391  
392 # If there were no improper or lazy lists, this would do:
393 # "list(".$s->map($show)->strings_join(", ").")"
394  
395 23 my @v;
396 my $v;
397 LP: {
398 23 ($v, $s) = $s->first_and_rest;
399 41 push @v, &$show($v);
400 41 $s = FP::List::force_noeval($s);
401 41 ▶ if (FP::List::is_pair_noforce($s)) {
Branch: 2 branches
BranchTrueFalseTotal
if (FP::List::is_pair_noforce($s)) { } 18 23 41
elsif (FP::List::is_null_noforce($s)) { } 18 5 23
402 18 redo LP;
403 } elsif (FP::List::is_null_noforce($s)) {
404 18 "list(" . join(", ", @v) . ")"
405 } else {
406 5 push @v, &$show($s);
407 5 "improper_list(" . join(", ", @v) . ")"
408 }
409 }
410 }
411  
412 }
413  
414 60 ▶use FP::Equal;
Subroutine BEGIN: called CC=1 SCAR=0.0
415 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
416 1 equal(list(2, 3, 4), list(2, 3))
417 }
418 60 undef;
419 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
420 1 equal(list(2, 3, 4), list(2, 3, 4))
421 }
422 60 1;
423  
424 20486 ▶sub cons {
Subroutine cons: called CC=4 SCAR=13.9
Pod undocumented
425 20486 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 20486 0 20486
426 20486 ▶ if (defined blessed($_[1]) and my $f = $_[1]->can("cons")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($_[1]) and my $f = $_[1]->can('cons')) { } 12902 7584 20486
Condition100%3/3
defined builtin::blessed($_[1]) and my $f = $_[1]->can("cons")
!ll&&!rl&&r
59752512902
MC/DC100%2/2
defined builtin::blessed($_[1]) and my $f = $_[1]->can("cons")
defined builtin::blessed($_[1]) my $f = $_[1]->can("cons")
Truth table100%3/3
A and B
A= defined builtin::blessed($_[1])B= my $f = $_[1]->can("cons")
ABresult
0X0
100
111
427 12902 @_ = ($_[1], $_[0]);
428 12902 goto &$f;
429 } else {
430 7584 goto \&unsafe_cons
431 }
432 }
433  
434 4 ▶sub cons_ {
Subroutine cons_: called CC=2 SCAR=7.0
Pod undocumented
435 4 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 4 0 4
436 4 my ($item) = @_;
437 4 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
438 4 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 4 0 4
439 4 ▶ if (defined blessed($_[0]) and my $f = $_[0]->can("cons")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($_[0]) and my $f = $_[0]->can('cons')) { } 3 1 4
Condition66%2/3
defined builtin::blessed($_[0]) and my $f = $_[0]->can("cons")
!ll&&!rl&&r
103
MC/DC50%1/2
defined builtin::blessed($_[0]) and my $f = $_[0]->can("cons")
defined builtin::blessed($_[0]) my $f = $_[0]->can("cons")
Truth table66%2/3
A and B
A= defined builtin::blessed($_[0])B= my $f = $_[0]->can("cons")
ABresult
0X0
100
111
440 3 push @_, $item;
441 3 goto &$f;
442 } else {
443 1 unsafe_cons($item, $_[0])
444 }
445 }
446 4 }
447  
448 1 ▶TEST { cons_(1)->(list(2)) } GIVES { list(1, 2) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
449 1 ▶TEST { cons_(1)->(2) } GIVES { cons 1, 2 };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
450  
451 4 ▶sub pair {
Subroutine pair: called CC=2 SCAR=7.2
Pod undocumented
452 4 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 4 0 4
453 4 goto \&unsafe_cons
454 }
455  
456 # no type checking, but perhaps faster (especially if inlining ever
457 # becomes possible). Note that unsafe_car and unsafe_cdr are safe if
458 # subclasses are keeping the first fields the same and the argument
459 # was confirmed to be a pair with `is_pair`. unsafe_cons is safe if
460 # the rest argument should never dictate the type of the result.
461  
462 7589 ▶sub unsafe_cons {
Subroutine unsafe_cons: called CC=3 SCAR=11.1
Pod undocumented
463 7589 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 7589 0 7589
464 7589 my @p = @_;
465 7589 bless \@p, "FP::List::Pair";
466 7589 ▶ if ($immutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FP::List::immutable) 7589 0 7589
467 7589 Internals::SvREADONLY $p[0], 1;
468 7589 Internals::SvREADONLY $p[1], 1;
469 }
470 7589 Internals::SvREADONLY @p, 1;
471 \@p
472 7589 }
473  
474 # WARNING: be careful, this isn't safe even if `is_pair` returns true, as
475 # that only assures that ->car etc. can be called.
476 0 ▶sub unsafe_car {
Subroutine unsafe_car: not called CC=2 SCAR=17.9
Pod undocumented
477 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
478 0 $_[0][0]
479 }
480  
481 # WARNING: same as for unsafe_car
482 0 ▶sub unsafe_cdr {
Subroutine unsafe_cdr: not called CC=2 SCAR=17.9
Pod undocumented
483 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
484 0 $_[0][1]
485 }
486  
487 10211 ▶sub is_pair {
Subroutine is_pair: called CC=6 SCAR=18.5
Pod undocumented
488 10211 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 10211 0 10211
489 10211 my ($v) = @_;
490 10211 ▶ my $r = blessed($v) // return;
Condition100%2/2
builtin::blessed($v) // (return)
l!l
9867344
MC/DC100%1/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
491 (
492 9867 ▶ $r eq "FP::List::Pair"
Branch: 1 branch
BranchTrueFalseTotal
$r eq "FP::List::Pair" or $v->isa("FP::List::Pair") or $v->isa("FP::Lazy::Promise") and is_pair(force($v)) 9051 816 9867
Condition66%6/9
$r eq 'FP::List::Pair' || $v->isa('FP::List::Pair') || $v->isa('FP::Lazy::Promise') && is_pair(force($v))
l!l&&r!l&&!r
90510816
$r eq "FP::List::Pair" or $v->isa("FP::List::Pair")
l!l&&r!l&&!r
902031816
$v->isa("FP::Lazy::Promise") and is_pair(force($v))
!ll&&!rl&&r
81600
MC/DC50%2/4
$r eq 'FP::List::Pair' || $v->isa('FP::List::Pair') || $v->isa('FP::Lazy::Promise') && is_pair(force($v))
$r eq "FP::List::Pair" $v->isa("FP::List::Pair") $v->isa("FP::Lazy::Promise") is_pair(force($v))
Truth table60%3/5
A or B || C and D
A= $r eq "FP::List::Pair"B= $v->isa("FP::List::Pair")C= $v->isa("FP::Lazy::Promise")D= is_pair(force($v))
ABCDresult
1XXX1
01XX1
00111
000X0
00100
493 or $v->isa("FP::List::Pair")
494 or $v->isa("FP::Lazy::Promise") && is_pair(force $v)
495 )
496  
497 # ^ XX evil: inlined `is_promise`
498 }
499  
500 41 ▶sub is_pair_noforce {
Subroutine is_pair_noforce: called CC=4 SCAR=13.9
Pod undocumented
501 41 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 41 0 41
502 41 my ($v) = @_;
503 41 ▶ my $r = blessed($v) // return;
Condition100%2/2
builtin::blessed($v) // (return)
l!l
374
MC/DC100%1/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
504 37 ▶ ($r eq "FP::List::Pair" or $v->isa("FP::List::Pair"))
Branch: 1 branch
BranchTrueFalseTotal
$r eq "FP::List::Pair" or $v->isa("FP::List::Pair") 18 19 37
505 }
506  
507 72 ▶sub is_pair_of {
Subroutine is_pair_of: called CC=1 SCAR=0.0
Pod undocumented
508 72 my ($p0, $p1) = @_;
509 55 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
510 55 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 55 0 55
511 55 my ($v) = @_;
512 55 ▶ (is_pair($v) and &$p0($$v[0]) and &$p1($$v[1]))
Branch: 1 branch
BranchTrueFalseTotal
is_pair($v) and &$p0($v->[0]) and &$p1($v->[1]) 48 7 55
Condition100%3/3
is_pair($v) and &$p0($v->[0])
!ll&&!rl&&r
3448
MC/DC100%2/2
is_pair($v) and &$p0($v->[0])
is_pair($v) &$p0($v->[0])
Truth table100%3/3
A and B
A= is_pair($v)B= &$p0($v->[0])
ABresult
0X0
100
111
513 }
514 72 }
515  
516 # nil value
517  
518 60 my $null = do {
519 60 my @null;
520 60 bless \@null, "FP::List::Null";
521 60 Internals::SvREADONLY @null, 1;
522 \@null
523 60 };
524 60 Internals::SvREADONLY $null, 1;
525  
526 4545 ▶sub null () {
Subroutine null: called CC=1 SCAR=0.0
Pod undocumented
527  
528 # @_ == 0 or fp_croak_arity 0;
529 # nope, it is also called as a method
530 4545 $null
531 }
532  
533 1 ▶TEST { null->cons(1)->cons(2)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
534 60 [2, 1];
535  
536 16359 ▶sub is_null {
Subroutine is_null: called CC=6 SCAR=17.9
Pod undocumented
537 16359 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 16359 0 16359
538 16359 my ($v) = @_;
539 16359 ▶ my $r = blessed($v) // return;
Condition100%2/2
builtin::blessed($v) // (return)
l!l
16017342
MC/DC100%1/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
540 (
541 16017 ▶ $r eq "FP::List::Null"
Branch: 1 branch
BranchTrueFalseTotal
$r eq "FP::List::Null" or $v->isa("FP::List::Null") or $v->isa("FP::Lazy::Promise") and is_null(force($v)) 1455 14562 16017
Condition100%9/9
$r eq 'FP::List::Null' || $v->isa('FP::List::Null') || $v->isa('FP::Lazy::Promise') && is_null(force($v))
l!l&&r!l&&!r
14553714525
$r eq "FP::List::Null" or $v->isa("FP::List::Null")
l!l&&r!l&&!r
14352014562
$v->isa("FP::Lazy::Promise") and is_null(force($v))
!ll&&!rl&&r
144359037
MC/DC100%4/4
$r eq 'FP::List::Null' || $v->isa('FP::List::Null') || $v->isa('FP::Lazy::Promise') && is_null(force($v))
$r eq "FP::List::Null" $v->isa("FP::List::Null") $v->isa("FP::Lazy::Promise") is_null(force($v))
Truth table100%5/5
A or B || C and D
A= $r eq "FP::List::Null"B= $v->isa("FP::List::Null")C= $v->isa("FP::Lazy::Promise")D= is_null(force($v))
ABCDresult
1XXX1
01XX1
00111
000X0
00100
542 or $v->isa("FP::List::Null")
543 or $v->isa("FP::Lazy::Promise") && is_null(force $v)
544 )
545 }
546  
547 23 ▶sub is_null_noforce {
Subroutine is_null_noforce: called CC=4 SCAR=13.9
Pod undocumented
548 23 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 23 0 23
549 23 my ($v) = @_;
550 23 ▶ my $r = blessed($v) // return;
Condition100%2/2
builtin::blessed($v) // (return)
l!l
194
MC/DC100%1/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
551 19 ▶ ($r eq "FP::List::Null" or $v->isa("FP::List::Null"))
Branch: 1 branch
BranchTrueFalseTotal
$r eq "FP::List::Null" or $v->isa("FP::List::Null") 18 1 19
552 }
553  
554 4 ▶sub is_pair_or_null {
Subroutine is_pair_or_null: called CC=6 SCAR=19.0
Pod undocumented
555 4 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 4 0 4
556 4 my ($v) = @_;
557 4 ▶ my $r = blessed($v) // return;
Condition100%2/2
builtin::blessed($v) // (return)
l!l
31
MC/DC100%1/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
558 (
559 3 ▶ $r eq "FP::List::List"
Branch: 1 branch
BranchTrueFalseTotal
$r eq "FP::List::List" or $v->isa("FP::List::List") or $v->isa("FP::Lazy::Promise") and is_pair_or_null(force($v)) 2 1 3
Condition55%5/9
$r eq 'FP::List::List' || $v->isa('FP::List::List') || $v->isa('FP::Lazy::Promise') && is_pair_or_null(force($v))
l!l&&r!l&&!r
201
$r eq "FP::List::List" or $v->isa("FP::List::List")
l!l&&r!l&&!r
021
$v->isa("FP::Lazy::Promise") and is_pair_or_null(force($v))
!ll&&!rl&&r
100
MC/DC25%1/4
$r eq 'FP::List::List' || $v->isa('FP::List::List') || $v->isa('FP::Lazy::Promise') && is_pair_or_null(force($v))
$r eq "FP::List::List" $v->isa("FP::List::List") $v->isa("FP::Lazy::Promise") is_pair_or_null(force($v))
Truth table40%2/5
A or B || C and D
A= $r eq "FP::List::List"B= $v->isa("FP::List::List")C= $v->isa("FP::Lazy::Promise")D= is_pair_or_null(force($v))
ABCDresult
1XXX1
01XX1
00111
000X0
00100
560 or $v->isa("FP::List::List")
561 or $v->isa("FP::Lazy::Promise") && is_pair_or_null(force $v)
562 )
563 }
564  
565 1 ▶TEST { is_pair_or_null cons 1, 2 } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
566 1 ▶TEST { is_pair_or_null null } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
567 1 ▶TEST { is_pair_or_null 1 } undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
568 1 ▶TEST { is_pair_or_null bless [], "NirvAna" } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
569  
570 # test subclassing? whatever
571  
572 6 ▶sub is_null_or_pair_of {
Subroutine is_null_or_pair_of: called CC=5 SCAR=16.2
Pod undocumented
573 6 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 6 0 6
574 6 my ($v, $p0, $p1) = @_;
575 6 FORCE $v;
576 6 ▶ (is_null $v or (is_pair $v and &$p0($v->car) and &$p1($v->cdr)))
Branch: 1 branch
BranchTrueFalseTotal
is_null($v) or is_pair($v) and &$p0($v->car) and &$p1($v->cdr) 1 5 6
Condition88%8/9
is_null($v) || is_pair($v) && &$p0($v->car) && &$p1($v->cdr)
l!l&&r!l&&!r
114
is_pair($v) and &$p0($v->car) and &$p1($v->cdr)
!ll&&!rl&&r
221
is_pair($v) and &$p0($v->car)
!ll&&!rl&&r
023
MC/DC75%3/4
is_null($v) || is_pair($v) && &$p0($v->car) && &$p1($v->cdr)
is_null($v) is_pair($v) &$p0($v->car) &$p1($v->cdr)
Truth table80%4/5
A || B and C and D
A= is_null($v)B= is_pair($v)C= &$p0($v->car)D= &$p1($v->cdr)
ABCDresult
1XXX1
01111
00XX0
010X0
01100
577 }
578  
579 1 ▶sub null_or_pair_of {
Subroutine null_or_pair_of: called CC=2 SCAR=7.1
Pod undocumented
580 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
581 1 my ($p0, $p1) = @_;
582  
583 6 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
584 6 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 6 0 6
585 6 my ($v) = @_;
586 6 is_null_or_pair_of($v, $p0, $p1)
587 }
588 1 }
589  
590 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
591 1 require FP::Array;
592 1 FP::Array::array_map(
593 null_or_pair_of(\&is_null, \&is_pair),
594 [
595 null,
596 cons(1, 2),
597 cons(null, 1),
598 cons(null, null),
599 cons(null, cons(1, 1)),
600 cons(cons(1, 1), cons(1, 1))
601 ]
602 )
603 }
604 60 [1, undef, undef, '', 1, ''];
605  
606 38 ▶sub is_list {
Subroutine is_list: called CC=4 SCAR=13.9
Pod undocumented
607 38 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 38 0 38
608 38 my ($v) = @_;
609 38 FORCE $v;
610 (
611 is_null($v) ? 1 : (
612 38 ▶ is_pair($v) ? do {
Branch: 2 branches
BranchTrueFalseTotal
is_null($v) ? : 11 27 38
is_pair($v) ? : 22 5 27
613 22 @_ = $v->cdr;
614 22 goto \&is_list;
615 }
616 : ''
617 )
618 )
619 }
620 60 *FP::List::List::is_proper_sequence = \&is_list;
621  
622 1 ▶TEST { is_list cons 1, cons 2, null } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
623 1 ▶TEST { is_list cons 1, cons 2, 3 } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
624 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
625 1 require FP::Lazy;
626 1 ▶ is_list cons 1, FP::Lazy::lazy { cons 2, null }
Subroutine __ANON__: called CC=1 SCAR=0.0
627 1 }
628 60 1;
629 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
630 1 ▶ is_list cons 1, FP::Lazy::lazy { cons 2, 3 }
Subroutine __ANON__: called CC=1 SCAR=0.0
631 1 }
632 60 '';
633  
634 3 ▶sub die_not_a_pair {
Subroutine die_not_a_pair: called CC=2 SCAR=7.1
Pod undocumented
635 3 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3 0 3
636 3 my ($v) = @_;
637 3 croak "not a pair: " . show($v);
638 }
639  
640 13920 ▶sub car {
Subroutine car: called CC=5 SCAR=16.2
Pod undocumented
641 13920 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 13920 0 13920
642 13920 my ($v) = @_;
643 13920 ▶ my $r = blessed($v) // die_not_a_pair($v);
Condition66%2/3
builtin::blessed($v) // die_not_a_pair($v)
l!l&&r!l&&!r
1391910
MC/DC0%0/2
builtin::blessed($v) // die_not_a_pair($v)
builtin::blessed($v) die_not_a_pair($v)
Truth table33%1/3
A // B
A= builtin::blessed($v)B= die_not_a_pair($v)
ABresult
1X1
011
000
644 13919 ▶ if ($r eq "FP::List::Pair") {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'FP::List::Pair') { } 13778 141 13919
elsif (is_promise($v)) { } 80 61 141
645 13778 $v->[0]
646 } elsif (is_promise $v) {
647 80 @_ = force $v;
648 80 goto \&car;
649 } else {
650 61 $v->car
651 }
652 }
653  
654 sub first;
655 60 *first = \&car;
656  
657 # XX add maybe_first and perhaps_first wrappers here? Shouldn't this
658 # be more structured/automatic, finally.
659  
660 1 ▶sub first_set {
Subroutine first_set: called CC=2 SCAR=7.1
Pod undocumented
661 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
662 1 my ($p, $v) = @_;
663 1 cons($v, $p->rest)
664 }
665  
666 1 ▶TEST { cons(3, 4)->first_set("a") } bless ["a", 4], 'FP::List::Pair';
Subroutine __ANON__: called CC=1 SCAR=0.0
667  
668 1 ▶sub first_update {
Subroutine first_update: called CC=2 SCAR=7.0
Pod undocumented
669 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
670 1 my ($p, $fn) = @_;
671 1 my ($v, $r) = $p->first_and_rest;
672 1 cons(&$fn($v), $r)
673 }
674  
675 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
676 1 ▶ cons(3, 4)->first_update(sub { $_[0] * 2 })
Subroutine __ANON__: called CC=1 SCAR=0.0
677 1 }
678 60 bless [6, 4], 'FP::List::Pair';
679  
680 13870 ▶sub cdr {
Subroutine cdr: called CC=5 SCAR=16.7
Pod undocumented
681 13870 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 13870 0 13870
682 13870 my ($v) = @_;
683 13870 ▶ my $r = blessed($v) // die_not_a_pair($v);
Condition66%2/3
builtin::blessed($v) // die_not_a_pair($v)
l!l&&r!l&&!r
1386910
MC/DC0%0/2
builtin::blessed($v) // die_not_a_pair($v)
builtin::blessed($v) die_not_a_pair($v)
Truth table33%1/3
A // B
A= builtin::blessed($v)B= die_not_a_pair($v)
ABresult
1X1
011
000
684 13869 ▶ if ($r->isa("FP::List::Pair")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r->isa('FP::List::Pair')) { } 13783 86 13869
elsif (is_promise($v)) { } 86 0 86
685 13783 $v->[1]
686 } elsif (is_promise $v) {
687 86 @_ = force $v;
688 86 goto \&cdr;
689 } else {
690 0 $v->cdr
691 }
692 }
693  
694 1 ▶TEST { is_pair cons(2, 3) } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
695 1 ▶TEST { is_pair "FP::List::Pair" } undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
696 1 ▶TEST { car cons(2, 3) } 2;
Subroutine __ANON__: called CC=1 SCAR=0.0
697 1 ▶TEST_EXCEPTION { car "FP::List::Pair" } "not a pair: 'FP::List::Pair'";
Subroutine __ANON__: called CC=1 SCAR=0.0
698 1 ▶TEST_EXCEPTION { cdr "FP::List::Pair" } "not a pair: 'FP::List::Pair'";
Subroutine __ANON__: called CC=1 SCAR=0.0
699  
700 sub rest;
701 60 *rest = \&cdr;
702  
703 0 ▶sub cddr {
Subroutine cddr: not called CC=2 SCAR=17.9
Pod undocumented
704 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
705 0 cdr cdr $_[0]
706 }
707  
708 1 ▶sub cdddr {
Subroutine cdddr: called CC=2 SCAR=7.2
Pod undocumented
709 1 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1 0 1
710 1 cdr cdr cdr $_[0]
711 }
712  
713 0 ▶sub cddddr {
Subroutine cddddr: not called CC=2 SCAR=17.9
Pod undocumented
714 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
715 0 cdr cdr cdr cdr $_[0]
716 }
717  
718 0 ▶sub cadr {
Subroutine cadr: not called CC=2 SCAR=17.9
Pod undocumented
719 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
720 0 car cdr $_[0]
721 }
722  
723 0 ▶sub caddr {
Subroutine caddr: not called CC=2 SCAR=17.9
Pod undocumented
724 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
725 0 car cdr cdr $_[0]
726 }
727  
728 0 ▶sub cadddr {
Subroutine cadddr: not called CC=2 SCAR=17.9
Pod undocumented
729 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
730 0 car cdr cdr cdr $_[0]
731 }
732  
733 0 ▶sub caddddr {
Subroutine caddddr: not called CC=2 SCAR=17.9
Pod undocumented
734 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
735 0 car cdr cdr cdr cdr $_[0]
736 }
737  
738 1 ▶sub c_r {
Subroutine c_r: called CC=5 SCAR=16.2
Pod undocumented
739 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
740 1 my ($s, $chain) = @_;
741 1 my $c;
742 1 ▶ while (length($c = chop $chain)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($c = chop $chain)) 5 1 6
743 5 ▶ $s
Branch: 2 branches
BranchTrueFalseTotal
$c eq 'a' ? : 2 3 5
$c eq 'd' ? : 3 0 3
744 = $c eq "a" ? car($s)
745 : $c eq "d" ? cdr($s)
746 : die "only 'a' and 'd' acceptable in chain, have: '$chain'";
747 }
748 $s
749 1 }
750  
751 60 *FP::List::List::c_r = \&c_r;
752  
753 1 ▶TEST { list(1, list(4, 7, 9), 5)->c_r("addad") }
Subroutine __ANON__: called CC=1 SCAR=0.0
754 60 9;
755  
756 5721 ▶sub car_and_cdr {
Subroutine car_and_cdr: called CC=5 SCAR=17.3
Pod undocumented
757 5721 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 5721 0 5721
758 5721 my ($v) = @_;
759 5721 ▶ my $r = blessed($v) // die_not_a_pair($v);
Condition33%1/3
builtin::blessed($v) // die_not_a_pair($v)
l!l&&r!l&&!r
572100
MC/DC0%0/2
builtin::blessed($v) // die_not_a_pair($v)
builtin::blessed($v) die_not_a_pair($v)
Truth table33%1/3
A // B
A= builtin::blessed($v)B= die_not_a_pair($v)
ABresult
1X1
011
000
760 5721 ▶ if ($r eq "FP::List::Pair") {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'FP::List::Pair') { } 5547 174 5721
elsif (is_promise($v)) { } 174 0 174
761 5547 @$v
762 } elsif (is_promise $v) {
763 174 @_ = force $v;
764 174 goto \&car_and_cdr;
765 } else {
766 0 $v->car_and_cdr
767 }
768 }
769  
770 sub first_and_rest;
771 60 *first_and_rest = \&car_and_cdr;
772  
773 3 ▶sub perhaps_first_and_rest {
Subroutine perhaps_first_and_rest: called CC=6 SCAR=20.2
Pod undocumented
774 3 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3 0 3
775 3 my ($v) = @_;
776 3 ▶ my $r = blessed($v) // die_not_a_pair($v);
Condition66%2/3
builtin::blessed($v) // die_not_a_pair($v)
l!l&&r!l&&!r
201
MC/DC0%0/2
builtin::blessed($v) // die_not_a_pair($v)
builtin::blessed($v) die_not_a_pair($v)
Truth table33%1/3
A // B
A= builtin::blessed($v)B= die_not_a_pair($v)
ABresult
1X1
011
000
777 2 ▶ if ($r eq "FP::List::Pair") {
Branch: 3 branches
BranchTrueFalseTotal
if ($r eq 'FP::List::Pair') { } 1 1 2
elsif (is_promise($v)) { } 0 1 1
elsif ($r eq 'FP::List::Null') { } 1 0 1
778 1 @$v
779 } elsif (is_promise $v) {
780 0 @_ = force $v;
781 0 goto \&perhaps_first_and_rest;
782 } elsif ($r eq "FP::List::Null") {
783 ()
784 1 } else {
785 0 $v->perhaps_first_and_rest
786 }
787 }
788  
789 1 ▶TEST { [cons(1, 2)->perhaps_first_and_rest] } [1, 2];
Subroutine __ANON__: called CC=1 SCAR=0.0
790 1 ▶TEST { [null->perhaps_first_and_rest] } [];
Subroutine __ANON__: called CC=1 SCAR=0.0
791 1 ▶TEST { [perhaps_first_and_rest cons(1, 2)] } [1, 2];
Subroutine __ANON__: called CC=1 SCAR=0.0
792 1 ▶TEST { [perhaps_first_and_rest null] } [];
Subroutine __ANON__: called CC=1 SCAR=0.0
793 1 ▶TEST_EXCEPTION { [perhaps_first_and_rest "FP::List::Null"] }
Subroutine __ANON__: called CC=1 SCAR=0.0
794 60 "not a pair: 'FP::List::Null'"; # and XX actually not a null either.
795  
796 3 ▶sub list_perhaps_one {
Subroutine list_perhaps_one: called CC=4 SCAR=13.9
Pod undocumented
797 3 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3 0 3
798 3 my ($s) = @_;
799 3 FORCE $s; # make work for stre
800 3 ▶ if (is_pair($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_pair($s)) { } 2 1 3
801 2 my ($a, $r) = first_and_rest $s;
802 2 ▶ if (is_null $r) { ($a) }
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($r)) { } 1 1 2
803 1 else { () }
804 } else {
805 ()
806 1 }
807 }
808  
809 60 *FP::List::List::perhaps_one = \&list_perhaps_one;
810  
811 1 ▶TEST { [list(8)->perhaps_one] } [8];
Subroutine __ANON__: called CC=1 SCAR=0.0
812 1 ▶TEST { [list(8, 9)->perhaps_one] } [];
Subroutine __ANON__: called CC=1 SCAR=0.0
813 1 ▶TEST { [list()->perhaps_one] } [];
Subroutine __ANON__: called CC=1 SCAR=0.0
814  
815 3 ▶sub list_xone {
Subroutine list_xone: called CC=4 SCAR=13.9
Pod undocumented
816 3 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3 0 3
817 3 my ($s) = @_;
818 3 FORCE $s; # make work for streams
819 3 ▶ if (is_pair($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_pair($s)) { } 2 1 3
820 2 my ($a, $r) = first_and_rest $s;
821 2 ▶ if (is_null $r) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($r)) { } 1 1 2
822 1 $a
823 } else {
824 1 die "expected 1 value, got more"
825 }
826 } else {
827 1 die "expected 1 value, got none"
828 }
829 }
830  
831 60 *FP::List::List::xone = \&list_xone;
832  
833 1 ▶TEST { [list(8)->xone] } [8];
Subroutine __ANON__: called CC=1 SCAR=0.0
834 1 ▶TEST_EXCEPTION { [list(8, 9)->xone] } "expected 1 value, got more";
Subroutine __ANON__: called CC=1 SCAR=0.0
835 1 ▶TEST_EXCEPTION { [list()->xone] } "expected 1 value, got none";
Subroutine __ANON__: called CC=1 SCAR=0.0
836  
837 101 ▶sub make_ref {
Subroutine make_ref: called CC=2 SCAR=6.9
Pod undocumented
838 101 my ($is_stream) = @_;
839 101 ▶ my $liststream = $is_stream ? "stream" : "list";
Branch: 1 branch
BranchTrueFalseTotal
$is_stream ? : 41 60 101
840  
841 31 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
842 31 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 31 0 31
843 31 my ($s, $i) = @_;
844 31 ▶ weaken $_[0] if $is_stream;
Branch: 1 branch
BranchTrueFalseTotal
if $is_stream 10 21 31
845 31 ▶ is_natural0 $i or fp_croak "invalid index: " . show($i);
Branch: 1 branch
BranchTrueFalseTotal
is_natural0($i) or fp_croak('invalid index: ' . show($i)) 27 4 31
846 27 my $orig_i = $i;
847 LP: {
848 27 $s = force $s;
849 1715 ▶ if (is_pair $s) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_pair($s)) { } 1709 6 1715
elsif (is_null($s)) { } 2 4 6
elsif (defined builtin::blessed($s) and my $m = $s->can('FP_Sequence_ref')) { } 1 3 4
850 1709 ▶ if ($i <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 0) { } 21 1688 1709
851 21 $s->car
852 } else {
853 1688 $s = $s->cdr;
854 1688 $i--;
855 1688 redo LP;
856 }
857 } elsif (is_null $s) {
858 2 die "requested element $orig_i of $liststream of length "
859 . ($orig_i - $i)
860 ▶ } elsif (defined blessed($s) and my $m = $s->can("FP_Sequence_ref"))
Condition66%2/3
defined builtin::blessed($s) and my $m = $s->can("FP_Sequence_ref")
!ll&&!rl&&r
301
MC/DC50%1/2
defined builtin::blessed($s) and my $m = $s->can("FP_Sequence_ref")
defined builtin::blessed($s) my $m = $s->can("FP_Sequence_ref")
Truth table66%2/3
A and B
A= defined builtin::blessed($s)B= my $m = $s->can("FP_Sequence_ref")
ABresult
0X0
100
111
861 {
862 1 @_ = ($s, $i);
863 1 goto $m
864 } else {
865 3 die "improper $liststream"
866 }
867 }
868 }
869 101 }
870  
871 sub list_ref;
872 60 *list_ref = make_ref(0);
873 60 *FP::List::List::ref = \&list_ref;
874  
875 3529 ▶sub list {
Subroutine list: called CC=2 SCAR=6.9
Pod undocumented
876 3529 my $res = null;
877 3529 ▶ for (my $i = $#_; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 10464 3529 13993
878 10464 $res = cons($_[$i], $res);
879 }
880 3529 $res
881 }
882  
883 # Like 'list' but terminates the chain with the last argument instead
884 # of a 'null'. This shouldn't be used in normal circumstances. It's
885 # mainly here to make the output of FP_Show_show valid code.
886 4 ▶sub improper_list {
Subroutine improper_list: called CC=2 SCAR=6.9
Pod undocumented
887 4 my $res = pop;
888 4 ▶ for (my $i = $#_; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 5 4 9
889 5 $res = cons($_[$i], $res);
890 }
891 4 $res
892 }
893  
894 # These violate the principle of a purely functional data
895 # structure. Are they ok since they are constructors (the outside
896 # world will never see mutation)? (Note that streams can be cyclic
897 # already without mutating the cons cells, by way of using a recursive
898 # binding (mutating the variable that holds it, "my $s; $s = cons 1,
899 # lazy { $s };").)
900  
901 # WARNING: results of this function won't be deallocated
902 # automatically. You have to break the reference cycle explicitely!
903 1 ▶sub circularlist {
Subroutine circularlist: called CC=2 SCAR=7.1
Pod undocumented
904 1 my $l = list(@_);
905 1 my $last = $l->drop($#_);
906 1 ▶ if ($immutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FP::List::immutable) { } 1 0 1
907 1 Internals::SvREADONLY $$last[1], 0;
908 1 $$last[1] = $l;
909 1 Internals::SvREADONLY $$last[1], 1;
910 } else {
911 0 $$last[1] = $l;
912 }
913 1 $l
914 }
915  
916 # And the result of this function will open up (interrupt the cycle)
917 # as soon as you let go of the front element.
918  
919 2 ▶sub weaklycircularlist {
Subroutine weaklycircularlist: called CC=2 SCAR=7.2
Pod undocumented
920 2 my $l = list(@_);
921 2 my $last = $l->drop($#_);
922 2 ▶ if ($immutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FP::List::immutable) { } 2 0 2
923 2 Internals::SvREADONLY $$last[1], 0;
924 2 $$last[1] = $l;
925 2 weaken($$last[1]);
926 2 Internals::SvREADONLY $$last[1], 1;
927 } else {
928 0 $$last[1] = $l;
929 0 weaken($$last[1]);
930 }
931 2 $l
932 }
933  
934 60 ▶use Chj::Destructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
935  
936 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
937 1 my $z = 0;
938 1 my $v = do {
939 0 ▶ my $l = circularlist "a", "b", Destructor { $z++ }, "d";
Subroutine __ANON__: not called CC=1 SCAR=1.2
940 1 $l->ref(5)
941 };
942 1 [$z, $v]
943 }
944 60 [0, "b"]; # leaking the test list!
945  
946 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
947 1 my $z = 0;
948 1 my $v = do {
949 1 ▶ my $l = weaklycircularlist "a", "b", Destructor { $z++ }, "d";
Subroutine __ANON__: called CC=1 SCAR=0.0
950 1 $l->ref(5)
951 };
952 1 [$z, $v]
953 }
954 60 [1, "b"]; # no leak.
955  
956 1 ▶TEST_EXCEPTION {
Subroutine __ANON__: called CC=1 SCAR=0.0
957 1 my $z = 0;
958 1 my $v = do {
959 1 ▶ my $l = weaklycircularlist "a", "b", Destructor { $z++ }, "d";
Subroutine __ANON__: called CC=1 SCAR=0.0
960 1 $l = $l->rest;
961 1 $l->ref(4)
962 };
963 0 [$z, $v]
964 }
965 60 'improper list'; # nice message at least, thanks to undef != null
966  
967 72 ▶sub delayed (&) {
Subroutine delayed: called CC=2 SCAR=7.0
Pod undocumented
968 72 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 72 0 72
969 72 my ($thunk) = @_;
970 48 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
971 # evaluate thunk, expecting a function and pass our arguments
972 # to that function
973 48 my $cont = &$thunk();
974 48 goto &$cont
975 }
976 72 }
977  
978 65 ▶sub list_of {
Subroutine list_of: called CC=2 SCAR=7.0
Pod undocumented
979 65 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 65 0 65
980 65 my ($p) = @_;
981 44 ▶ either \&is_null, is_pair_of($p, delayed { list_of($p) })
Subroutine __ANON__: called CC=1 SCAR=0.0
982 65 }
983  
984 1 ▶TEST { list_of(\&is_natural)->(list 1, 2, 3) } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
985 1 ▶TEST { list_of(\&is_natural)->(list -1, 2, 3) } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
986 1 ▶TEST { list_of(\&is_natural)->(list 1, 2, " 3") } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
987 1 ▶TEST { list_of(\&is_natural)->(1) } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
988 1 ▶TEST { list_of(\&is_natural)->(list()) } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
989  
990 7 ▶sub nonempty_list_of {
Subroutine nonempty_list_of: called CC=2 SCAR=7.0
Pod undocumented
991 7 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 7 0 7
992 7 my ($p) = @_;
993 4 ▶ is_pair_of($p, delayed { list_of($p) })
Subroutine __ANON__: called CC=1 SCAR=0.0
994 7 }
995  
996 1 ▶TEST { nonempty_list_of(\&is_natural)->(list 1, 2, 3) } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
997 1 ▶TEST { nonempty_list_of(\&is_natural)->(list -1, 2, 3) } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
998 1 ▶TEST { nonempty_list_of(\&is_natural)->(list 1, 2, " 3") } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
999 1 ▶TEST { nonempty_list_of(\&is_natural)->(1) } undef; # XX vs. above
Subroutine __ANON__: called CC=1 SCAR=0.0
1000 1 ▶TEST { nonempty_list_of(\&is_natural)->(list()) } ''; # vs. 0 ?
Subroutine __ANON__: called CC=1 SCAR=0.0
1001  
1002 101 ▶sub make_length {
Subroutine make_length: called CC=2 SCAR=7.0
Pod undocumented
1003 101 my ($is_stream) = @_;
1004 101 ▶ my $liststream = $is_stream ? "stream" : "list";
Branch: 1 branch
BranchTrueFalseTotal
$is_stream ? : 41 60 101
1005  
1006 72 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
1007 72 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 72 0 72
1008 72 my ($l) = @_;
1009 72 ▶ weaken $_[0] if $is_stream;
Branch: 1 branch
BranchTrueFalseTotal
if $is_stream 4 68 72
1010 72 my $len = 0;
1011 72 $l = force $l;
1012 72 ▶ while (!is_null $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_null($l)) 260 71 331
1013 260 ▶ if (is_pair $l) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_pair($l)) { } 259 1 260
elsif (defined builtin::blessed($l) and my $m = $l->can('FP_Sequence_length')) { } 1 0 1
1014 259 $len++;
1015 259 $l = force $l->cdr;
1016 ▶ } elsif (defined blessed($l)
Condition33%1/3
defined builtin::blessed($l) and my $m = $l->can("FP_Sequence_length")
!ll&&!rl&&r
001
MC/DC0%0/2
defined builtin::blessed($l) and my $m = $l->can("FP_Sequence_length")
defined builtin::blessed($l) my $m = $l->can("FP_Sequence_length")
Truth table33%1/3
A and B
A= defined builtin::blessed($l)B= my $m = $l->can("FP_Sequence_length")
ABresult
0X0
100
111
1017 and my $m = $l->can("FP_Sequence_length"))
1018 {
1019 1 @_ = ($l, $len);
1020 1 goto $m
1021 } else {
1022 0 die "improper $liststream"
1023 }
1024 }
1025 $len
1026 71 }
1027 101 }
1028  
1029 sub list_length;
1030 60 *list_length = make_length(0);
1031  
1032 60 *FP::List::Pair::length = \&list_length;
1033  
1034 # method on Pair not List, since we defined a length method for Null
1035 # explicitely
1036  
1037 1 ▶TEST { list(4, 5, 6)->caddr } 6;
Subroutine __ANON__: called CC=1 SCAR=0.0
1038 1 ▶TEST { list()->length } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
1039 1 ▶TEST { list(4, 5)->length } 2;
Subroutine __ANON__: called CC=1 SCAR=0.0
1040  
1041 24 ▶sub list_to_string {
Subroutine list_to_string: called CC=3 SCAR=11.0
Pod undocumented
1042 24 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 24 0 24
1043 24 my ($l) = @_;
1044 24 my $len = list_length $l;
1045  
1046 # preallocation for the case where $l consists only of single
1047 # characters (otherwise will extend dynamically):
1048 24 my $res = " " x $len;
1049 24 my $i = 0;
1050 24 ▶ while (!is_null $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_null($l)) 101 24 125
1051 101 my $c = car $l;
1052 101 substr($res, $i, 1) = $c;
1053 101 $l = cdr $l;
1054 101 $i += length $c;
1055 }
1056 $res
1057 24 }
1058  
1059 60 *FP::List::List::string = \&list_to_string;
1060  
1061 1 ▶TEST { null->string } "";
Subroutine __ANON__: called CC=1 SCAR=0.0
1062 1 ▶TEST { cons("a", null)->string } "a";
Subroutine __ANON__: called CC=1 SCAR=0.0
1063 1 ▶TEST { list("Ha", "ll", "o")->string } "Hallo";
Subroutine __ANON__: called CC=1 SCAR=0.0
1064 1 ▶TEST { list("", "", "o")->string } 'o';
Subroutine __ANON__: called CC=1 SCAR=0.0
1065 1 ▶TEST { list("a", "", "o")->string } 'ao';
Subroutine __ANON__: called CC=1 SCAR=0.0
1066  
1067 206 ▶sub list_to_array {
Subroutine list_to_array: called CC=3 SCAR=11.0
Pod undocumented
1068 206 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 206 0 206
1069 206 my ($l) = @_;
1070 206 my $res = [];
1071 206 my $i = 0;
1072 206 ▶ while (!is_null $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_null($l)) 593 206 799
1073 593 $$res[$i] = car $l;
1074 593 $l = cdr $l;
1075 593 $i++;
1076 }
1077 $res
1078 206 }
1079  
1080 60 *FP::List::List::array = \&list_to_array;
1081  
1082 12 ▶sub list_to_purearray {
Subroutine list_to_purearray: called CC=2 SCAR=7.0
Pod undocumented
1083 12 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 12 0 12
1084 12 my ($l) = @_;
1085 12 my $a = list_to_array $l;
1086 12 require FP::PureArray;
1087 12 FP::PureArray::array_to_purearray($a)
1088 }
1089  
1090 60 *FP::List::List::purearray = \&list_to_purearray;
1091  
1092 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1093 3 ▶ list(1, 3, 4)->purearray->map (sub { $_[0]**2 })
Subroutine __ANON__: called CC=1 SCAR=0.0
1094 1 }
1095 60 bless [1, 9, 16], "FP::_::PureArray";
1096  
1097 5 ▶sub list_sort {
Subroutine list_sort: called CC=3 SCAR=12.5
Pod undocumented
1098 5 ▶ @_ == 1 or @_ == 2 or fp_croak_arity "1 or 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or fp_croak_arity("1 or 2") 5 0 5
Condition33%1/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
050
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
1099 5 my ($l, $maybe_cmp) = @_;
1100 5 list_to_purearray($l)->sort ($maybe_cmp)
1101 }
1102  
1103 60 *FP::List::List::sort = \&list_sort;
1104  
1105 0 ▶sub list_sortCompare {
Subroutine list_sortCompare: not called CC=2 SCAR=17.9
Pod undocumented
1106 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
1107 0 my ($l) = @_;
1108 0 list_to_purearray($l)->sortCompare
1109 }
1110  
1111 60 *FP::List::List::sortCompare = \&list_sortCompare;
1112  
1113 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1114 1 require FP::Ops;
1115 1 list(5, 3, 8, 4)->sort (\&FP::Ops::real_cmp)->array
1116 }
1117 60 [3, 4, 5, 8];
1118  
1119 1 ▶TEST { ref(list(5, 3, 8, 4)->sort (\&FP::Ops::real_cmp)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1120 60 'FP::_::PureArray'; # XX ok? Need to `->list` if a list is needed
1121  
1122 1 ▶TEST { list(5, 3, 8, 4)->sort (\&FP::Ops::real_cmp)->list->car }
Subroutine __ANON__: called CC=1 SCAR=0.0
1123 60 3; # but then PureArray has `first`, too, if that's all you need.
1124  
1125 1 ▶TEST { list(5, 3, 8, 4)->sort (\&FP::Ops::real_cmp)->first }
Subroutine __ANON__: called CC=1 SCAR=0.0
1126 60 3;
1127  
1128 #(just for completeness)
1129 1 ▶TEST { list(5, 3, 8, 4)->sort (\&FP::Ops::real_cmp)->stream->car }
Subroutine __ANON__: called CC=1 SCAR=0.0
1130 60 3;
1131  
1132 37 ▶sub rlist_to_array {
Subroutine rlist_to_array: called CC=3 SCAR=11.0
Pod undocumented
1133 37 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 37 0 37
1134 37 my ($l) = @_;
1135 37 my $res = [];
1136 37 my $len = list_length $l;
1137 37 my $i = $len;
1138 37 ▶ while (!is_null $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_null($l)) 122 37 159
1139 122 $i--;
1140 122 $$res[$i] = car $l;
1141 122 $l = cdr $l;
1142 }
1143 $res
1144 37 }
1145  
1146 60 *FP::List::List::reverse_array = \&rlist_to_array;
1147  
1148 32 ▶sub list_to_values {
Subroutine list_to_values: called CC=2 SCAR=7.0
Pod undocumented
1149 32 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 32 0 32
1150 32 my ($l) = @_;
1151 32 @{ list_to_array($l) }
1152 }
1153  
1154 60 *FP::List::List::values = \&list_to_values;
1155  
1156 # XX naming inconsistency versus docs/design.md ? Same with
1157 # rlist_to_array.
1158 36 ▶sub rlist_to_values {
Subroutine rlist_to_values: called CC=2 SCAR=7.0
Pod undocumented
1159 36 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 36 0 36
1160 36 my ($l) = @_;
1161 36 @{ rlist_to_array($l) }
1162 }
1163  
1164 60 *FP::List::List::reverse_values = \&rlist_to_values;
1165  
1166 1 ▶TEST { [list(3, 4, 5)->reverse_values] }
Subroutine __ANON__: called CC=1 SCAR=0.0
1167 60 [5, 4, 3];
1168  
1169 202 ▶sub make_for_each {
Subroutine make_for_each: called CC=3 SCAR=12.6
Pod undocumented
1170 202 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 202 0 202
1171 202 my ($is_stream, $with_islast) = @_;
1172 202 ▶ my $liststream = $is_stream ? "stream" : "list";
Branch: 1 branch
BranchTrueFalseTotal
$is_stream ? : 202 0 202
1173  
1174 200 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=25.7
1175 200 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 200 0 200
1176 200 my ($proc, $s) = @_;
1177 200 ▶ weaken $_[1] if $is_stream;
Branch: 1 branch
BranchTrueFalseTotal
if $is_stream 200 0 200
1178 LP: {
1179 200 $s = force $s;
1180 585 ▶ if (is_pair $s) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_pair($s)) { } 389 196 585
elsif (is_null($s)) { } 196 0 196
elsif (defined builtin::blessed($s) and my $m = $s->can('for_each')) { } 0 0 0
1181 389 my $s2 = cdr $s;
1182 389 ▶ &$proc(scalar car($s), $with_islast ? scalar is_null($s2) : ());
Branch: 1 branch
BranchTrueFalseTotal
$with_islast ? : 0 389 389
1183 388 $s = $s2;
1184 388 redo LP;
1185 } elsif (is_null $s) {
1186  
1187 # drop out
1188 ▶ } elsif (defined blessed($s) and my $m = $s->can("for_each")) {
Condition0%0/3
defined builtin::blessed($s) and my $m = $s->can("for_each")
!ll&&!rl&&r
000
MC/DC0%0/2
defined builtin::blessed($s) and my $m = $s->can("for_each")
defined builtin::blessed($s) my $m = $s->can("for_each")
Truth table0%0/3
A and B
A= defined builtin::blessed($s)B= my $m = $s->can("for_each")
ABresult
0X0
100
111
1189 0 @_ = ($s, $proc);
1190 0 goto $m
1191 } else {
1192 0 die "improper $liststream"
1193 }
1194 }
1195 }
1196 202 }
1197  
1198 sub list_for_each;
1199 60 *list_for_each = make_for_each(1, 0);
1200 60 *FP::List::List::for_each = flip \&list_for_each;
1201  
1202 sub list_for_each_with_islast;
1203 60 *list_for_each_with_islast = make_for_each(1, 1);
1204 60 *FP::List::List::for_each_with_islast = flip \&list_for_each_with_islast;
1205  
1206 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
1207 1 list(1, 3)->for_each(\&xprintln)
1208 }
1209 60 "1\n3\n";
1210  
1211 # tons of slightly adapted COPIES from FP::Stream. XX finally find a
1212 # solution for this
1213  
1214 4 ▶sub list_drop {
Subroutine list_drop: called CC=4 SCAR=14.0
Pod undocumented
1215 4 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 4 0 4
1216 4 my ($s, $n) = @_;
1217 4 ▶ while ($n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) 12 4 16
1218 12 $s = force $s;
1219 12 ▶ die "list too short" if is_null $s;
Branch: 1 branch
BranchTrueFalseTotal
if is_null($s) 0 12 12
1220 12 $s = cdr $s;
1221 12 $n--
1222 }
1223 $s
1224 4 }
1225  
1226 60 *FP::List::List::drop = \&list_drop;
1227  
1228 26 ▶sub list_take {
Subroutine list_take: called CC=4 SCAR=14.0
Pod undocumented
1229 26 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 26 0 26
1230 26 my ($s, $n) = @_;
1231 26 ▶ if ($n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) { } 23 3 26
1232 23 $s = force $s;
1233 23 ▶ is_null($s) ? $s : cons(car($s), list_take(cdr($s), $n - 1));
Branch: 1 branch
BranchTrueFalseTotal
is_null($s) ? : 0 23 23
1234 } else {
1235 3 null
1236 }
1237 }
1238  
1239 60 *FP::List::List::take = \&list_take;
1240  
1241 0 ▶sub list_slice {
Subroutine list_slice: not called CC=2 SCAR=17.9
Pod undocumented
1242 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
1243 0 my ($start, $end) = @_;
1244 0 $end = force $end;
1245 0 my $rec;
1246 0 ▶ $rec = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1247 0 my ($s) = @_;
1248 0 my $rec = $rec;
1249 0 $s = force $s;
1250 0 ▶ if (is_null $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($s)) { } 0 0 0
1251 0 $s # null
1252 } else {
1253 0 ▶ if ($s eq $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq $end) { } 0 0 0
1254 0 null
1255 } else {
1256 0 cons car($s), &$rec(cdr $s)
1257 }
1258 }
1259 0 };
1260 0 @_ = ($start);
1261 0 goto &{ Weakened $rec};
1262 }
1263  
1264 60 *FP::List::List::slice = \&list_slice;
1265  
1266 # maybe call it `cut_at` instead?
1267  
1268 # /COPIES
1269  
1270 27 ▶sub string_to_list {
Subroutine string_to_list: called CC=5 SCAR=16.6
Pod undocumented
1271 27 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 27 0 27
Condition33%1/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
0027
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
1272 27 my ($str, $maybe_tail) = @_;
1273 27 ▶ my $tail = $maybe_tail // null;
Condition66%2/3
$maybe_tail // &null()
l!l&&r!l&&!r
4230
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table66%2/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
1274 27 my $i = length($str) - 1;
1275 27 ▶ while ($i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 174 27 201
1276 174 $tail = cons(substr($str, $i, 1), $tail);
1277 174 $i--;
1278 }
1279 $tail
1280 27 }
1281  
1282 1 ▶TEST { [list_to_values string_to_list "abc"] }
Subroutine __ANON__: called CC=1 SCAR=0.0
1283 60 ['a', 'b', 'c'];
1284 1 ▶TEST { list_length string_to_list "ao" }
Subroutine __ANON__: called CC=1 SCAR=0.0
1285 60 2;
1286 1 ▶TEST { list_to_string string_to_list "Hello" }
Subroutine __ANON__: called CC=1 SCAR=0.0
1287 60 'Hello';
1288  
1289 # XX HACK, COPY from FP::Array to work around circular dependency
1290 37 ▶sub array_fold_right {
Subroutine array_fold_right: called CC=3 SCAR=11.0
Pod undocumented
1291 37 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 37 0 37
1292 37 my ($fn, $tail, $a) = @_;
1293 37 my $i = @$a - 1;
1294 37 ▶ while ($i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 91 37 128
1295 91 $tail = &$fn($$a[$i], $tail);
1296 91 $i--;
1297 }
1298 $tail
1299 37 }
1300  
1301 154 ▶sub array_fold {
Subroutine array_fold: called CC=3 SCAR=11.1
Pod undocumented
1302 154 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 154 0 154
1303 154 my ($fn, $start, $ary) = @_;
1304 154 for (@$ary) {
1305 245 $start = &$fn($_, $start);
1306 }
1307 $start
1308 154 }
1309  
1310 # /HACK
1311  
1312 30 ▶sub array_to_list {
Subroutine array_to_list: called CC=4 SCAR=17.1
Pod undocumented
1313 30 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 30 0 30
Condition33%1/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
0030
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
1314 30 my ($a, $maybe_tail) = @_;
1315 30 ▶ array_fold_right(\&cons, $maybe_tail // null, $a)
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
0300
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
1316 }
1317  
1318 1 ▶TEST { list_to_string array_to_list [1, 2, 3] }
Subroutine __ANON__: called CC=1 SCAR=0.0
1319 60 '123';
1320  
1321 # XX naming correct?
1322 154 ▶sub array_to_list_reverse {
Subroutine array_to_list_reverse: called CC=4 SCAR=17.1
Pod undocumented
1323 154 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 154 0 154
Condition33%1/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
00154
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
1324 154 my ($a, $maybe_tail) = @_;
1325 154 ▶ array_fold(\&cons, $maybe_tail // null, $a)
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
01540
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
1326 }
1327  
1328 1 ▶TEST { list_to_string array_to_list_reverse [1, 2, 3] }
Subroutine __ANON__: called CC=1 SCAR=0.0
1329 60 '321';
1330  
1331 156 ▶sub list_reverse_with_tail {
Subroutine list_reverse_with_tail: called CC=3 SCAR=11.0
Pod undocumented
1332 156 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 156 0 156
1333 156 my ($l, $tail) = @_;
1334 156 ▶ while (!is_null $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_null($l)) 275 156 431
1335 275 $tail = cons car($l), $tail;
1336 275 $l = cdr $l;
1337 }
1338 $tail
1339 156 }
1340  
1341 155 ▶sub list_reverse {
Subroutine list_reverse: called CC=2 SCAR=7.1
Pod undocumented
1342 155 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 155 0 155
1343 155 my ($l) = @_;
1344 155 list_reverse_with_tail($l, $l->null)
1345 }
1346  
1347 60 *FP::List::List::reverse_with_tail = \&list_reverse_with_tail;
1348 60 *FP::List::List::reverse = \&list_reverse;
1349  
1350 1 ▶TEST { list_to_string list_reverse string_to_list "Hello" }
Subroutine __ANON__: called CC=1 SCAR=0.0
1351 60 'olleH';
1352  
1353 118 ▶sub list_strings_join {
Subroutine list_strings_join: called CC=2 SCAR=7.0
Pod undocumented
1354 118 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 118 0 118
1355 118 my ($l, $val) = @_;
1356  
1357 # now depend on FP::Array anyway. Lazily. XX hack~
1358 118 require FP::Array;
1359 118 FP::Array::array_strings_join(list_to_array($l), $val);
1360 }
1361  
1362 60 *FP::List::List::strings_join = \&list_strings_join;
1363  
1364 1 ▶TEST { list(1, 2, 3)->strings_join("-") }
Subroutine __ANON__: called CC=1 SCAR=0.0
1365 60 "1-2-3";
1366  
1367 1 ▶sub list_strings_join_reverse {
Subroutine list_strings_join_reverse: called CC=2 SCAR=7.0
Pod undocumented
1368 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
1369 1 my ($l, $val) = @_;
1370  
1371 # now depend on FP::Array anyway. Lazily. XX hack~
1372 1 require FP::Array;
1373 1 FP::Array::array_strings_join(rlist_to_array($l), $val);
1374 }
1375  
1376 60 *FP::List::List::strings_join_reverse = \&list_strings_join_reverse;
1377  
1378 1 ▶TEST { list(1, 2, 3)->strings_join_reverse("-") }
Subroutine __ANON__: called CC=1 SCAR=0.0
1379 60 "3-2-1";
1380  
1381 # write as a S-expr (trying to follow R5RS Scheme)
1382 152 ▶sub _write_sexpr {
Subroutine _write_sexpr: called CC=8 SCAR=21.0
1383 152 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 152 0 152
1384 152 my ($l, $fh, $already_in_a_list) = @_;
1385 _WRITE_SEXPR: {
1386 152 $l = force($l, 1);
1387 236 ▶ if (is_pair $l) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_pair($l)) { } 127 109 236
elsif (is_null($l)) { } 0 109 109
1388 127 ▶ xprint $fh, $already_in_a_list ? ' ' : '(';
Branch: 1 branch
BranchTrueFalseTotal
$already_in_a_list ? : 84 43 127
1389 127 _write_sexpr(car($l), $fh, 0);
1390 127 my $d = force(cdr($l), 1);
1391 127 ▶ if (is_null $d) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_null($d)) { } 37 90 127
elsif (is_pair($d)) { } 84 6 90
1392 37 xprint $fh, ')';
1393 } elsif (is_pair $d) {
1394  
1395 # tail-calling _write_sexpr $d, $fh, 1
1396 84 $l = $d;
1397 84 $already_in_a_list = 1;
1398 84 redo _WRITE_SEXPR;
1399 } else {
1400 6 xprint $fh, " . ";
1401 6 _write_sexpr($d, $fh, 0);
1402 6 xprint $fh, ')';
1403 }
1404 } elsif (is_null $l) {
1405 0 xprint $fh, "()";
1406 } else {
1407  
1408 # normal perl things; should have a show method already
1409 # for this? whatever.
1410 109 ▶ if (ref $l) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $l) { } 0 109 109
1411 0 die "don't know how to write_sexpr this: " . show($l);
1412 } else {
1413  
1414 # assume string; there's nothing else left.
1415 109 $l =~ s/"/\\"/sg;
1416 109 xprint $fh, '"', $l, '"';
1417 }
1418 }
1419 }
1420 }
1421  
1422 19 ▶sub write_sexpr {
Subroutine write_sexpr: called CC=3 SCAR=11.7
Pod undocumented
1423 19 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 19 0 19
1424 19 my ($l, $fh) = @_;
1425 19 ▶ _write_sexpr($l, $fh || *STDOUT{IO}, 0)
Condition50%1/2
$fh || *STDOUT{'IO'}
l!l
019
MC/DC0%0/1
$fh || *STDOUT{'IO'}
$fh
Truth table50%1/2
A
A= $fh
Aresult
11
00
1426 }
1427  
1428 1 ▶TEST_STDOUT { write_sexpr cons("123", cons("4", null)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1429 60 '("123" "4")';
1430 1 ▶TEST_STDOUT { write_sexpr(string_to_list "Hello \"World\"") }
Subroutine __ANON__: called CC=1 SCAR=0.0
1431 60 '("H" "e" "l" "l" "o" " " "\"" "W" "o" "r" "l" "d" "\"")';
1432 1 ▶TEST_STDOUT { write_sexpr(cons 1, 2) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1433 60 '("1" . "2")';
1434  
1435 #TEST_STDOUT{ write_sexpr cons(1, cons(cons(2, undef), undef))}
1436 # '';
1437 # -> XX should print #f or something for undef ! Not give exception.
1438 1 ▶TEST_STDOUT { write_sexpr cons(1, cons(cons(2, null), null)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1439 60 '("1" ("2"))';
1440  
1441 60 *FP::List::List::write_sexpr = \&write_sexpr;
1442  
1443 # adapted copy of stream_map_with_tail, as usual...
1444 11 ▶sub list_map_with_tail {
Subroutine list_map_with_tail: called CC=3 SCAR=11.0
Pod undocumented
1445 11 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 11 0 11
1446 11 my ($fn, $l, $tail) = @_;
1447 11 FORCE $l; # be careful as usual, right?
1448 11 ▶ is_null($l)
Branch: 1 branch
BranchTrueFalseTotal
is_null($l) ? : 4 7 11
1449 ? $tail
1450 : cons(&$fn(car $l), list_map_with_tail($fn, cdr($l), $tail))
1451 }
1452  
1453 60 *FP::List::List::map_with_tail = flip2of3 \&list_map_with_tail;
1454  
1455 0 ▶sub improper_last {
Subroutine improper_last: not called CC=3 SCAR=24.8
Pod undocumented
1456 0 ▶ @_ == 1 or fp_croak_arity "1";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
1457 0 my ($l) = @_;
1458 0 ▶ while (is_pair($l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_pair($l)) 0 0 0
1459 0 $l = cdr($l);
1460 }
1461 $l
1462 0 }
1463  
1464 60 *FP::List::List::improper_last = \&improper_last;
1465  
1466 3 ▶sub improper_map {
Subroutine improper_map: called CC=6 SCAR=21.6
Pod undocumented
1467 3 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2-3") 3 0 3
Condition33%1/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
030
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table33%1/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
1468 3 my ($fn, $l, $maybe_tail) = @_;
1469 3 FORCE $l; # be careful as usual, right?
1470 3 ▶ if (is_null($l)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_null($l)) { } 0 3 3
elsif (is_pair($l)) { } 2 1 3
1471 0 ▶ $maybe_tail // $l
Condition0%0/3
$maybe_tail // $l
l!l&&r!l&&!r
000
MC/DC0%0/2
$maybe_tail // $l
$maybe_tail $l
Truth table0%0/3
A // B
A= $maybe_tailB= $l
ABresult
1X1
011
000
1472 } elsif (is_pair($l)) {
1473 2 cons($fn->(car $l), improper_map($fn, cdr($l), $maybe_tail))
1474 } else {
1475 1 $fn->($l)
1476 }
1477 }
1478  
1479 1 ▶sub FP::List::List::improper_map {
Subroutine improper_map: called CC=3 SCAR=12.0
1480 1 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2-3") 1 0 1
Condition33%1/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
100
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table33%1/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
1481 1 my ($l, $fn, $maybe_tail) = @_;
1482 1 @_ = ($fn, $l, $maybe_tail);
1483 1 goto \&improper_map
1484 }
1485  
1486 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
1487 3 ▶ write_sexpr cons(1, cons(2, 3))->improper_map(sub { $_[0] * 2 })
Subroutine __ANON__: called CC=1 SCAR=0.0
1488 1 }
1489 60 '("2" "4" . "6")';
1490  
1491 12 ▶sub improper_filtermap {
Subroutine improper_filtermap: called CC=11 SCAR=27.8
Pod undocumented
1492 12 __ 'improper_filtermap($fn, $l, $maybe_tail):
1493 If $fn returns (), the position is discarded in the resulting
1494 sequence. $fn cannot return multiple values, though.';
1495 12 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2-3") 12 0 12
Condition33%1/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
0120
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table33%1/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
1496 12 my ($fn, $l, $maybe_tail) = @_;
1497 12 FORCE $l; # be careful as usual, right?
1498 12 ▶ if (is_null($l)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_null($l)) { } 0 12 12
elsif (is_pair($l)) { } 9 3 12
1499 0 ▶ $maybe_tail // $l
Condition0%0/3
$maybe_tail // $l
l!l&&r!l&&!r
000
MC/DC0%0/2
$maybe_tail // $l
$maybe_tail $l
Truth table0%0/3
A // B
A= $maybe_tailB= $l
ABresult
1X1
011
000
1500 } elsif (is_pair($l)) {
1501 9 my @v = $fn->(car $l);
1502 9 my $r = improper_filtermap($fn, cdr($l), $maybe_tail);
1503 9 ▶ if (@v == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@v == 1) { } 8 1 9
elsif (not @v) { } 1 0 1
1504 8 cons($v[0], $r)
1505 } elsif (!@v) {
1506 1 $r
1507 } else {
1508 0 die "not supporting multiple value returns from \$fn";
1509 }
1510 } else {
1511 3 my @v = $fn->($l);
1512 3 ▶ if (@v == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@v == 1) { } 2 1 3
elsif (not @v) { } 1 0 1
1513 2 $v[0]
1514 } elsif (!@v) {
1515  
1516 # Converting a possibly single input item into an empty
1517 # list. (Only if improper_filtermap were itself perhaps_*,
1518 # we could handle this case otherwise. Perl list context
1519 # is magic?)
1520 1 ▶ $maybe_tail // null
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
010
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
1521 } else {
1522 0 die "not supporting multiple value returns from \$fn";
1523 }
1524 }
1525 }
1526  
1527 3 ▶sub FP::List::List::improper_filtermap {
Subroutine improper_filtermap: called CC=3 SCAR=12.0
1528 3 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2-3") 3 0 3
Condition33%1/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
300
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table33%1/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
1529 3 my ($l, $fn, $maybe_tail) = @_;
1530 3 @_ = ($fn, $l, $maybe_tail);
1531 3 goto \&improper_filtermap
1532 }
1533  
1534 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
1535 write_sexpr cons(1, cons(2, cons(3, 4)))
1536 4 ▶ ->improper_filtermap(sub { $_[0] * 2 })
Subroutine __ANON__: called CC=1 SCAR=0.0
1537 1 }
1538 60 '("2" "4" "6" . "8")';
1539  
1540 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
1541 write_sexpr cons(1, cons(2, cons(3, 4)))->improper_filtermap(
1542 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1543 4 my $v = $_[0] * 2;
1544 4 ▶ $v == 6 ? () : $v
Branch: 1 branch
BranchTrueFalseTotal
$v == 6 ? : 1 3 4
1545 }
1546 )
1547 1 }
1548 60 '("2" "4" . "8")';
1549  
1550 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
1551 write_sexpr cons(1, cons(2, cons(3, 4)))->improper_filtermap(
1552 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1553 4 my $v = $_[0] * 2;
1554 4 ▶ $v == 8 ? () : $v
Branch: 1 branch
BranchTrueFalseTotal
$v == 8 ? : 1 3 4
1555 }
1556 )
1557 1 }
1558 60 '("2" "4" "6")';
1559  
1560 # mostly COPY-PASTE from improper_filtermap
1561 0 ▶sub list_filtermap {
Subroutine list_filtermap: not called CC=8 SCAR=42.8
Pod undocumented
1562 0 __ 'list_filtermap($fn, $l, $maybe_tail):
1563 If $fn returns (), the position is discarded in the resulting
1564 sequence. $fn cannot return multiple values, though.';
1565 0 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2-3") 0 0 0
Condition0%0/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
1566 0 my ($fn, $l, $maybe_tail) = @_;
1567 0 FORCE $l; # be careful as usual, right?
1568 0 ▶ if (is_null($l)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_null($l)) { } 0 0 0
elsif (is_pair($l)) { } 0 0 0
1569 0 ▶ $maybe_tail // $l
Condition0%0/3
$maybe_tail // $l
l!l&&r!l&&!r
000
MC/DC0%0/2
$maybe_tail // $l
$maybe_tail $l
Truth table0%0/3
A // B
A= $maybe_tailB= $l
ABresult
1X1
011
000
1570 } elsif (is_pair($l)) {
1571 0 my @v = $fn->(car $l);
1572 0 my $r = improper_filtermap($fn, cdr($l), $maybe_tail);
1573 0 ▶ if (@v == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@v == 1) { } 0 0 0
elsif (not @v) { } 0 0 0
1574 0 cons($v[0], $r)
1575 } elsif (!@v) {
1576 0 $r
1577 } else {
1578 0 die "not supporting multiple value returns from \$fn";
1579 }
1580 } else {
1581 0 die "improper list"
1582 }
1583 }
1584  
1585 0 ▶sub FP::List::List::filtermap {
Subroutine filtermap: not called CC=3 SCAR=24.8
1586 0 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2-3") 0 0 0
Condition0%0/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
1587 0 my ($l, $fn, $maybe_tail) = @_;
1588 0 @_ = ($fn, $l, $maybe_tail);
1589 0 goto \&list_filtermap
1590 }
1591  
1592 6 ▶sub list_zip2 {
Subroutine list_zip2: called CC=4 SCAR=13.9
Pod undocumented
1593 6 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 6 0 6
1594 6 my ($l, $m) = @_;
1595 6 ▶ ( is_null($l) ? $l
Branch: 2 branches
BranchTrueFalseTotal
is_null($l) ? : 1 5 6
is_null($m) ? : 1 4 5
1596 : is_null($m) ? $m
1597 : cons([car($l), car($m)], list_zip2(cdr($l), cdr($m))))
1598 }
1599  
1600 1 ▶TEST { list_to_array list_zip2 list(qw(a b c)), list(2, 3) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1601 60 [[a => 2], [b => 3]];
1602  
1603 1 ▶TEST { list_to_array list_zip2 list(qw(a b)), list(2, 3, 4) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1604 60 [[a => 2], [b => 3]];
1605  
1606 60 *FP::List::List::zip = \&list_zip2; # XX make n-ary
1607  
1608 3 ▶sub list_to_alist {
Subroutine list_to_alist: called CC=3 SCAR=11.0
Pod undocumented
1609 3 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3 0 3
1610 3 my ($l) = @_;
1611 3 ▶ is_null($l) ? $l : do {
Branch: 1 branch
BranchTrueFalseTotal
is_null($l) ? : 1 2 3
1612 2 my ($k, $l2) = $l->first_and_rest;
1613 2 my ($v, $l3) = $l2->first_and_rest;
1614 2 cons(cons($k, $v), list_to_alist($l3))
1615 }
1616 }
1617 60 *FP::List::List::alist = \&list_to_alist;
1618  
1619 1 ▶TEST_STDOUT { list(a => 10, b => 20)->alist->write_sexpr }
Subroutine __ANON__: called CC=1 SCAR=0.0
1620 60 '(("a" . "10") ("b" . "20"))';
1621  
1622 101 ▶sub make_filter {
Subroutine make_filter: called CC=1 SCAR=0.0
Pod undocumented
1623 101 my ($is_stream) = @_;
1624 101 my $filter;
1625 686 ▶ $filter = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1626 686 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 686 0 686
1627 686 my ($fn, $l) = @_;
1628 686 ▶ weaken $_[1] if $is_stream;
Branch: 1 branch
BranchTrueFalseTotal
if $is_stream 289 397 686
1629 650 ▶ lazy_if {
Subroutine __ANON__: called CC=3 SCAR=11.0
1630 60 ▶ no warnings 'recursion'; # XXX this should be tail calling???
Subroutine BEGIN: called CC=1 SCAR=0.0
1631 650 $l = force $l;
1632 650 ▶ is_null($l) ? $l : do {
Branch: 1 branch
BranchTrueFalseTotal
is_null($l) ? : 128 522 650
1633 522 my ($a, $r) = $l->first_and_rest;
1634 60 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1635 522 my $r2 = &$filter($fn, $r);
1636 522 ▶ &$fn($a) ? cons($a, $r2) : $r2
Branch: 1 branch
BranchTrueFalseTotal
&$fn($a) ? : 360 162 522
1637 }
1638 }
1639 686 $is_stream;
1640 101 };
1641 101 Weakened($filter)
1642 }
1643  
1644 sub list_filter;
1645 60 *list_filter = make_filter(0);
1646  
1647 60 *FP::List::List::filter = flip \&list_filter;
1648  
1649 # almost-COPY of filter
1650 101 ▶sub make_filter_with_tail {
Subroutine make_filter_with_tail: called CC=1 SCAR=4.3
Pod undocumented
1651 101 my ($is_stream) = @_;
1652 101 my $filter_with_tail;
1653 0 ▶ $filter_with_tail = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1654 0 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 0 0 0
1655 0 my ($fn, $l, $tail) = @_;
1656 0 ▶ weaken $_[1] if $is_stream;
Branch: 1 branch
BranchTrueFalseTotal
if $is_stream 0 0 0
1657 0 ▶ lazy_if {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1658 0 $l = force $l;
1659 0 ▶ is_null($l) ? $tail : do {
Branch: 1 branch
BranchTrueFalseTotal
is_null($l) ? : 0 0 0
1660 0 my $a = car $l;
1661 0 my $r = &$filter_with_tail($fn, cdr($l), $tail);
1662 0 ▶ &$fn($a) ? cons($a, $r) : $r
Branch: 1 branch
BranchTrueFalseTotal
&$fn($a) ? : 0 0 0
1663 }
1664 }
1665 0 $is_stream;
1666 101 };
1667 101 Weakened($filter_with_tail)
1668 }
1669  
1670 sub list_filter_with_tail;
1671 60 *list_filter_with_tail = make_filter_with_tail(0);
1672 60 *FP::List::List::filter_with_tail = flip2of3 \&list_filter_with_tail;
1673  
1674 303 ▶sub list_map {
Subroutine list_map: called CC=3 SCAR=11.0
Pod undocumented
1675 303 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 303 0 303
1676 303 my ($fn, $l) = @_;
1677 is_null($l) ? $l : cons(
1678 scalar &$fn(car $l),
1679 303 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
is_null($l) ? : 82 221 303
1680 60 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1681 221 list_map($fn, cdr $l)
1682 }
1683 )
1684 }
1685  
1686 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1687 1 ▶ list_to_array list_map sub { $_[0] * $_[0] }, list 1, 2, -3
Subroutine __ANON__: called CC=1 SCAR=0.0
1688 }
1689 60 [1, 4, 9];
1690  
1691 # n-ary map
1692 4 ▶sub list_mapn {
Subroutine list_mapn: called CC=3 SCAR=11.0
Pod undocumented
1693 4 my $fn = shift;
1694 4 for (@_) {
1695 8 ▶ return $_ if is_null $_
Branch: 1 branch
BranchTrueFalseTotal
if is_null($_) 2 6 8
1696 }
1697 2 cons(&$fn(map { car $_} @_), list_mapn($fn, map { cdr $_} @_))
1698 }
1699  
1700 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=1.2
1701 0 ▶ list_to_array list_mapn(sub { [@_] }, array_to_list([1, 2, 3]),
Subroutine __ANON__: not called CC=1 SCAR=1.2
1702 string_to_list(""))
1703 }
1704 60 [];
1705 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1706 1 ▶ list_to_array list_mapn(sub { [@_] }, array_to_list([1, 2, 3]),
Subroutine __ANON__: called CC=1 SCAR=0.0
1707 string_to_list("ab"))
1708 }
1709 60 [[1, 'a'], [2, 'b']];
1710  
1711 80 ▶sub FP::List::List::map {
Subroutine map: called CC=3 SCAR=11.4
1712 80 ▶ @_ >= 2 or fp_croak_arity ">= 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 or fp_croak_arity(">= 2") 80 0 80
1713 80 my $l = shift;
1714 80 my $fn = shift;
1715 80 ▶ @_ ? list_mapn($fn, $l, @_) : list_map($fn, $l)
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 80 80
1716 }
1717  
1718 4 ▶sub list_map_with_index_ {
Subroutine list_map_with_index_: called CC=3 SCAR=11.0
Pod undocumented
1719 4 my $i = shift;
1720 4 my $fn = shift;
1721 4 for (@_) {
1722 4 ▶ return $_ if is_null $_
Branch: 1 branch
BranchTrueFalseTotal
if is_null($_) 1 3 4
1723 }
1724 3 cons(&$fn($i, map { car $_} @_),
1725 3 list_map_with_index_($i + 1, $fn, map { cdr $_} @_))
1726 }
1727  
1728 1 ▶sub list_map_with_index {
Subroutine list_map_with_index: called CC=2 SCAR=7.2
Pod undocumented
1729 1 ▶ @_ >= 2 or fp_croak_arity ">= 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 or fp_croak_arity(">= 2") 1 0 1
1730 1 list_map_with_index_(0, @_)
1731 }
1732  
1733 1 ▶sub FP::List::List::map_with_index {
Subroutine map_with_index: called CC=2 SCAR=7.0
1734 1 ▶ @_ >= 2 or fp_croak_arity ">= 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 or fp_croak_arity(">= 2") 1 0 1
1735 1 my $l = shift;
1736 1 my $fn = shift;
1737 1 list_map_with_index($fn, $l, @_)
1738 }
1739  
1740 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1741 1 ▶ list(1, 2, 20)->map_with_index(sub { [@_] })->array
Subroutine __ANON__: called CC=1 SCAR=0.0
1742 }
1743 60 [[0, 1], [1, 2], [2, 20]];
1744  
1745 7 ▶sub list_map_with_islast {
Subroutine list_map_with_islast: called CC=5 SCAR=16.1
Pod undocumented
1746 7 ▶ @_ >= 2 or fp_croak_arity ">= 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 or fp_croak_arity(">= 2") 7 0 7
1747 7 my $fn = shift;
1748 7 ▶ my @rest = map { is_null($_) ? return null : rest $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
is_null($_) ? : 2 8 10
1749 5 my $is_last = '';
1750  
1751 # return *number* of ending streams, ok? XX this is unlike
1752 # array_map_with_islast
1753 5 ▶ for (@rest) { $is_last++ if is_null $_ }
Branch: 1 branch
BranchTrueFalseTotal
if is_null($_) 2 5 7
1754 5 cons(&$fn($is_last, map { $_->first } @_), list_map_with_islast($fn, @rest))
1755 }
1756  
1757 2 ▶sub FP::List::List::map_with_islast {
Subroutine map_with_islast: called CC=1 SCAR=0.0
1758 2 my ($l0, $fn, @l) = @_;
1759 2 list_map_with_islast($fn, $l0, @l)
1760 }
1761  
1762 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1763 1 ▶ list(1, 2, 20)->map_with_islast(sub { $_[0] })->array
Subroutine __ANON__: called CC=1 SCAR=0.0
1764 }
1765 60 ['', '', 1];
1766  
1767 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1768 1 ▶ list(1, 2, 20)->map_with_islast(sub { [@_] }, list "b", "c")->array
Subroutine __ANON__: called CC=1 SCAR=0.0
1769 }
1770 60 [['', 1, "b"], [1, 2, "c"]];
1771  
1772 # left fold, sometimes called `foldl` or `reduce`
1773 # (XX adapted copy from Stream.pm)
1774 362 ▶sub list_fold {
Subroutine list_fold: called CC=3 SCAR=11.0
Pod undocumented
1775 362 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 362 0 362
1776 362 my ($fn, $start, $l) = @_;
1777 362 my $v;
1778 LP: {
1779 362 ▶ if (is_pair $l) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_pair($l)) 5319 362 5681
1780 5319 ($v, $l) = first_and_rest $l;
1781 5319 $start = &$fn($v, $start);
1782 5319 redo LP;
1783 }
1784 }
1785 $start
1786 362 }
1787  
1788 60 *FP::List::List::fold = rot3left \&list_fold;
1789  
1790 1 ▶TEST { list_fold(\&cons, null, list(1, 2))->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
1791 60 [2, 1];
1792  
1793 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1794 1 ▶ list(1, 2, 3)->map(sub { $_[0] + 1 })->fold(sub { $_[0] + $_[1] }, 0)
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1795 }
1796 60 9;
1797  
1798 104 ▶sub list_fold_right {
Subroutine list_fold_right: called CC=4 SCAR=14.1
Pod undocumented
1799 104 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 104 0 104
1800 104 my ($fn, $start, $l) = @_;
1801 104 ▶ if (is_pair $l) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_pair($l)) { } 76 28 104
elsif (is_null($l)) { } 28 0 28
1802 60 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1803 76 my $rest = list_fold_right($fn, $start, cdr $l);
1804 76 &$fn(car($l), $rest)
1805 } elsif (is_null $l) {
1806 28 $start
1807 } else {
1808 0 die "improper list"
1809 }
1810 }
1811  
1812 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1813 3 ▶ list_fold_right sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1814 3 my ($v, $res) = @_;
1815 3 [$v, @$res]
1816 1 }, [], list(4, 5, 9)
1817 }
1818 60 [4, 5, 9];
1819  
1820 5 ▶sub FP::List::List::fold_right {
Subroutine fold_right: called CC=2 SCAR=7.0
1821 5 my $l = shift;
1822 5 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 5 0 5
1823 5 my ($fn, $start) = @_;
1824 5 list_fold_right($fn, $start, $l)
1825 }
1826  
1827 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1828 1 ▶ list(1, 2, 3)->map(sub { $_[0] + 1 })->fold_right(sub { $_[0] + $_[1] }, 0)
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1829 }
1830 60 9;
1831  
1832 # same as fold_right but passes the whole list remainder instead of
1833 # only the car to the function
1834 4 ▶sub list_pair_fold_right {
Subroutine list_pair_fold_right: called CC=4 SCAR=14.1
Pod undocumented
1835 4 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 4 0 4
1836 4 my ($fn, $start, $l) = @_;
1837 4 ▶ if (is_pair $l) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_pair($l)) { } 3 1 4
elsif (is_null($l)) { } 1 0 1
1838 60 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1839 3 my $rest = list_pair_fold_right($fn, $start, cdr $l);
1840 3 &$fn($l, $rest)
1841 } elsif (is_null $l) {
1842 1 $start
1843 } else {
1844 0 die "improper list"
1845 }
1846 }
1847  
1848 60 *FP::List::List::pair_fold_right = rot3left \&list_pair_fold_right;
1849  
1850 1 ▶TEST_STDOUT { list(5, 6, 9)->pair_fold_right(\&cons, null)->write_sexpr }
Subroutine __ANON__: called CC=1 SCAR=0.0
1851 60 '(("5" "6" "9") ("6" "9") ("9"))';
1852  
1853 # no list_ prefix? It doesn't consume lists. Although, still FP::List
1854 # specific. But there's no way to shorten it by way of method
1855 # calling. For FP::Stream, call it stream_unfold, similarly for other
1856 # *'special'* kinds of lists?
1857  
1858 # unfold p f g seed [tail-gen] -> list
1859  
1860 # p: predicate function, wenn true for seed stops production;
1861 # f: function to produce output value from seed;
1862 # g: function to produce the next seed value;
1863 # seed: the initial seed value;
1864 # tail-gen: optional function to map the last seed value, the value
1865 # `null` is taken otherwise
1866  
1867 # For more documentation, see
1868 # http://srfi.schemers.org/srfi-1/srfi-1.html#FoldUnfoldMap
1869  
1870 30 ▶sub unfold {
Subroutine unfold: called CC=5 SCAR=16.3
Pod undocumented
1871 30 ▶ @_ == 4 or @_ == 5 or fp_croak_arity "4 or 5";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or @_ == 5 or fp_croak_arity("4 or 5") 30 0 30
Condition66%2/3
@_ == 4 or @_ == 5
l!l&&r!l&&!r
7230
MC/DC0%0/2
@_ == 4 or @_ == 5
@_ == 4 @_ == 5
Truth table66%2/3
A or B
A= @_ == 4B= @_ == 5
ABresult
1X1
011
000
1872 30 my ($p, $f, $g, $seed, $maybe_tail_gen) = @_;
1873 30 ▶ &$p($seed)
Branch: 2 branches
BranchTrueFalseTotal
&$p($seed) ? : 8 22 30
defined $maybe_tail_gen ? : 1 7 8
1874 ? (defined $maybe_tail_gen ? &$maybe_tail_gen($seed) : null)
1875 : cons(&$f($seed), unfold($p, $f, $g, &$g($seed), $maybe_tail_gen));
1876 }
1877  
1878 1 ▶TEST { unfold(\&is_zero, \&inc, \&dec, 5)->array } [6, 5, 4, 3, 2];
Subroutine __ANON__: called CC=1 SCAR=0.0
1879 1 ▶TEST { unfold(\&is_zero, \&inc, \&dec, 5, \&list)->array } [6, 5, 4, 3, 2, 0];
Subroutine __ANON__: called CC=1 SCAR=0.0
1880  
1881 # unfold-right p f g seed [tail] -> list
1882  
1883 2 ▶sub unfold_right {
Subroutine unfold_right: called CC=5 SCAR=16.2
Pod undocumented
1884 2 ▶ @_ == 4 or @_ == 5 or fp_croak_arity "4 or 5";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or @_ == 5 or fp_croak_arity("4 or 5") 2 0 2
Condition66%2/3
@_ == 4 or @_ == 5
l!l&&r!l&&!r
110
MC/DC0%0/2
@_ == 4 or @_ == 5
@_ == 4 @_ == 5
Truth table66%2/3
A or B
A= @_ == 4B= @_ == 5
ABresult
1X1
011
000
1885 2 my ($p, $f, $g, $seed, $maybe_tail) = @_;
1886 2 ▶ my $tail = @_ == 5 ? $maybe_tail : null;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 5 ? : 1 1 2
1887 LP: {
1888 2 ▶ if (&$p($seed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$p($seed)) { } 2 10 12
1889 2 $tail
1890 } else {
1891 10 ($seed, $tail) = (&$g($seed), cons(&$f($seed), $tail));
1892 10 redo LP;
1893 }
1894 }
1895 }
1896  
1897 1 ▶TEST { unfold_right(\&is_zero, \&inc, \&dec, 5)->array } [2, 3, 4, 5, 6];
Subroutine __ANON__: called CC=1 SCAR=0.0
1898 1 ▶TEST { unfold_right(\&is_zero, \&inc, \&dec, 5, list 99)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
1899 60 [2, 3, 4, 5, 6, 99];
1900  
1901 20 ▶sub list_append {
Subroutine list_append: called CC=3 SCAR=11.0
Pod undocumented
1902 20 ▶ my $l = @_ ? shift : null;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 19 1 20
1903 20 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 20 20 40
1904 20 my $l2 = shift;
1905 20 $l = list_fold_right(\&cons, $l2, $l)
1906 }
1907 $l
1908 20 }
1909  
1910 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1911 1 list_append list(1, 2, 3), list("a", "b"), list(4, 5)
1912 }
1913 60 list(1, 2, 3, 'a', 'b', 4, 5);
1914  
1915 1 ▶TEST {list_append} list();
Subroutine __ANON__: called CC=1 SCAR=0.0
1916  
1917 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1918 1 list_to_array list_append(array_to_list(["a", "b"]), array_to_list([1, 2]))
1919 }
1920 60 ['a', 'b', 1, 2];
1921  
1922 60 *FP::List::List::append = \&list_append;
1923  
1924 1 ▶TEST { array_to_list(["a", "b"])->append(array_to_list([1, 2]))->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
1925 60 ['a', 'b', 1, 2];
1926  
1927 2 ▶sub list_to_perlstring {
Subroutine list_to_perlstring: called CC=2 SCAR=6.9
Pod undocumented
1928 2 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 2 0 2
1929 2 my ($l) = @_;
1930 list_to_string cons(
1931 "'",
1932 12 ▶ list_fold_right sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1933 12 my ($c, $rest) = @_;
1934 12 my $out = cons($c, $rest);
1935 12 ▶ if ($c eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq q[']) { } 1 11 12
1936 1 cons("\\", $out)
1937 } else {
1938 11 $out
1939 }
1940 },
1941 2 cons("'", null),
1942 $l
1943 )
1944 }
1945  
1946 1 ▶TEST { list_to_perlstring string_to_list "Hello" }
Subroutine __ANON__: called CC=1 SCAR=0.0
1947 60 "'Hello'";
1948 1 ▶TEST { list_to_perlstring string_to_list "Hello's" }
Subroutine __ANON__: called CC=1 SCAR=0.0
1949 60 q{'Hello\'s'};
1950  
1951 60 *FP::List::List::perlstring = \&list_to_perlstring;
1952  
1953 3 ▶sub list_butlast {
Subroutine list_butlast: called CC=4 SCAR=14.5
Pod undocumented
1954 3 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3 0 3
1955 3 my ($l) = @_;
1956 3 ▶ if (is_null($l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($l)) { } 0 3 3
1957 0 die "butlast: got empty list"
1958  
1959 # XX could make use of OO for the distinction instead
1960 } else {
1961 3 my ($a, $r) = $l->first_and_rest;
1962 3 ▶ is_null($r) ? $r : cons($a, list_butlast($r))
Branch: 1 branch
BranchTrueFalseTotal
is_null($r) ? : 1 2 3
1963 }
1964 }
1965  
1966 60 *FP::List::List::butlast = \&list_butlast;
1967  
1968 1 ▶TEST { list(3, 4, 5)->butlast->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
1969 60 [3, 4];
1970 1 ▶TEST_EXCEPTION { list()->butlast->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
1971 60 'can\'t take the butlast of the empty list';
1972  
1973 3 ▶sub list_drop_while {
Subroutine list_drop_while: called CC=4 SCAR=13.9
Pod undocumented
1974 3 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 3 0 3
1975 3 my ($pred, $l) = @_;
1976 3 ▶ while (!is_null $l and &$pred(car $l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not is_null($l) and &$pred(car($l))) 19 3 22
Condition100%3/3
not is_null($l) and &$pred(car($l))
!ll&&!rl&&r
1219
MC/DC100%2/2
not is_null($l) and &$pred(car($l))
not is_null($l) &$pred(car($l))
Truth table100%3/3
not(A) and B
A= not is_null($l)B= &$pred(car($l))
ABresult
0X0
100
111
1977 19 $l = cdr $l;
1978 }
1979 $l
1980 3 }
1981  
1982 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1983 11 ▶ list_to_string list_drop_while(sub { $_[0] ne 'X' },
Subroutine __ANON__: called CC=1 SCAR=0.0
1984 1 string_to_list "Hello World")
1985 }
1986 60 "";
1987 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1988 5 ▶ list_to_string list_drop_while(sub { $_[0] ne 'o' },
Subroutine __ANON__: called CC=1 SCAR=0.0
1989 1 string_to_list "Hello World")
1990 }
1991 60 "o World";
1992  
1993 60 *FP::List::List::drop_while = flip \&list_drop_while;
1994  
1995 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
1996 1 ▶ string_to_list("Hello World")->drop_while(sub { $_[0] ne 'o' })->string
Subroutine __ANON__: called CC=1 SCAR=0.0
1997 }
1998 60 "o World";
1999  
2000 9 ▶sub list_rtake_while_and_rest {
Subroutine list_rtake_while_and_rest: called CC=4 SCAR=13.9
Pod undocumented
2001 9 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 9 0 9
2002 9 my ($pred, $l) = @_;
2003 9 my $res = $l->null;
2004 9 my $c;
2005 9 ▶ while (!is_null $l and &$pred($c = $l->car)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not is_null($l) and &$pred($c = $l->car)) 23 9 32
Condition100%3/3
not is_null($l) and &$pred($c = $l->car)
!ll&&!rl&&r
2723
MC/DC100%2/2
not is_null($l) and &$pred($c = $l->car)
not is_null($l) &$pred($c = $l->car)
Truth table100%3/3
not(A) and B
A= not is_null($l)B= &$pred($c = $l->car)
ABresult
0X0
100
111
2006 23 $res = cons $c, $res;
2007 23 $l = $l->cdr;
2008 }
2009 9 ($res, $l)
2010 }
2011  
2012 60 *FP::List::List::rtake_while_and_rest = flip \&list_rtake_while_and_rest;
2013  
2014 1 ▶sub list_rtake_while {
Subroutine list_rtake_while: called CC=2 SCAR=7.0
Pod undocumented
2015 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
2016 1 my ($pred, $l) = @_;
2017 1 my ($res, $rest) = list_rtake_while_and_rest($pred, $l);
2018 1 $res
2019 }
2020  
2021 60 *FP::List::List::rtake_while = flip \&list_rtake_while;
2022  
2023 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2024 1 list_to_string list_reverse(list_rtake_while \&char_is_alphanumeric,
2025 string_to_list "Hello World")
2026 }
2027 60 'Hello';
2028  
2029 8 ▶sub list_take_while_and_rest {
Subroutine list_take_while_and_rest: called CC=2 SCAR=7.0
Pod undocumented
2030 8 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 8 0 8
2031 8 my ($pred, $l) = @_;
2032 8 my ($rres, $rest) = list_rtake_while_and_rest($pred, $l);
2033 8 (list_reverse($rres), $rest)
2034 }
2035  
2036 60 *FP::List::List::take_while_and_rest = flip \&list_take_while_and_rest;
2037  
2038 8 ▶sub list_take_while {
Subroutine list_take_while: called CC=2 SCAR=7.0
Pod undocumented
2039 8 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 8 0 8
2040 8 my ($pred, $l) = @_;
2041 8 my ($res, $rest) = list_take_while_and_rest($pred, $l);
2042 8 $res
2043 }
2044  
2045 60 *FP::List::List::take_while = flip \&list_take_while;
2046  
2047 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2048 5 ▶ list_to_string list_take_while(sub { $_[0] ne 'o' },
Subroutine __ANON__: called CC=1 SCAR=0.0
2049 1 string_to_list "Hello World")
2050 }
2051 60 "Hell";
2052 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2053 2 ▶ list_to_string list_take_while(sub { $_[0] eq 'H' },
Subroutine __ANON__: called CC=1 SCAR=0.0
2054 1 string_to_list "Hello World")
2055 }
2056 60 "H";
2057 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2058 1 ▶ list_to_string list_take_while(sub {1}, string_to_list "Hello World")
Subroutine __ANON__: called CC=1 SCAR=0.0
2059 }
2060 60 "Hello World";
2061 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2062 1 ▶ list_to_string list_take_while(sub {0}, string_to_list "Hello World")
Subroutine __ANON__: called CC=1 SCAR=0.0
2063 }
2064 60 "";
2065  
2066 3 ▶sub list_last {
Subroutine list_last: called CC=3 SCAR=11.0
Pod undocumented
2067 3 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3 0 3
2068 3 my ($v) = @_;
2069 LIST_LAST: {
2070 3 my ($a, $r) = $v->first_and_rest;
2071 4 ▶ if (is_null $r) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($r)) { } 2 2 4
2072 2 $a
2073 } else {
2074 2 $v = $r;
2075 2 redo LIST_LAST;
2076 }
2077 }
2078 }
2079  
2080 60 *FP::List::List::last = \&list_last;
2081  
2082 1 ▶TEST { list(qw(a b c))->last } 'c';
Subroutine __ANON__: called CC=1 SCAR=0.0
2083 1 ▶TEST { list(qw(a))->last } 'a';
Subroutine __ANON__: called CC=1 SCAR=0.0
2084 1 ▶TEST_EXCEPTION { list(qw())->last }
Subroutine __ANON__: called CC=1 SCAR=0.0
2085 60 "can't take the first_and_rest of the empty list";
2086  
2087 # XX add stream_last to Stream.pm (only change as usual: deallocate head)
2088  
2089 # XX: add list_last_pair (see SRFI 1)
2090  
2091 9 ▶sub list_every {
Subroutine list_every: called CC=5 SCAR=16.3
Pod undocumented
2092 9 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 9 0 9
2093 9 my ($pred, $l) = @_;
2094 LP: {
2095 9 ▶ if (is_pair $l) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_pair($l)) { } 30 4 34
elsif (is_null($l)) { } 4 0 4
2096 30 my $r = &$pred(car $l);
2097 30 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 25 5 30
2098 25 $l = cdr $l;
2099 25 redo LP;
2100 } else {
2101 5 $r
2102 }
2103 } elsif (is_null $l) {
2104 4 1
2105 } else {
2106  
2107 # improper list
2108 # (XX check value instead? But that would be improper_every.)
2109 #0
2110 0 die "improper list"
2111 }
2112 }
2113 }
2114  
2115 60 *FP::List::List::every = flip \&list_every;
2116  
2117 # XXX do we want this alias? Or do we just want to rename every to
2118 # all?
2119 sub list_all;
2120 60 *list_all = \&list_every;
2121  
2122 60 *FP::List::List::all = flip \&list_every;
2123  
2124 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2125 [
2126 map {
2127 1 ▶ list_every sub { $_[0] > 0 }, $_
Subroutine __ANON__: called CC=1 SCAR=0.0
2128 } list(1, 2, 3),
2129 list(1, 0, 3),
2130 list(),
2131 ]
2132 }
2133 60 [1, '', 1];
2134  
2135 60 ▶use FP::Char 'char_is_alphanumeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
2136  
2137 1 ▶TEST { string_to_list("Hello")->every(\&char_is_alphanumeric) }
Subroutine __ANON__: called CC=1 SCAR=0.0
2138 60 1;
2139 1 ▶TEST { string_to_list("Hello ")->every(\&char_is_alphanumeric) }
Subroutine __ANON__: called CC=1 SCAR=0.0
2140 60 '';
2141  
2142 # none is defined in FP::Abstract::Sequence
2143 1 ▶TEST { string_to_list("Hello")->none(\&char_is_alphanumeric) }
Subroutine __ANON__: called CC=1 SCAR=0.0
2144 60 0;
2145 1 ▶TEST { string_to_list(" -()&")->none(\&char_is_alphanumeric) }
Subroutine __ANON__: called CC=1 SCAR=0.0
2146 60 1;
2147 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2148 1 my $z = 0;
2149 my $r = string_to_list(" -()&a")
2150 1 ▶ ->none(sub { $z++; char_is_alphanumeric $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
2151 1 [$z, $r]
2152 }
2153 60 [6, 0];
2154 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2155 1 my $z = 0;
2156 my $r = string_to_list(" a-()&a")
2157 1 ▶ ->none(sub { $z++; char_is_alphanumeric $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
2158 1 [$z, $r]
2159 }
2160 60 [2, 0];
2161  
2162 21 ▶sub list_any {
Subroutine list_any: called CC=5 SCAR=16.3
Pod undocumented
2163 21 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 21 0 21
2164 21 my ($pred, $l) = @_;
2165 21 my $v;
2166 LP: {
2167 21 ▶ if (is_pair $l) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_pair($l)) { } 27 14 41
elsif (is_null($l)) { } 14 0 14
2168 27 ▶ ($v = &$pred(car $l)) or do {
Branch: 1 branch
BranchTrueFalseTotal
$v = &$pred(car($l)) or do { $l = cdr($l); redo LP } 7 20 27
2169 20 $l = cdr $l;
2170 20 redo LP;
2171 }
2172 } elsif (is_null $l) {
2173 14 $v
2174 } else {
2175 0 die "improper list"
2176 }
2177 }
2178 }
2179  
2180 60 *FP::List::List::any = flip \&list_any;
2181  
2182 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2183 1 ▶ list_any sub { $_[0] % 2 }, array_to_list [2, 4, 8]
Subroutine __ANON__: called CC=1 SCAR=0.0
2184 }
2185 60 0;
2186 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=1.2
2187 0 ▶ list_any sub { $_[0] % 2 }, array_to_list []
Subroutine __ANON__: not called CC=1 SCAR=1.2
2188 }
2189 60 undef;
2190 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2191 1 ▶ list_any sub { $_[0] % 2 }, array_to_list [2, 5, 8]
Subroutine __ANON__: called CC=1 SCAR=0.0
2192 }
2193 60 1;
2194 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2195 1 ▶ list_any sub { $_[0] % 2 }, array_to_list [7]
Subroutine __ANON__: called CC=1 SCAR=0.0
2196 }
2197 60 1;
2198  
2199 # The following two functions differ from their paragons from SRFI-1
2200 # in that they do not return false or undef on failure, but (), and
2201 # carry `perhaps` in their name of this reason.
2202  
2203 17 ▶sub list_perhaps_find_tail {
Subroutine list_perhaps_find_tail: called CC=4 SCAR=13.9
Pod undocumented
2204 17 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 17 0 17
2205 17 my ($fn, $l) = @_;
2206 LP: {
2207 17 ▶ if (is_null $l) { () }
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($l)) { } 2 33 35
2208 else {
2209 33 my ($v, $l1) = $l->first_and_rest;
2210 33 ▶ if (&$fn($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$fn($v)) { } 15 18 33
2211 15 $l
2212 } else {
2213 18 $l = $l1;
2214 redo LP
2215 18 }
2216 }
2217 }
2218 }
2219  
2220 60 *FP::List::List::perhaps_find_tail = flip \&list_perhaps_find_tail;
2221  
2222 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2223 1 list(3, 1, 37, -8, -5, 0, 0)->perhaps_find_tail(\&is_even)->array
2224 }
2225 60 [-8, -5, 0, 0];
2226 1 ▶TEST { [list(3, 1, 37, -5)->perhaps_find_tail(\&is_even)] }
Subroutine __ANON__: called CC=1 SCAR=0.0
2227 60 [];
2228  
2229 13 ▶sub list_perhaps_find {
Subroutine list_perhaps_find: called CC=3 SCAR=12.0
Pod undocumented
2230 13 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 13 0 13
2231 13 my ($fn, $l) = @_;
2232 13 ▶ if (my ($l) = list_perhaps_find_tail($fn, $l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($l) = list_perhaps_find_tail($fn, $l)) { } 13 0 13
2233 13 $l->car
2234 } else {
2235 ()
2236 0 }
2237 }
2238  
2239 60 *FP::List::List::perhaps_find = flip \&list_perhaps_find;
2240  
2241 1 ▶TEST { list(3, 1, 4, 1, 5, 9)->perhaps_find(\&is_even) }
Subroutine __ANON__: called CC=1 SCAR=0.0
2242 60 4;
2243  
2244 # And then still also add the SRFI-1 counterparts, without `maybe` in
2245 # the names as they should have according to our guidelines, XX hmm.
2246  
2247 sub list_find_tail;
2248 60 *list_find_tail = perhaps_to_maybe(\&list_perhaps_find_tail);
2249 60 *FP::List::List::find_tail = flip \&list_find_tail;
2250  
2251 sub list_find;
2252 60 *list_find = perhaps_to_maybe(\&list_perhaps_find);
2253 60 *FP::List::List::find = flip \&list_find;
2254  
2255 1 ▶TEST { list(3, 1, 4, 1, 5, 9)->find(\&is_even) }
Subroutine __ANON__: called CC=1 SCAR=0.0
2256 60 4;
2257 1 ▶TEST { list(3, 1, 37, -8, -5, 0, 0)->find_tail(\&is_even)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
2258 60 [-8, -5, 0, 0];
2259 1 ▶TEST { [list(3, 1, 37, -5)->find_tail(\&is_even)] }
Subroutine __ANON__: called CC=1 SCAR=0.0
2260 60 [undef];
2261  
2262 # Grouping
2263  
2264 101 ▶sub make_group {
Subroutine make_group: called CC=1 SCAR=0.1
Pod undocumented
2265 101 my ($is_stream) = @_;
2266 3 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
2267 3 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 3 0 3
Condition33%1/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
003
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
2268 3 my ($equal, $s, $maybe_tail) = @_;
2269 3 ▶ weaken $_[1] if $is_stream;
Branch: 1 branch
BranchTrueFalseTotal
if $is_stream 1 2 3
2270 3 ▶ lazy_if {
Subroutine __ANON__: called CC=3 SCAR=11.3
2271 3 FORCE $s;
2272 3 ▶ if (is_null $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($s)) { } 0 3 3
2273 0 ▶ $maybe_tail // null
Condition0%0/3
$maybe_tail // &null()
l!l&&r!l&&!r
000
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table0%0/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
2274 } else {
2275 3 my ($a, $r) = $s->first_and_rest;
2276 3 my $rec;
2277 14 ▶ $rec = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2278 14 my ($prev, $s) = @_;
2279 14 ▶ lazy_if {
Subroutine __ANON__: called CC=4 SCAR=13.9
2280 14 my $s = $s;
2281 14 my $group = cons $prev, null;
2282 LP: {
2283 14 FORCE $s;
2284 19 ▶ if (is_null $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($s)) { } 3 16 19
2285 3 ▶ cons $group, ($maybe_tail // null)
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
030
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
2286 } else {
2287 16 my ($a, $r) = $s->first_and_rest;
2288 16 ▶ if (&$equal($prev, $a)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$equal($prev, $a)) { } 5 11 16
2289 5 $s = $r;
2290 5 $group = cons $a, $group;
2291 5 redo LP;
2292 } else {
2293 11 cons $group, &$rec($a, $r)
2294 }
2295 }
2296 }
2297 }
2298 14 $is_stream;
2299 3 };
2300  
2301 # TCO?
2302 ##XXX disable for v5.20.2 (Debian), wtf Weakened
2303 3 ($rec)->($a, $r)
2304 }
2305 }
2306 3 $is_stream
2307 }
2308 101 }
2309  
2310 sub list_group;
2311 60 *list_group = make_group(0);
2312  
2313 2 ▶sub FP::List::List::group {
Subroutine group: called CC=3 SCAR=12.0
2314 2 __
2315 'group($self, $equal, $maybe_tail): build groups of subsequent items that are $equal.';
2316 2 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 2 0 2
Condition33%1/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
002
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
2317 2 my ($self, $equal, $maybe_tail) = @_;
2318 2 list_group($equal, $self, $maybe_tail)
2319 }
2320  
2321 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2322 1 list(3, 4, 4, 5, 6, 8, 5, 5)->group(\&FP::Ops::number_eq)
2323 }
2324 60 list(list(3), list(4, 4), list(5), list(6), list(8), list(5, 5));
2325  
2326 # Split on items for which the predicate returns true:
2327  
2328 101 ▶sub make_split {
Subroutine make_split: called CC=1 SCAR=0.1
Pod undocumented
2329 101 my ($is_stream) = @_;
2330 4 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.4
2331 4 __
2332 'split($self, $pred, $retain_item, $maybe_tail): split on items for which $pred returns true. If $retain_item is true, the item that matched will be included in the previous group.';
2333 4 ▶ @_ >= 2 and @_ <= 4 or fp_croak_arity "2-4";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 4 or fp_croak_arity("2-4") 4 0 4
Condition33%1/3
@_ >= 2 and @_ <= 4
!ll&&!rl&&r
004
MC/DC0%0/2
@_ >= 2 and @_ <= 4
@_ >= 2 @_ <= 4
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 4
ABresult
0X0
100
111
2334  
2335 4 require FP::PureArray;
2336  
2337 4 my ($s, $pred, $retain_item, $maybe_tail) = @_;
2338 4 ▶ weaken $_[1] if $is_stream;
Branch: 1 branch
BranchTrueFalseTotal
if $is_stream 0 4 4
2339 4 ▶ lazy_if {
Subroutine __ANON__: called CC=3 SCAR=11.3
2340 4 FORCE $s;
2341 4 ▶ if (is_null $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($s)) { } 0 4 4
2342 0 ▶ $maybe_tail // null
Condition0%0/3
$maybe_tail // &null()
l!l&&r!l&&!r
000
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table0%0/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
2343 } else {
2344 4 my $rec;
2345 14 ▶ $rec = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2346 14 my ($s) = @_;
2347 14 ▶ lazy_if {
Subroutine __ANON__: called CC=7 SCAR=19.6
2348 14 my $s = $s;
2349 14 my @group;
2350 LP: {
2351 14 FORCE $s;
2352 25 ▶ if (is_null $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($s)) { } 4 21 25
2353 4 ▶ if (@group) {
Branch: 1 branch
BranchTrueFalseTotal
if (@group) { } 1 3 4
2354 1 ▶ cons(
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
010
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
2355 FP::PureArray::array_to_purearray(
2356 \@group),
2357 ($maybe_tail // null)
2358 )
2359 } else {
2360 3 ▶ ($maybe_tail // null)
Condition33%1/3
$maybe_tail // &null()
l!l&&r!l&&!r
030
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table33%1/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
2361 }
2362 } else {
2363 21 my ($a, $r) = $s->first_and_rest;
2364 21 ▶ if ($pred->($a)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$pred($a)) { } 10 11 21
2365 10 ▶ if ($retain_item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retain_item) 3 7 10
2366 3 push @group, $a;
2367 }
2368 cons(
2369 10 FP::PureArray::array_to_purearray(
2370 \@group),
2371 $rec->($r)
2372 )
2373 } else {
2374 11 $s = $r;
2375 11 push @group, $a;
2376 11 redo LP;
2377 }
2378 }
2379 }
2380 }
2381 14 $is_stream;
2382 4 };
2383 4 $rec->($s)
2384 }
2385 }
2386 4 $is_stream
2387 }
2388 101 }
2389  
2390 sub list_split;
2391 60 *list_split = make_split(0);
2392  
2393 60 *FP::List::List::split = \&list_split;
2394  
2395 # For split tests see FP::List::t.
2396  
2397 # Turn a mix of (nested) arrays and lists into a flat list.
2398  
2399 # If the third argument is given, it needs to be a reference to either
2400 # lazy or lazyLight. In that case it will force promises, but only
2401 # lazily (i.e. provide a promise that will do the forcing and consing).
2402  
2403 438 ▶sub mixed_flatten {
Subroutine mixed_flatten: called CC=10 SCAR=23.1
Pod undocumented
2404 438 ▶ @_ >= 1 and @_ <= 3 or fp_croak_arity "1-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 3 or fp_croak_arity("1-3") 438 0 438
Condition33%1/3
@_ >= 1 and @_ <= 3
!ll&&!rl&&r
00438
MC/DC0%0/2
@_ >= 1 and @_ <= 3
@_ >= 1 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 3
ABresult
0X0
100
111
2405 438 my ($v, $maybe_tail, $maybe_delay) = @_;
2406 438 ▶ my $tail = $maybe_tail // null;
Condition66%2/3
$maybe_tail // &null()
l!l&&r!l&&!r
428100
MC/DC0%0/2
$maybe_tail // &null()
$maybe_tail &null()
Truth table66%2/3
A // B
A= $maybe_tailB= &null()
ABresult
1X1
011
000
2407 LP: {
2408 438 ▶ if ($maybe_delay and is_promise $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_delay and is_promise($v)) { } 78 427 505
Condition100%3/3
$maybe_delay and is_promise($v)
!ll&&!rl&&r
4638178
MC/DC100%2/2
$maybe_delay and is_promise($v)
$maybe_delay is_promise($v)
Truth table100%3/3
A and B
A= $maybe_delayB= is_promise($v)
ABresult
0X0
100
111
2409 78 my $delay = $maybe_delay;
2410 &$delay(
2411 74 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2412 74 @_ = (force($v), $tail, $delay);
2413 74 goto \&mixed_flatten;
2414 }
2415 78 );
2416 } else {
2417 427 ▶ if (is_null $v) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_null($v)) { } 21 406 427
elsif (is_pair($v)) { } 67 339 406
elsif (ref $v eq 'ARRAY') { } 94 245 339
2418 21 $tail
2419 } elsif (is_pair $v) {
2420 60 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
2421 67 $tail = mixed_flatten(cdr($v), $tail, $maybe_delay);
2422 67 $v = car $v;
2423 67 redo LP;
2424 } elsif (ref $v eq "ARRAY") {
2425 @_ = (
2426 148 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
2427 148 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 148 0 148
2428 148 my ($v, $tail) = @_;
2429 60 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
2430  
2431 # ^XX don't understand why it warns here
2432 148 @_ = ($v, $tail, $maybe_delay);
2433 148 goto \&mixed_flatten;
2434 },
2435 94 $tail,
2436 $v
2437 );
2438 94 require FP::Stream; # XX ugly? de-circularize?
2439 goto(
2440 94 ▶ $maybe_delay
Branch: 1 branch
BranchTrueFalseTotal
$maybe_delay ? : 87 7 94
2441 ? \&FP::Stream::stream__array_fold_right
2442  
2443 #^ XX just expecting it to be loaded
2444 : \&array_fold_right
2445 );
2446 } else {
2447  
2448 #warn "improper list: $v"; well that's part of the spec, man
2449 245 cons($v, $tail)
2450 }
2451 }
2452 }
2453 }
2454  
2455 60 *FP::List::List::mixed_flatten = \&mixed_flatten;
2456  
2457 1 ▶TEST { list_to_array mixed_flatten [1, 2, 3] }
Subroutine __ANON__: called CC=1 SCAR=0.0
2458 60 [1, 2, 3];
2459 1 ▶TEST { list_to_array mixed_flatten [1, 2, [3, 4]] }
Subroutine __ANON__: called CC=1 SCAR=0.0
2460 60 [1, 2, 3, 4];
2461 1 ▶TEST { list_to_array mixed_flatten [1, cons(2, [string_to_list "ab", 4])] }
Subroutine __ANON__: called CC=1 SCAR=0.0
2462 60 [1, 2, 'a', 'b', 4];
2463 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2464 1 list(1, cons(2, [string_to_list "ab", 4]))->mixed_flatten->array
2465 }
2466 60 [1, 2, 'a', 'b', 4];
2467 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2468 1 list_to_string mixed_flatten [string_to_list "abc", string_to_list "def",
2469 "ghi"]
2470 }
2471 60 'abcdefghi'; # only works thanks to perl chars and strings being
2472 # the same datatype
2473  
2474 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
2475 write_sexpr(
2476 1 ▶ mixed_flatten lazyLight {
Subroutine __ANON__: called CC=1 SCAR=0.0
2477 1 ▶ cons(lazy { 1 + 1 }, null)
Subroutine __ANON__: called CC=1 SCAR=0.0
2478 },
2479 undef,
2480 1 \&lazyLight
2481 )
2482 }
2483 60 '("2")';
2484 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
2485 write_sexpr(
2486 1 ▶ mixed_flatten lazyLight {
Subroutine __ANON__: called CC=1 SCAR=0.0
2487 cons(
2488 1 ▶ lazy {
Subroutine __ANON__: called CC=1 SCAR=0.0
2489 1 ▶ [1 + 1, lazy { 2 + 1 }]
Subroutine __ANON__: called CC=1 SCAR=0.0
2490 },
2491 1 null
2492 )
2493 },
2494 undef,
2495 1 \&lazyLight
2496 )
2497 }
2498 60 '("2" "3")';
2499  
2500 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
2501  
2502 11 ▶ sub countdown {
Subroutine countdown: called CC=2 SCAR=6.9
Pod undocumented
2503 11 my ($i) = @_;
2504 11 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) { } 10 1 11
2505 10 ▶ lazyLight { cons($i, countdown($i - 1)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
2506 10 } else {
2507 1 null
2508 }
2509 }
2510 write_sexpr(
2511 1 ▶ mixed_flatten lazyLight {
Subroutine __ANON__: called CC=1 SCAR=0.0
2512 1 ▶ cons(lazy { [1 + 1, countdown 10] }, null)
Subroutine __ANON__: called CC=1 SCAR=0.0
2513 },
2514 undef,
2515 1 \&lazyLight
2516 )
2517 }
2518 60 '("2" "10" "9" "8" "7" "6" "5" "4" "3" "2" "1")';
2519  
2520 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
2521 1 ▶ write_sexpr(mixed_flatten [lazyLight { [3, [9, 10]] }], undef, \&lazyLight)
Subroutine __ANON__: called CC=1 SCAR=0.0
2522 }
2523 60 '("3" "9" "10")';
2524 1 ▶TEST_STDOUT {
Subroutine __ANON__: called CC=1 SCAR=0.0
2525 1 ▶ write_sexpr(mixed_flatten [1, 2, lazyLight { [3, 9] }], undef, \&lazyLight)
Subroutine __ANON__: called CC=1 SCAR=0.0
2526 }
2527 60 '("1" "2" "3" "9")';
2528  
2529 7 ▶sub list_insertion_variants {
Subroutine list_insertion_variants: called CC=5 SCAR=16.2
Pod undocumented
2530 7 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2 or 3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2 or 3") 7 0 7
Condition66%2/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
520
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table66%2/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
2531 7 my ($l, $v, $variants_tail) = @_;
2532 7 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 5 2 7
2533 5 $variants_tail = null
2534 }
2535 7 ▶ if ($l->is_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_null) { } 3 4 7
2536 3 cons(list($v), $variants_tail)
2537 } else {
2538 4 my ($a, $r) = $l->first_and_rest;
2539 $r->insertion_variants($v)->map_with_tail(
2540 7 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2541 7 my ($r2) = @_;
2542 7 cons $a, $r2
2543 },
2544 4 $variants_tail
2545 )->cons(cons($v, $l))
2546 }
2547 }
2548  
2549 60 *FP::List::List::insertion_variants = \&list_insertion_variants;
2550  
2551 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2552 1 list_insertion_variants list(qw(a b c)), 0
2553 }
2554 60 list(
2555 list(0, 'a', 'b', 'c'),
2556 list('a', 0, 'b', 'c'),
2557 list('a', 'b', 0, 'c'),
2558 list('a', 'b', 'c', 0)
2559 );
2560 1 ▶TEST { list_insertion_variants list(qw(a)), 0, list "END" }
Subroutine __ANON__: called CC=1 SCAR=0.0
2561 60 list(list(0, 'a'), list('a', 0), 'END');
2562 1 ▶TEST { list_insertion_variants list(), 0, list "END" } list(list(0), 'END');
Subroutine __ANON__: called CC=1 SCAR=0.0
2563  
2564 10 ▶sub list_merge {
Subroutine list_merge: called CC=6 SCAR=18.1
Pod undocumented
2565 10 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 10 0 10
2566 10 my ($A, $B, $cmp) = @_;
2567 10 ▶ if (is_null $A) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_null($A)) { } 1 9 10
elsif (is_null($B)) { } 0 9 9
2568 1 $B
2569 } elsif (is_null $B) {
2570 0 $A
2571 } else {
2572 9 my ($a, $ar) = $A->first_and_rest;
2573 9 my ($b, $br) = $B->first_and_rest;
2574 9 my $dir = $cmp->($a, $b);
2575 9 ▶ if ($dir < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dir < 0) { } 3 6 9
elsif ($dir == 0) { } 2 4 6
2576 3 cons($a, list_merge($ar, $B, $cmp))
2577 } elsif ($dir == 0) {
2578 2 cons($a, cons($b, list_merge($ar, $br, $cmp)))
2579 } else {
2580 4 cons($b, list_merge($A, $br, $cmp))
2581 }
2582 }
2583 }
2584 60 *FP::List::List::merge = \&list_merge;
2585  
2586 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2587 1 require FP::Ops;
2588 1 list_merge list(-3, 1, 1, 3, 4), list(-6, -4, -3, 0, 1, 2, 5, 6, 7),
2589 \&FP::Ops::real_cmp
2590 }
2591 60 list(-6, -4, -3, -3, 0, 1, 1, 1, 2, 3, 4, 5, 6, 7);
2592  
2593 # Adapted copy-paste from FP/Stream.pm
2594  
2595 4 ▶sub cartesian_product_2 {
Subroutine cartesian_product_2: called CC=2 SCAR=6.9
Pod undocumented
2596 4 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 4 0 4
2597 4 my ($a, $orig_b) = @_;
2598 4 my $rec;
2599 32 ▶ $rec = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
2600 32 my ($a, $b) = @_;
2601 32 my $rec = $rec;
2602 {
2603 32 ▶ if (is_null $a) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_null($a)) { } 4 28 32
elsif (is_null($b)) { } 8 20 28
2604 4 null
2605 } elsif (is_null $b) {
2606 8 &$rec(cdr($a), $orig_b);
2607 } else {
2608 20 cons(cons(car($a), car($b)), &$rec($a, cdr $b))
2609 }
2610 }
2611 4 };
2612 4 Weakened($rec)->($a, $orig_b)
2613 }
2614  
2615 60 *FP::List::List::cartesian_product_2 = \&cartesian_product_2;
2616  
2617 1 ▶TEST { cartesian_product_2 list("A", "B"), list(list(1), list(2)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
2618 60 list(list('A', 1), list('A', 2), list('B', 1), list('B', 2));
2619  
2620 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
2621 1 cartesian_product_2 list("E", "F"), cartesian_product_2 list("C", "D"),
2622 list(list("A"), list("B"))
2623 }
2624 60 list(
2625 list("E", "C", "A"),
2626 list("E", "C", "B"),
2627 list("E", "D", "A"),
2628 list("E", "D", "B"),
2629 list("F", "C", "A"),
2630 list("F", "C", "B"),
2631 list("F", "D", "A"),
2632 list("F", "D", "B")
2633 );
2634  
2635 2 ▶sub cartesian_product {
Subroutine cartesian_product: called CC=3 SCAR=11.2
Pod undocumented
2636 2 my @v = @_;
2637 2 ▶ if (!@v) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @v) { } 0 2 2
elsif (@v == 1) { } 1 1 2
2638 0 die "cartesian_product: need at least 1 argument"
2639 } elsif (@v == 1) {
2640 1 list_map \&list, $v[0]
2641 } else {
2642 1 my $first = shift @v;
2643 1 cartesian_product_2($first, cartesian_product(@v))
2644 }
2645 }
2646  
2647 60 *FP::List::List::cartesian_product = \&cartesian_product;
2648  
2649 1 ▶TEST { cartesian_product list("A", "B"), list(1, 2) }
Subroutine __ANON__: called CC=1 SCAR=0.0
2650 60 list(list('A', 1), list('A', 2), list('B', 1), list('B', 2));
2651  
2652 60 ▶use FP::Char 'is_char';
Subroutine BEGIN: called CC=1 SCAR=0.0
2653  
2654 0 ▶sub is_charlist {
Subroutine is_charlist: not called CC=2 SCAR=17.9
Pod undocumented
2655 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
2656 0 my ($l) = @_;
2657 0 list_every \&is_char, $l
2658 }
2659  
2660 60 *FP::List::List::is_charlist = \&is_charlist;
2661  
2662 # XX update to use fp_croak ?
2663 0 ▶sub ldie {
Subroutine ldie: not called CC=3 SCAR=24.8
Pod undocumented
2664  
2665 # perl string arguments are messages, char lists are turned to
2666 # perl-quoted strings, then everyting is appended
2667 my @strs = map {
2668 0 ▶ if (is_charlist $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_charlist($_)) { } 0 0 0
elsif (is_null($_)) { } 0 0 0
2669 0 list_to_perlstring $_
2670 } elsif (is_null $_) {
2671 0 "()"
2672 } else {
2673  
2674 # XX have a better write_sexpr that can fall back to something
2675 # better?, and anyway, need string
2676 0 $_
2677 }
2678 } @_;
2679 0 croak join("", @strs)
2680 }
2681  
2682 60 package FP::List::Null {
2683 60 FP::Interfaces::implemented qw(FP::Abstract::Pure
2684 FP::Abstract::Sequence
2685 FP::Abstract::Equal
2686 FP::Abstract::Show);
2687 }
2688  
2689 60 package FP::List::Pair {
2690 60 FP::Interfaces::implemented qw(FP::Abstract::Pure
2691 FP::Abstract::Sequence
2692 FP::Abstract::Equal
2693 FP::Abstract::Show);
2694 }
2695  
2696 60 1
« lib/FP/Weak.pm ↑ Summary blib/lib/FP/Trie/t.pm »