lib/FP/Collection.pm

filter: Statementstmt 95.3% 62 / 65 Branchbran 60.0% 6 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 1 totaltotal 86.8% 86 / 99 CC 9 SCAR 8.2
CC
9
Cov
88%
FP_Equal_equal
5.4
Collection
3.0
contains
2.1
CRAP 2.3
« lib/FP/Repl/AutoTrap.pm ↑ Summary lib/FP/autobox.pm »
1 #
2 # Copyright (c) 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::Collection
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Equal qw(equal is_equal);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use FP::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 my $c= Collection 10, 12, 9, 13;
19 ok $c->contains(12);
20 ok not $c->contains(11);
21 # my $c2 = $c->add(11); TODO
22 # ok $c2->contains(11);
23 # ok not $c->contains(11);
24  
25 # always sorted? Oh, and by string?
26 is_equal [$c->values], [10, 12, 13, 9];
27  
28 is_equal Collection(10, 13), Collection(13, 10);
29 ok not equal Collection(10, 13), Collection(13, 10, 11);
30 ok not equal Collection(10, 13), Collection(13, 10, 11);
31  
32 =head1 DESCRIPTION
33  
34 Unlike L<FP::HashSet> these are objects, but there's currently not
35 much more to it and those two should either be merged into 1 module
36 (that can handle both blessed and unblessed ones?), or FP::Collection
37 should be the pure variant. Thus this is very much unfinished.
38  
39 =head1 SEE ALSO
40  
41 Implements: L<FP::Abstract::Show>, L<FP::Abstract::Equal>. (TODO: L<FP::Abstract::Sequence>?)
42  
43 Members have to implement (or be covered by L<FP::Id::id>): L<FP::Abstract::Id>.
44  
45 Other: L<FP::HashSet>.
46  
47 =head1 NOTE
48  
49 This is alpha software! Read the status section in the package README
50 or on the L<website|http://functional-perl.org/>.
51  
52 =cut
53  
54 package FP::Collection;
55 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 2 our @EXPORT = qw(Collection);
62 2 our @EXPORT_OK = qw();
63 2 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
64  
65 2 ▶use FP::Id;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 7 ▶sub Collection {
Subroutine Collection: called CC=3 SCAR=11.0
Pod undocumented
68 7 my %c;
69 7 for (@_) {
70 18 my $id = id $_;
71  
72 # ^ optimize via inlining? forever.
73 die "Collection: duplicate elements with id '$id': $c{$id}, $_"
74 18 ▶ if exists $c{$id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $c{$id} 0 18 18
75  
76 # ^ consciously use stringification so that pointers can be
77 # seen?
78 18 $c{$id} = $_;
79 }
80 7 bless \%c, "FP::_::Collection"
81 }
82  
83 2 package FP::_::Collection {
84 2 ▶ use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶ use FP::Id;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶ use FP::Equal;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶ use Chj::NamespaceCleanAbove;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 2 ▶ sub contains {
Subroutine contains: called CC=2 SCAR=7.2
90 2 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 2 0 2
91 2 exists $_[0]->{ id $_[1] }
92 }
93  
94 1 ▶ sub values {
Subroutine values: called CC=2 SCAR=7.0
95 1 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1 0 1
96 1 my ($v) = @_;
97 1 map { $v->{$_} } sort keys %$v
98 }
99  
100 # sub add, or wait till can do this efficiently ?
101  
102 3 ▶ sub FP_Equal_equal {
Subroutine FP_Equal_equal: called CC=5 SCAR=16.8
103 3 my ($a, $b) = @_;
104 3 ▶ %$a == %$b and do {
Branch: 1 branch
BranchTrueFalseTotal
%$a == %$b and do { foreach $_ (keys %$a) { exists $b->{$_} and equal($a->{$_}, $b->{$_}) or return ""; } ; 1 } 1 2 3
105 1 for (keys %$a) {
106 2 ▶ exists $b->{$_} and equal($a->{$_}, $b->{$_}) or return '';
Branch: 1 branch
BranchTrueFalseTotal
exists $b->{$_} and equal($a->{$_}, $b->{$_}) or return "" 2 0 2
Condition33%1/3
exists $b->{$_} and equal($a->{$_}, $b->{$_})
!ll&&!rl&&r
002
MC/DC0%0/2
exists $b->{$_} and equal($a->{$_}, $b->{$_})
exists $b->{$_} equal($a->{$_}, $b->{$_})
Truth table33%1/3
A and B
A= exists $b->{$_}B= equal($a->{$_}, $b->{$_})
ABresult
0X0
100
111
107 }
108 1
109 1 }
110 }
111  
112 0 ▶ sub FP_Show_show {
Subroutine FP_Show_show: not called CC=1 SCAR=6.9
113 0 my ($s, $show) = @_;
114 "Collection("
115 0 . join(", ", map { $show->($s->{$_}) } sort keys %$s) . ")"
116 }
117  
118 2 ▶ use FP::Interfaces;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 2 FP::Interfaces::implemented qw(FP::Abstract::Show FP::Abstract::Equal);
120 }
121  
122 2 1
« lib/FP/Repl/AutoTrap.pm ↑ Summary lib/FP/autobox.pm »