lib/FP/MutableArray.pm

filter: Statementstmt 81.0% 60 / 74 Branchbran 16.6% 3 / 18 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 78.2% 18 / 23 Podpod 0.0% 0 / 3 totaltotal 65.0% 82 / 126 CC 12 SCAR 16.2
CC
12
Cov
66%
is_mutablearray
20.0
unsafe_mutable
12.0
array_to_purearray
6.0
CRAP 5.1
« lib/Chj/pp.pm ↑ Summary lib/Chj/xpipe.pm »
1 #
2 # Copyright (c) 2019-2020 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::MutableArray
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::MutableArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use FP::Div 'inc';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 my $a = mutablearray(1,4,5)->map(\&inc);
20 is $a->sum, 13;
21 $a->[0]++;
22 is $a->sum, 14;
23  
24 # They can be turned into immutable ones:
25 my $b = $a->pure;
26 like( (eval { $b->[0]++; 1 } || $@),
27 qr/^Modification of a read-only value attempted/);
28  
29 # Although this really consumes the original array, which is now
30 # immutable, too:
31 like( (eval { $a->[0]++; 1 } || $@),
32 qr/^Modification of a read-only value attempted/);
33  
34 is $a->sum, 14;
35  
36 is( FP::_::MutableArray->null == FP::_::MutableArray->null, '' );
37  
38 =head1 DESCRIPTION
39  
40 Native Perl arrays but blessed so that they can follow the sequences
41 protocol.
42  
43 =head1 SEE ALSO
44  
45 Implements: L<FP::Abstract::Sequence>, L<FP::Abstract::Equal>,
46 L<FP::Abstract::Show>
47  
48 L<FP::PureArray>, L<FP::Array>
49  
50 =head1 NOTE
51  
52 This is alpha software! Read the status section in the package README
53 or on the L<website|http://functional-perl.org/>.
54  
55 =cut
56  
57 package FP::MutableArray;
58 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 2 our @EXPORT = qw(is_mutablearray mutablearray array_to_mutablearray);
64 2 our @EXPORT_OK = qw();
65 2 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
66  
67 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 0 ▶sub is_mutablearray {
Subroutine is_mutablearray: not called CC=4 SCAR=30.0
Pod undocumented
71 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
72 0 my ($v) = @_;
73 0 ▶ my $r = blessed($v) // return;
Condition0%0/2
builtin::blessed($v) // (return)
l!l
00
MC/DC0%0/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table0%0/2
A
A= builtin::blessed($v)
Aresult
11
00
74 0 ▶ ($r eq "FP::_::MutableArray" or $v->isa("FP::_::MutableArray"))
Branch: 1 branch
BranchTrueFalseTotal
$r eq "FP::_::MutableArray" or $v->isa("FP::_::MutableArray") 0 0 0
75 }
76  
77 4 ▶sub mutablearray {
Subroutine mutablearray: called CC=1 SCAR=0.0
Pod undocumented
78 4 FP::_::MutableArray->new_from_array([@_])
79 }
80  
81 0 ▶sub array_to_purearray {
Subroutine array_to_purearray: not called CC=2 SCAR=17.9
Pod undocumented
82 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
83 0 FP::_::MutableArray->new_from_array($_[0])
84 }
85  
86 2 package FP::_::MutableArray {
87 2 ▶ use base qw(FP::Array::Mixin);
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶ use FP::Interfaces;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶ use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 ▶ use Chj::NamespaceCleanAbove;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 14 ▶ sub new_from_array {
Subroutine new_from_array: called CC=2 SCAR=7.1
94 14 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 14 0 14
95 14 my ($class, $a) = @_;
96 14 bless $a, $class
97 }
98  
99 0 ▶ sub mutablearray {
Subroutine mutablearray: not called CC=2 SCAR=17.9
100 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
101 0 my $s = shift;
102 0 $s
103 }
104  
105 1 ▶ sub purearray {
Subroutine purearray: called CC=2 SCAR=7.1
106 1 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1 0 1
107 1 my $s = shift;
108 1 FP::_::PureArray->new_from_array($s)
109 }
110  
111 2 ▶ sub null {
Subroutine null: called CC=2 SCAR=7.6
112 2 my $proto = shift;
113 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
114  
115 # can't cache this since mutation is allowed!:
116 2 $class->new_from_array([])
117 }
118  
119 0 ▶ sub constructor_name {
Subroutine constructor_name: not called CC=1 SCAR=6.9
120 0 "mutablearray"
121 }
122  
123 1 ▶ sub pure {
Subroutine pure: called CC=2 SCAR=7.1
124 1 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1 0 1
125  
126 # same as `array_to_purearray`
127 1 require FP::PureArray; # cost?
128 1 FP::_::PureArray->new_from_array($_[0])
129 }
130  
131 2 our $unsafe_mutable_warned = 0;
132  
133 0 ▶ sub unsafe_mutable {
Subroutine unsafe_mutable: not called CC=3 SCAR=24.8
134 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
135 0 my $a = shift;
136 0 ▶ carp "is already mutable" unless $unsafe_mutable_warned++;
Branch: 1 branch
BranchTrueFalseTotal
unless $FP::_::MutableArray::unsafe_mutable_warned++ 0 0 0
137 0 $a
138 }
139  
140 2 _END_; # Chj::NamespaceCleanAbove
141  
142 2 FP::Interfaces::implemented qw(
143 FP::Abstract::Sequence
144 FP::Abstract::Equal
145 FP::Abstract::Show);
146 }
147  
148 2 1
« lib/Chj/pp.pm ↑ Summary lib/Chj/xpipe.pm »