lib/FP/Abstract/Sequence.pm

filter: Statementstmt 84.9% 124 / 146 Branchbran 58.0% 36 / 62 Conditioncond 66.6% 10 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 89.1% 41 / 46 Podpod 0.0% 0 / 20 totaltotal 70.5% 211 / 299 CC 37 SCAR 11.1
CC
37
Cov
76%
median
12.0
subsection
6.0
mean
6.0
CRAP 3.0
« lib/FP/Path.pm ↑ Summary lib/PXML/SVG.pm »
1 #
2 # Copyright (c) 2015-2022 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::Abstract::Sequence - functional sequence protocol
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Predicates qw(is_sequence is_proper_sequence);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 # ^ placed here because we can't export functions from
18 # FP::Abstract::Sequence
19 1 ▶ use FP::PureArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use FP::StrictList;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use FP::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶ use FP::Array 'array';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶ use FP::Equal 'is_equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 is_equal list(purearray(3, 4),
28 strictlist(3, 4),
29 list(3, 4),
30 stream(3, 4),
31 cons(3, 4),
32 array(3, 4), # Should we change this given `FP::autobox`?
33 3,
34 {3 => 4})->map(\&is_sequence),
35 list(1, 1, 1, 1, 1, undef, undef, undef);
36  
37 is is_sequence(cons 3, 4), 1;
38 is is_proper_sequence(cons 3, 4), 0; # improper list
39  
40 my $ns = purearray(FP::Abstract::Sequence->FP_Interface__method_names);
41 # The methods you can count on being supported by sequences.
42  
43 is_equal $ns->sort->take(5),
44 purearray('any', 'append', 'array', 'cons', 'drop');
45  
46 =head1 DESCRIPTION
47  
48 This is a functional protocol, i.e. its use does not exert any side
49 effects (that are visible to the user). It does *not* imply
50 `FP::Abstract::Pure`, though; impure data structures could implement
51 it all the same (in addition to a mutation protocol).
52  
53 =head1 SEE ALSO
54  
55 L<FP::Abstract::Pure>
56  
57 =head1 NOTE
58  
59 This is alpha software! Read the status section in the package README
60 or on the L<website|http://functional-perl.org/>.
61  
62 =cut
63  
64 package FP::Abstract::Sequence;
65  
66 60 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 60 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 60 require FP::List; # "use"ing it would create a circular dependency
71 60 ▶use FP::Array_sort qw(on_maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 60 ▶use FP::Cmp qw(cmp_complement);
Subroutine BEGIN: called CC=1 SCAR=0.0
73 60 ▶use FP::Lazy;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 60 ▶use FP::Ops qw(add mult real_cmp);
Subroutine BEGIN: called CC=1 SCAR=0.0
75 60 ▶use FP::Predicates qw(complement is_even);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 60 ▶use FP::Div qw(average);
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 60 ▶use Chj::NamespaceCleanAbove;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 60 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 157 ▶sub FP_Interface__method_names {
Subroutine FP_Interface__method_names: called CC=1 SCAR=0.0
Pod undocumented
82 157 my $class = shift;
83 (
84  
85 # base definition in this file hence not necessary to specify
86 # here except for the sake of safety in case the base implementation is
87 # removed:
88 qw(
89 is_proper_sequence
90 flatten
91 intersperse
92 extreme
93 min
94 max
95 minmax
96 subsection
97 make_reduce
98 reduce
99 reduce_right
100 sum
101 mean
102 median
103 product
104 none
105 join
106 ),
107  
108 # virtual methods:
109 157 grep { not /^TODO/ } qw(
110 is_null
111 first rest
112 first_and_rest
113 maybe_first maybe_rest
114 perhaps_first perhaps_rest
115 perhaps_first_and_rest
116 map
117 map_with_index
118 map_with_islast
119 filter
120 TODO_filter_with_tail
121 drop_while
122 TODO_rtake_while_and_rest
123 TODO_rtake_while
124 take_while_and_rest
125 take_while
126 every
127 any
128 find
129 fold
130 fold_right
131 preferred_fold
132 append
133 reverse
134 take
135 drop
136 xone
137 perhaps_one
138 zip
139 for_each
140 for_each_with_islast
141 strings_join
142 length
143 second
144 cons
145 array
146 list
147 stream
148 sort
149 sortCompare
150 group
151 string
152 values
153 ),
154  
155 # $class->SUPER::FP_Interface__method_names
156 )
157 }
158  
159 # XX why sortCompare and not an optional argument to sort ?
160  
161 #XXX different protocol for random access ones:
162 #hmm add ref here too? vs. efficient_ref etc. *?* or slow_ref or somthing?
163 # InefficientSequence
164 # ref
165 # last
166 # butlast
167 # also sub subsection slice ?
168 # *set = blessing \&array_set;
169 # *push = blessing \&array_push;
170 # *pop = blessing_snd \&array_pop;
171 # *shift = blessing_snd \&array_shift;
172 # *unshift = blessing \&array_unshift;
173  
174 #XXX other
175 # group group_by
176 # zip2
177  
178 # XXX these don't weaken the caller arguments, thus will leak for
179 # streams. How to solve this (and not copy-paste-adapt the methods
180 # manually) without fixing perl?
181  
182 3 ▶sub flatten {
Subroutine flatten: called CC=4 SCAR=14.0
Pod undocumented
183 3 ▶ @_ == 1 or @_ == 2 or fp_croak_arity "1 or 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or fp_croak_arity("1 or 2") 3 0 3
Condition66%2/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
120
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
184 3 my ($self, $perhaps_tail) = @_;
185 $self->fold_right(
186 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
187 5 my ($v, $rest) = @_;
188 5 $v->append($rest)
189 },
190 3 ▶ @_ == 2 ? $perhaps_tail : FP::List::null()
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 2 1 3
191 );
192 }
193  
194 # XXX and on top of that, these return a lazy result even if the input
195 # isn't; related to the above issue. Find solution for both.
196  
197 # (XX only works computationally efficient for *some* sequences;
198 # introduce an FP::Abstract::IterativeSequence or so and move it
199 # there?)
200 7 ▶sub intersperse {
Subroutine intersperse: called CC=2 SCAR=6.9
Pod undocumented
201 7 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 7 0 7
202 7 my ($self, $value) = @_;
203  
204 # (should we recurse locally like most sequence functions? Or is
205 # it actually a good idea to call the method on the rest (for
206 # improper_listS, but once we introduce a `cons` method on
207 # PureArray etc. that won't happen anymore?)?)
208 7 ▶ lazy {
Subroutine __ANON__: called CC=3 SCAR=11.0
209 7 ▶ $self->is_null ? $self : do {
Branch: 1 branch
BranchTrueFalseTotal
$self->is_null ? : 1 6 7
210 6 my ($v, $rest) = $self->first_and_rest;
211 6 ▶ $rest->is_null
Branch: 1 branch
BranchTrueFalseTotal
$rest->is_null ? : 3 3 6
212 ? $self
213 : FP::List::cons($v,
214 FP::List::cons($value, $rest->intersperse($value)))
215 }
216 }
217 7 }
218  
219 # XX better name?
220 5 ▶sub extreme {
Subroutine extreme: called CC=2 SCAR=7.0
Pod undocumented
221 5 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 5 0 5
222 5 my ($self, $cmp) = @_;
223  
224 # XXX: fold_right is good for FP::Stream streaming. left fold will
225 # be better for FP::List. How? Add fold_left for explicit left
226 # folding and make fold chose the preferred solution for
227 # order-irrelevant folding?
228 $self->rest->fold(
229 13 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
230 13 my ($v, $res) = @_;
231 13 my $c = &$cmp($v, $res);
232 13 ▶ $c < 0 ? $v : $res
Branch: 1 branch
BranchTrueFalseTotal
$c < 0 ? : 6 7 13
233 },
234 5 $self->first
235 );
236 }
237  
238 3 ▶sub min {
Subroutine min: called CC=4 SCAR=14.5
Pod undocumented
239 3 ▶ @_ == 1 or @_ == 2 or fp_croak_arity "1 or 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or fp_croak_arity("1 or 2") 3 0 3
Condition66%2/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
210
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
240 3 my ($self, $maybe_cmp) = @_;
241 5 ▶ $self->extreme($maybe_cmp // sub { $_[0] <=> $_[1] })
Subroutine __ANON__: called CC=1 SCAR=0.0
242 3 ▶}
Condition66%2/3
$maybe_cmp // sub { $_[0] <=> $_[1]; }
l!l&&r!l&&!r
120
MC/DC0%0/2
$maybe_cmp // sub { $_[0] <=> $_[1]; }
$maybe_cmp sub { $_[0] <=> $_[1]; }
Truth table66%2/3
A // B
A= $maybe_cmpB= sub { $_[0] <=> $_[1]; }
ABresult
1X1
011
000
243  
244 2 ▶sub max {
Subroutine max: called CC=4 SCAR=14.1
Pod undocumented
245 2 ▶ @_ == 1 or @_ == 2 or fp_croak_arity "1 or 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or fp_croak_arity("1 or 2") 2 0 2
Condition66%2/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
110
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
246 2 my ($self, $maybe_cmp) = @_;
247 $self->extreme(
248 defined($maybe_cmp)
249 ? cmp_complement($maybe_cmp)
250 2 ▶ : sub { -($_[0] <=> $_[1]) })
Subroutine __ANON__: called CC=1 SCAR=0.0
251 2 ▶}
Branch: 1 branch
BranchTrueFalseTotal
defined $maybe_cmp ? : 1 1 2
252  
253 2 ▶sub minmax {
Subroutine minmax: called CC=4 SCAR=13.9
Pod undocumented
254 2 ▶ @_ == 1 or @_ == 2 or fp_croak_arity "1 or 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or fp_croak_arity("1 or 2") 2 0 2
Condition66%2/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
110
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
255 2 my ($self, $maybe_extract) = @_;
256  
257 # XXX same comment as in `extreme`
258 @{
259 2 $self->rest->fold(
260 defined $maybe_extract
261 3 ▶ ? sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
262 3 my ($v, $res) = @_;
263 3 my ($min, $max) = @$res;
264 3 my $v_ = &$maybe_extract($v);
265 [
266 3 ▶ $v_ < &$maybe_extract($min) ? $v : $min,
Branch: 2 branches
BranchTrueFalseTotal
$v_ < &$maybe_extract($min) ? : 1 2 3
$v_ > &$maybe_extract($max) ? : 2 1 3
267 $v_ > &$maybe_extract($max) ? $v : $max
268 ]
269 }
270 3 ▶ : sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
271 3 my ($v, $res) = @_;
272 3 my ($min, $max) = @$res;
273 3 ▶ [$v < $min ? $v : $min, $v > $max ? $v : $max]
Branch: 2 branches
BranchTrueFalseTotal
$v < $min ? : 1 2 3
$v > $max ? : 2 1 3
274 },
275 2 ▶ [$self->first, $self->first]
Branch: 1 branch
BranchTrueFalseTotal
defined $maybe_extract ? : 1 1 2
276 )
277 }
278 }
279  
280 0 ▶sub subsection {
Subroutine subsection: not called CC=2 SCAR=17.9
Pod undocumented
281 0 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 0 0 0
282 0 my ($self, $i0, $i1) = @_;
283  
284 # XXX same comment as in `extreme`
285 0 $self->drop($i0)->take($i1 - $i0)
286 }
287  
288 # Reduce is a variant of fold that doesn't require an initial
289 # reduction value but instead takes $seq.first() as the initial
290 # reduction value (it hence only works in general if the types of the
291 # two arguments of $fn are the same). If $seq is empty, produces the
292 # result by calling $fn with no arguments.
293  
294 # Note: this base implementation relies on an efficient ->rest
295 # operation; for types where ->rest can't be made efficient (via
296 # e.g. slices), override this implementation!
297  
298 120 ▶sub make_reduce {
Subroutine make_reduce: called CC=1 SCAR=0.0
Pod undocumented
299 120 my ($_class, $fold) = @_;
300 362 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
301 362 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 362 0 362
302 362 my ($seq, $fn) = @_;
303 362 ▶ if ($seq->is_null()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->is_null) { } 2 360 362
304 2 &$fn()
305 } else {
306 360 my ($first, $rest) = $seq->first_and_rest;
307 360 $rest->$fold($fn, $first)
308 }
309 }
310 120 }
311  
312 # This variant folds from whichever side makes more sense for the
313 # type:
314 # *reduce = __PACKAGE__->make_reduce("preferred_fold");
315  
316 # But, the question is really eager vs. lazy evaluation, i.e. foldl vs
317 # foldl'. Should we do that? For now just:
318  
319 sub reduce;
320 60 *reduce = __PACKAGE__->make_reduce("fold");
321  
322 # These variants are explicit in which side they are folding from:
323  
324 sub reduce_right;
325 60 *reduce_right = __PACKAGE__->make_reduce("fold_right");
326  
327 354 ▶sub sum {
Subroutine sum: called CC=2 SCAR=7.2
Pod undocumented
328 354 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 354 0 354
329 354 $_[0]->reduce(\&add)
330 }
331  
332 0 ▶sub mean {
Subroutine mean: not called CC=2 SCAR=17.9
Pod undocumented
333 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
334 0 my ($s) = @_;
335  
336 # XX imprecise for large numbers of items
337 0 $s->sum / $s->length
338 }
339  
340 0 ▶sub median {
Subroutine median: not called CC=3 SCAR=24.8
Pod undocumented
341 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
342 0 my ($s) = @_;
343 0 my $sorted = $s->sort(\&real_cmp);
344 0 my $len = $s->length;
345 0 my $mid = int($len / 2);
346 0 ▶ if (is_even $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_even($len)) { } 0 0 0
347 0 average($sorted->ref($mid - 1), $sorted->ref($mid));
348 } else {
349 0 $sorted->ref($mid)
350 }
351 }
352  
353 5 ▶sub product {
Subroutine product: called CC=2 SCAR=7.2
Pod undocumented
354 5 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 5 0 5
355 5 $_[0]->reduce(\&mult)
356 }
357  
358 4 ▶sub none {
Subroutine none: called CC=2 SCAR=7.1
Pod undocumented
359 4 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 4 0 4
360 4 my ($s, $pred) = @_;
361 4 $s->every(complement $pred)
362 }
363  
364 1 ▶sub split_at {
Subroutine split_at: called CC=2 SCAR=7.1
Pod undocumented
365 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
366 1 my ($s, $pos) = @_;
367  
368 # XXX weaken as all of them.
369 1 ($s->take($pos), $s->drop($pos))
370 }
371  
372 1 ▶sub chunks_of {
Subroutine chunks_of: called CC=2 SCAR=7.1
Pod undocumented
373 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
374 1 my ($s, $chunklen) = @_;
375  
376 # XXX weaken as all of them.
377 1 $s->stream->chunks_of($chunklen)
378 }
379  
380 1 ▶sub strictly_chunks_of {
Subroutine strictly_chunks_of: called CC=2 SCAR=7.1
Pod undocumented
381 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
382 1 my ($s, $chunklen) = @_;
383  
384 # XXX weaken as all of them.
385 1 $s->stream->strictly_chunks_of($chunklen)
386 }
387  
388 # join in Haskell is doing "++" on the items, should probably choose a
389 # protocol for this as well; for now, hard-code to strings_join:
390 1 ▶sub join {
Subroutine join: called CC=2 SCAR=7.1
Pod undocumented
391 1 my ($s) = @_;
392  
393 # Tail-call, please, for 'weakening maintenance'.
394  
395 # XX only AUTOLOAD is defined, not `can`! But $s was already
396 # forced by the AUTOLOAD thus nothing more is needed here. But
397 # this might change!
398 1 ▶ my $m = $s->can("strings_join")
Branch: 1 branch
BranchTrueFalseTotal
my $m = $s->can("strings_join") or die "bug: missing strings_join method on: $s" 1 0 1
399  
400 # bug since it's requested by the interface
401 or die "bug: missing strings_join method on: $s";
402  
403 1 goto $m
404 }
405  
406 # corresponding to is_hashset in FP::HashSet :
407  
408 0 ▶sub hashset {
Subroutine hashset: not called CC=2 SCAR=17.9
Pod undocumented
409 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
410 0 my ($s) = @_;
411 0 +{ map { $_ => $_ } $s->values }
412 }
413  
414 # corresponding to is_uhashset in FP::HashSet :
415  
416 0 ▶sub uhashset {
Subroutine uhashset: not called CC=2 SCAR=17.9
Pod undocumented
417 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
418 0 my ($s) = @_;
419 0 +{ map { $_ => undef } $s->values }
420 }
421  
422 60 _END_
« lib/FP/Path.pm ↑ Summary lib/PXML/SVG.pm »