lib/FP/Array/Mixin.pm

filter: Statementstmt 91.5% 151 / 165 Branchbran 50.0% 23 / 46 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.1% 27 / 31 Podpod 0.0% 0 / 13 totaltotal 77.6% 202 / 260 CC 25 SCAR 13.7
CC
25
Cov
82%
maybe_first_and_rest
12.0
perhaps_first_and_rest
12.0
group
8.1
CRAP 3.9
« lib/FP/Optional.pm ↑ Summary lib/Chj/CPAN/ModulePODUrl.pm »
1 #
2 # Copyright (c) 2014-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::Array::Mixin
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 Used to implement array based sequences.
19  
20 =head1 SEE ALSO
21  
22 L<FP::PureArray>, L<FP::MutableArray>
23  
24 L<FP::Array> -- definition of most functions used here
25  
26 =head1 NOTE
27  
28 This is alpha software! Read the status section in the package README
29 or on the L<website|http://functional-perl.org/>.
30  
31 =cut
32  
33 package FP::Array::Mixin;
34  
35 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 35 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 35 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 35 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 35 ▶use FP::Optional qw(perhaps_to_maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 35 ▶use FP::Combinators qw (flip flip2of3 rot3right rot3left);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 35 ▶use FP::Array ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
44 35 ▶use FP::Array_sort qw(array_sort array_sortCompare);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 945 ▶sub blessing {
Subroutine blessing: called CC=2 SCAR=7.3
47 945 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 945 0 945
48 945 my ($m) = @_;
49 677 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
50 677 my $class = ref $_[0];
51 677 ▶ if (my ($v) = &$m(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($v) = &$m(@_)) { } 675 0 675
52 675 $class->new_from_array($v)
53 } else {
54 ()
55 0 }
56 }
57 945 }
58  
59 35 ▶sub blessing2 {
Subroutine blessing2: called CC=2 SCAR=11.1
60 35 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 35 0 35
61 35 my ($m) = @_;
62 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
63 0 my $class = ref $_[0];
64 0 ▶ if (my ($v1, $v2) = &$m(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$m(@_)) { } 0 0 0
65 0 ($class->new_from_array($v1), $class->new_from_array($v2))
66 } else {
67 ()
68 0 }
69 }
70 35 }
71  
72 70 ▶sub blessing_snd {
Subroutine blessing_snd: called CC=2 SCAR=6.9
73 70 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 70 0 70
74 70 my ($m) = @_;
75 3 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
76 3 my $class = ref $_[0];
77 wantarray ## no critic
78 3 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
79 2 my ($v, $a) = &$m(@_);
80 2 ($v, $class->new_from_array($a))
81 }
82 : $class->new_from_array((&$m(@_))[-1]);
83 }
84 70 }
85  
86 35 ▶use Chj::NamespaceCleanAbove;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 0 ▶sub FP_Show_show {
Subroutine FP_Show_show: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my ($s, $show) = @_;
90 0 $s->constructor_name . "(" . join(", ", @{ array_map($show, $s) }) . ")"
91 }
92  
93 35 *FP_Equal_equal = \&array_equal;
94  
95 4 ▶sub is_proper_sequence {1}
Subroutine is_proper_sequence: called CC=1 SCAR=0.0
Pod undocumented
96  
97 # de-import array from FP::Array to avoid redefinition warning
98 35 ▶BEGIN { undef *array }
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 12 ▶sub array {
Subroutine array: called CC=2 SCAR=7.1
101 12 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 12 0 12
102 12 my $s = shift;
103  
104 # 'debless', and copy necessary as the user is entitled to mod it
105 # now. (XX: might optimize if only reference left by checking the
106 # refcount)
107 12 [@$s]
108 }
109  
110 6 ▶sub list {
Subroutine list: called CC=2 SCAR=7.0
Pod undocumented
111 6 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 6 0 6
112 6 my $s = shift;
113 6 require FP::List; # (overhead of repeated require?)
114 6 FP::List::array_to_list($s)
115 }
116  
117 21 ▶sub stream {
Subroutine stream: called CC=2 SCAR=7.0
Pod undocumented
118 21 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 21 0 21
119 21 my $s = shift;
120 21 require FP::Stream; # (ditto)
121 21 FP::Stream::array_to_stream($s)
122 }
123  
124 4 ▶sub strictlist {
Subroutine strictlist: called CC=2 SCAR=7.1
Pod undocumented
125 4 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 4 0 4
126 4 my $s = shift;
127  
128 # XX could be more efficient via an
129 # FP::StrictList::array_to_strictlist if it existed.
130 4 FP::StrictList::strictlist(@$s)
131 }
132  
133 1 ▶sub string {
Subroutine string: called CC=2 SCAR=7.2
Pod undocumented
134 1 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1 0 1
135 1 array_to_string $_[0]
136 }
137  
138 4 ▶sub is_null {
Subroutine is_null: called CC=2 SCAR=7.1
Pod undocumented
139 4 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 4 0 4
140 4 not @{ $_[0] }
141 }
142  
143 # Do *not* provide `is_pair`, though, since this is not a pair based
144 # data structure? Or is the `is_null` already evil because of this and
145 # should be named `is_empty`?
146  
147 16 ▶sub values {
Subroutine values: called CC=1 SCAR=0.0
Pod undocumented
148 16 @{ $_[0] }
149 }
150  
151 35 *cons = flip \&FP::List::pair; # XX ? Also, XXX FP::List might not
152 # be loaded here
153 35 *first = \&array_first;
154 35 *maybe_first = \&array_maybe_first;
155 35 *perhaps_first = \&array_perhaps_first;
156 35 *rest = blessing \&array_rest;
157 35 *maybe_rest = blessing \&array_maybe_rest;
158 35 *perhaps_rest = blessing \&array_perhaps_rest;
159  
160 2 ▶sub first_and_rest {
Subroutine first_and_rest: called CC=2 SCAR=7.1
Pod undocumented
161 2 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 2 0 2
162 2 my ($a) = @_;
163 2 (array_first($a), ref($a)->new_from_array(array_rest($a)))
164 }
165  
166 # XXX ah could have used blessing_snd ^ v
167 0 ▶sub maybe_first_and_rest {
Subroutine maybe_first_and_rest: not called CC=3 SCAR=24.8
Pod undocumented
168 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
169 0 my ($a) = @_;
170 0 ▶ @$a ? (array_first($a), ref($a)->new_from_array(array_rest($a))) : undef
Branch: 1 branch
BranchTrueFalseTotal
@$a ? : 0 0 0
171 }
172  
173 0 ▶sub perhaps_first_and_rest {
Subroutine perhaps_first_and_rest: not called CC=3 SCAR=24.8
Pod undocumented
174 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
175 0 my ($a) = @_;
176 0 ▶ @$a ? (array_first($a), ref($a)->new_from_array(array_rest($a))) : ()
Branch: 1 branch
BranchTrueFalseTotal
@$a ? : 0 0 0
177 }
178 35 *second = \&array_second;
179 35 *last = \&array_last;
180 35 *ref = \&array_ref;
181 35 *FP_Sequence_ref = \&ref;
182 35 *length = \&array_length;
183  
184 1 ▶sub FP_Sequence_length {
Subroutine FP_Sequence_length: called CC=1 SCAR=0.0
Pod undocumented
185 1 my ($self, $prefixlen) = @_;
186 1 $prefixlen + $self->length
187 }
188 35 *set = blessing \&array_set;
189 35 *update = blessing \&array_update;
190 35 *push = blessing \&array_push;
191 35 *pop = blessing_snd \&array_pop;
192 35 *shift = blessing_snd \&array_shift;
193 35 *unshift = blessing \&array_unshift;
194 35 *sub = blessing \&array_sub;
195 35 *take = blessing \&array_take;
196 35 *drop = blessing \&array_drop;
197 35 *drop_while = blessing flip \&array_drop_while;
198 35 *take_while = blessing flip \&array_take_while;
199 35 *take_while_and_rest = blessing2 flip \&array_take_while_and_rest;
200 35 *append = blessing \&array_append;
201 35 *reverse = blessing \&array_reverse;
202 35 *xone = \&array_xone;
203 35 *perhaps_one = \&array_perhaps_one;
204 35 *hashing_uniq = blessing \&array_hashing_uniq;
205 35 *zip2 = blessing \&array_zip2;
206 35 *for_each = flip \&array_for_each;
207 35 *for_each_with_islast = flip \&array_for_each_with_islast;
208 35 *map = blessing flip \&array_map;
209 35 *filtermap = blessing flip \&array_filtermap;
210 35 *map_with_index = blessing flip \&array_map_with_index;
211 35 *map_with_islast = blessing flip \&array_map_with_islast;
212 35 *filter = blessing flip \&array_filter;
213 35 *zip = blessing \&array_zip;
214 35 *fold = rot3left \&array_fold;
215 35 *fold_right = rot3left \&array_fold_right;
216 35 *preferred_fold = \&fold; # ?
217 35 *intersperse = blessing \&array_intersperse;
218 35 *strings_join = \&array_strings_join;
219 35 *every = flip \&array_every;
220 35 *any = flip \&array_any;
221 35 *sum = \&array_sum;
222 35 *hash_group_by = \&array_to_hash_group_by;
223  
224 35 *sort = blessing \&array_sort;
225 35 *sortCompare = blessing \&array_sortCompare;
226  
227 # XX provide them as functions, too? (prefixed with `purearray_`) (to
228 # avoid requiring the user to use `the_method` [and perhaps missing
229 # the explicit type check?])
230  
231 35 *perhaps_find_tail = blessing flip \&array_perhaps_find_tail;
232 35 *perhaps_find = flip \&array_perhaps_find;
233 35 *find = perhaps_to_maybe(\&array_perhaps_find);
234 35 *merge = blessing \&array_merge;
235  
236 7 ▶sub group {
Subroutine group: called CC=8 SCAR=20.9
Pod undocumented
237 7 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 7 0 7
Condition33%1/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
007
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
238 7 my ($self, $equal, $maybe_tail) = @_;
239 7 ▶ croak("can't currently handle tail argument for array-likes")
Branch: 1 branch
BranchTrueFalseTotal
if defined $maybe_tail 0 7 7
240 if defined $maybe_tail;
241 7 require FP::PureArray; # ugly?
242 7 my @res;
243 7 my $len = @$self;
244 7 ▶ if ($len >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len >= 1) 6 1 7
245 6 ▶ for (my $i = 0; $i < $len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 12 6 18
246 12 my $cur = $self->[$i];
247 12 $i++;
248 12 my @group = $cur;
249 LP: {
250 12 ▶ if ($i >= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $len) { } 6 12 18
251 6 push @res, FP::_::PureArray->new_from_array(\@group);
252 } else {
253 12 my $a = $self->[$i];
254 12 ▶ if ($equal->($cur, $a)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$equal($cur, $a)) { } 6 6 12
255 6 push @group, $a;
256 6 $i++;
257 6 redo LP;
258 } else {
259 6 push @res, FP::_::PureArray->new_from_array(\@group);
260 6 $i--; # !
261 }
262 }
263 }
264 }
265 }
266 7 FP::_::PureArray->new_from_array(\@res)
267 }
268  
269 35 _END_ # Chj::NamespaceCleanAbove
« lib/FP/Optional.pm ↑ Summary lib/Chj/CPAN/ModulePODUrl.pm »