blib/lib/Quantum/Superpositions/Lazy/State.pm

filter: Statementstmt 100% 44 / 44 Branchbran 50.0% 3 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 90.4% 66 / 73 CC 6 SCAR 6.5
CC
6
Cov
91%
clone_with
4.3
reset
2.0
merge
2.0
CRAP 1.9
« blib/lib/Quantum/Superpositions/Lazy.pm ↑ Summary blib/lib/Quantum/Superpositions/Lazy/Role/Collapsible.pm »
1 package Quantum::Superpositions::Lazy::State;
2 15 $Quantum::Superpositions::Lazy::State::VERSION = '1.12';
3 15 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Quantum::Superpositions::Lazy::Util qw(is_collapsible);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Types::Common::Numeric qw(PositiveOrZeroNum);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Types::Standard qw(Defined Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 has "weight" => (
14 is => "ro",
15 isa => PositiveOrZeroNum,
16 16317 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 15 );
18  
19 # TODO: should this assert for definedness?
20 15 has "value" => (
21 is => "ro",
22 isa => Defined,
23 required => 1,
24 );
25  
26 has 'collapsible' => (
27 is => 'ro',
28 isa => Bool,
29 lazy => 1,
30 16422 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 16422 is_collapsible $_[0]->value
32 },
33 15 );
34  
35 sub reset
36 1 ▶{
Subroutine reset: called CC=2 SCAR=7.1
Pod documented
37 1 my ($self) = @_;
38  
39 1 ▶ if ($self->collapsible) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->collapsible) 1 0 1
40 1 $self->value->reset;
41 }
42 }
43  
44 sub clone
45 16895 ▶{
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
46 16895 my ($self) = @_;
47  
48 return $self->new(
49 16895 $self->%{qw(value weight)}
50 );
51 }
52  
53 sub merge
54 1 ▶{
Subroutine merge: called CC=2 SCAR=7.1
Pod documented
55 1 my ($self, $with) = @_;
56  
57 1 ▶ croak "cannot merge a state: values mismatch"
Branch: 1 branch
BranchTrueFalseTotal
if $self->value ne $with->value 0 1 1
58 if $self->value ne $with->value;
59  
60 1 return $self->new(
61 weight => $self->weight + $with->weight,
62 value => $self->value,
63 );
64 }
65  
66 sub clone_with
67 16898 ▶{
Subroutine clone_with: called CC=4 SCAR=14.6
Pod documented
68 16898 my ($self, %transformers) = @_;
69  
70 16898 my $cloned = $self->clone;
71 16898 for my $to_transform (keys %transformers) {
72 16898 ▶ if ($self->can($to_transform) && exists $cloned->{$to_transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($to_transform) and exists $cloned->{$to_transform}) 16898 0 16898
Condition33%1/3
$self->can($to_transform) and exists $cloned->{$to_transform}
!ll&&!rl&&r
0016898
MC/DC0%0/2
$self->can($to_transform) and exists $cloned->{$to_transform}
$self->can($to_transform) exists $cloned->{$to_transform}
Truth table33%1/3
A and B
A= $self->can($to_transform)B= exists $cloned->{$to_transform}
ABresult
0X0
100
111
73 16898 $cloned->{$to_transform} = $transformers{$to_transform}->($cloned->{$to_transform});
74 }
75 }
76  
77 16898 return $cloned;
78 }
79  
80 15 1;
81  
82 =head1 NAME
83  
84 Quantum::Superpositions::Lazy::State - a weighted state implementation
85  
86 =head1 DESCRIPTION
87  
88 This is a simple implementation of a state that contains a weight and a value.
89 The states are meant to be immutable, so once created you cannot change the
90 value or weight of a state (without cloning it).
91  
92 =head1 METHODS
93  
94 =head2 new
95  
96 my $state = Quantum::Superpositions::Lazy::State->new(
97 weight => 2,
98 value => "on"
99 );
100  
101 A generic Moose constructor. Accepts two arguments: I<weight> of numeric type
102 (positive), which is optional and 1 by default, and I<value> of any type
103 (defined), which is required.
104  
105 =head2 weight
106  
107 Returns the weight.
108  
109 =head2 value
110  
111 Returns the value.
112  
113 =head2 reset
114  
115 Resets the state of a superposition inside of the I<value> attribute, if that
116 value is indeed a superposition.
117  
118 =head2 clone
119  
120 Creates a new state with the parameters of the current one.
121  
122 =head2 clone_with
123  
124 # doubles the weight on the cloned state
125 $state->clone_with(weight => sub { shift() * 2 });
126  
127 Clones the objects with I<clone> and then applies some transformators on top of
128 the object fields.
129  
130 =head2 merge
131  
132 Merges two states into one. Only possible for values that are the same
133 (compared as strings with I<eq>). The weights are added together on the
134 resulting state.
135  
136 =head1 SEE ALSO
137  
138 L<Quantum::Superpositions::Lazy::ComputedState>
139  
« blib/lib/Quantum/Superpositions/Lazy.pm ↑ Summary blib/lib/Quantum/Superpositions/Lazy/Role/Collapsible.pm »