lib/FP/PureHash.pm

filter: Statementstmt 64.1% 86 / 134 Branchbran 21.1% 11 / 52 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 65.6% 21 / 32 Podpod 0.0% 0 / 4 totaltotal 52.4% 118 / 225 CC 31 SCAR 21.6
CC
31
Cov
52%
FP_Equal_equal
30.0
new_from_hash
30.0
is_purehash
12.0
CRAP 8.6
« .htmlgen/FunctionalPerl/Htmlgen/FileUtil.pm ↑ Summary lib/FP/Char.pm »
1 #
2 # Copyright (c) 2019-2021 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::PureHash
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::PureHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 use FP::Show; use FP::Predicates;
18  
19 my $h = purehash hi => 123, there => "World";
20 my $h2 = $h->set("hi", "again");
21 is $h->ref("there"), "World";
22 is $$h{there}, "World";
23 is show($h), "purehash('hi' => 123, 'there' => 'World')";
24 is show($h2), "purehash('hi' => 'again', 'there' => 'World')";
25 is is_pure($h2), 1;
26  
27 like( (eval { purehash hi => 1, hi => 1, there => 2 } || $@),
28 qr/^duplicate key: 'hi' at/ );
29 like( (eval { $$h{there_} || 1} || $@),
30 # the detailed exception message may change!
31 qr/^Attempt to access disallowed key 'there_' in a restricted hash/ );
32  
33 =head1 DESCRIPTION
34  
35 Hash tables, currently based simply on Perl's internal hashes. They
36 are immutable, and restricted which means that accessing non-existing
37 keys yields an exception.
38  
39  
40 =head1 TODO
41  
42 - performant functional updates (currently the `set` method simply
43 copies the whole table)
44  
45 - more methods, move/adapt set functionality from FP::Hash and
46 FP::HashSet
47  
48 - a maybe_ref that returns FP::Failure or FP::Maybe ?
49  
50 - non-string keys?
51  
52 =head1 SEE ALSO
53  
54 Implements: L<FP::Abstract::Pure>, L<FP::Abstract::Map>,
55 L<FP::Abstract::Equal>, L<FP::Abstract::Show>
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::PureHash;
65 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 3 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 3 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 3 our @EXPORT = qw(purehash);
71 3 our @EXPORT_OK = qw(hash_clone_to_purehash hash_to_purehash);
72 3 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
73  
74 3 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 3 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 3 our $immutable = 1;
80  
81 7 ▶sub purehash {
Subroutine purehash: called CC=6 SCAR=18.1
Pod undocumented
82 7 __
83 'convert key/value pairs to an immutable hash; re-use of keys is an error';
84 7 ▶ die "uneven number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 6 6
85 7 my %out;
86 7 ▶ for (my $i = 0; $i < @_; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 32 5 37
87 33 my $k = $_[$i];
88 33 ▶ if (exists $out{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $out{$k}) 1 31 32
89 1 die "duplicate key: " . show($k);
90 }
91 31 $out{$k} = $_[$i + 1];
92 31 ▶ Internals::SvREADONLY $out{$k}, 1 if $FP::PureHash::immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $FP::PureHash::immutable 31 0 31
93 }
94 5 my $res = bless \%out, "FP::_::PureHash";
95 5 ▶ Internals::SvREADONLY %out, 1 if $FP::PureHash::immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $FP::PureHash::immutable 5 0 5
96  
97 # XX ^ this also changes the behaviour accessing a non-existing key, yeah;
98 # why not just overload? oh that was said to be slow or was it Tie ?
99 5 $res
100 }
101  
102 1 ▶sub hash_clone_to_purehash {
Subroutine hash_clone_to_purehash: called CC=2 SCAR=17.9
Pod undocumented
103 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
104 0 FP::_::PureHash->new_from_hash({ %{ $_[0] } })
105 }
106  
107 0 ▶sub hash_to_purehash {
Subroutine hash_to_purehash: not called CC=2 SCAR=17.9
Pod undocumented
108 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
109 0 FP::_::PureHash->new_from_hash($_[0])
110 }
111  
112 0 ▶sub is_purehash {
Subroutine is_purehash: not called CC=3 SCAR=24.8
Pod undocumented
113 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
114 0 my ($v) = @_;
115 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
116 0 $v->isa("FP::_::PureHash")
117 }
118  
119 3 package FP::Hash::Mixin {
120 3 ▶ use FP::Equal 'equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
121 3 ▶ use Chj::NamespaceCleanAbove;
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 2 ▶ sub FP_Show_show {
Subroutine FP_Show_show: called CC=1 SCAR=0.0
124 2 my ($s, $show) = @_;
125 $s->constructor_name . "("
126 . join(", ",
127 2 map { &$show($_) . " => " . &$show($$s{$_}) } sort keys %$s)
128 . ")"
129 }
130  
131 0 ▶ sub FP_Equal_equal {
Subroutine FP_Equal_equal: not called CC=5 SCAR=34.0
132 0 my ($a, $b) = @_;
133 0 ▶ keys(%$a) == keys(%$b) and do {
Branch: 1 branch
BranchTrueFalseTotal
keys %$a == keys %$b and do { foreach my $key (keys %$a) { exists $b->{$key} or return 0; equal($a->{$key}, $b->{$key}) or return 0; } ; 1 } 0 0 0
134 0 for my $key (keys %$a) {
135 0 ▶ exists $$b{$key} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $b->{$key} or return 0 0 0 0
136 0 ▶ equal($$a{$key}, $$b{$key}) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
equal($a->{$key}, $b->{$key}) or return 0 0 0 0
137 }
138 1
139 0 }
140 }
141  
142 3 _END_
143 }
144  
145 3 package FP::PureHash::autobox {
146 3 our $AUTOLOAD;
147  
148 0 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
149 0 my $methodname = $AUTOLOAD;
150 0 $methodname =~ s/.*:://;
151 0 my $v = FP::_::PureHash->new_from_hash($_[0]);
152 0 ▶ if (my $m = $v->can($methodname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $v->can($methodname)) { } 0 0 0
153 0 goto $m
154 } else {
155 0 die "no method '$methodname' found for object: $v";
156 }
157 }
158 }
159  
160 3 package FP::_::PureHash {
161 3 ▶ use base "FP::Hash::Mixin";
Subroutine BEGIN: called CC=1 SCAR=0.0
162 3 ▶ use FP::Interfaces;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 3 ▶ use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 3 ▶ use Chj::NamespaceCleanAbove;
Subroutine BEGIN: called CC=1 SCAR=0.0
165  
166 0 ▶ sub new_from_hash {
Subroutine new_from_hash: not called CC=5 SCAR=34.0
167 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
168 0 my ($class, $out) = @_;
169 0 ▶ if ($FP::PureHash::immutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FP::PureHash::immutable) 0 0 0
170 0 for my $k (keys %$out) {
171 0 Internals::SvREADONLY $out->{$k}, 1
172 }
173 }
174 0 my $res = bless $out, "FP::_::PureHash";
175 0 ▶ Internals::SvREADONLY %$out, 1 if $FP::PureHash::immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $FP::PureHash::immutable 0 0 0
176 0 $res
177 }
178  
179 2 ▶ sub constructor_name {"purehash"}
Subroutine constructor_name: called CC=1 SCAR=0.0
180  
181 # XX why not get, again? set and get? If ref, then what for set?
182 1 ▶ sub ref {
Subroutine ref: called CC=2 SCAR=7.1
183 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
184 1 my ($s, $key) = @_;
185 1 $$s{$key}
186 }
187  
188 0 ▶ sub perhaps_ref {
Subroutine perhaps_ref: not called CC=3 SCAR=24.8
189 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
190 0 my ($s, $key) = @_;
191 0 ▶ exists $$s{$key} ? $$s{$key} : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $s->{$key} ? : 0 0 0
192 }
193  
194 1 ▶ sub set {
Subroutine set: called CC=5 SCAR=16.4
195 1 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 1 0 1
196 1 my ($s, $key, $val) = @_;
197  
198 # XX the inefficient approach... to be replaced with new impl.
199 1 my %out = %$s;
200 1 $out{$key} = $val;
201 1 ▶ if ($FP::PureHash::immutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FP::PureHash::immutable) 1 0 1
202 1 for my $k (keys %out) {
203 2 Internals::SvREADONLY $out{$k}, 1
204 }
205 }
206 1 my $res = bless \%out, "FP::_::PureHash";
207 1 ▶ Internals::SvREADONLY %out, 1 if $FP::PureHash::immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $FP::PureHash::immutable 1 0 1
208 1 $res
209 }
210  
211 0 ▶ sub key_value_pairs {
Subroutine key_value_pairs: not called CC=2 SCAR=17.9
212 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
213 0 my ($h) = @_;
214 0 map { [$_, $h->{$_}] } sort keys %$h
215 }
216  
217 0 ▶ sub array {
Subroutine array: not called CC=2 SCAR=17.9
218 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
219 0 my ($h) = @_;
220 0 [$h->key_value_pairs]
221 }
222  
223 0 ▶ sub purearray {
Subroutine purearray: not called CC=2 SCAR=17.9
224 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
225 0 my ($h) = @_;
226  
227 # XX load FP::PureArray or not, here or always?
228 0 FP::_::PureArray->new_from_array($h->array)
229 }
230  
231 0 ▶ sub list {
Subroutine list: not called CC=2 SCAR=17.9
232 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
233 0 my ($h) = @_;
234  
235 # XX load FP::List or not, here or always?
236 0 FP::List::array_to_list($h->array)
237 }
238  
239 # Should we add this? Auto-choose the best sequence for the task?
240 0 ▶ sub sequence {
Subroutine sequence: not called CC=2 SCAR=17.9
241 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
242 0 my ($h) = @_;
243 0 $h->purearray
244 }
245  
246 3 FP::Interfaces::implemented qw(
247 FP::Abstract::Pure
248 FP::Abstract::Map
249 FP::Abstract::Equal
250 FP::Abstract::Show);
251  
252 3 _END_
253 }
254  
255 3 1
« .htmlgen/FunctionalPerl/Htmlgen/FileUtil.pm ↑ Summary lib/FP/Char.pm »