File Coverage

blib/lib/Quantum/Superpositions/Lazy/State.pm
Criterion Covered Total %
statement 37 37 100.0
branch 3 6 50.0
condition 1 3 33.3
subroutine 12 12 100.0
pod 4 4 100.0
total 57 62 91.9


line stmt bran cond sub pod time code
1             package Quantum::Superpositions::Lazy::State;
2              
3             our $VERSION = '1.11';
4              
5 15     15   194 use v5.24;
  15         50  
6 15     15   68 use warnings;
  15         30  
  15         307  
7 15     15   65 use Moo;
  15         24  
  15         66  
8 15     15   9227 use Quantum::Superpositions::Lazy::Util qw(is_collapsible);
  15         41  
  15         923  
9 15     15   7806 use Types::Common::Numeric qw(PositiveOrZeroNum);
  15         1178637  
  15         119  
10 15     15   6751 use Types::Standard qw(Defined Bool);
  15         37  
  15         90  
11 15     15   10500 use Carp qw(croak);
  15         55  
  15         883  
12              
13 15     15   6364 use namespace::clean;
  15         150855  
  15         95  
14              
15             has "weight" => (
16             is => "ro",
17             isa => PositiveOrZeroNum,
18             default => sub { 1 },
19             );
20              
21             # TODO: should this assert for definedness?
22             has "value" => (
23             is => "ro",
24             isa => Defined,
25             required => 1,
26             );
27              
28             has 'collapsible' => (
29             is => 'ro',
30             isa => Bool,
31             lazy => 1,
32             default => sub {
33             is_collapsible $_[0]->value
34             },
35             );
36              
37             sub reset
38             {
39 1     1 1 3 my ($self) = @_;
40              
41 1 50       14 if ($self->collapsible) {
42 1         11 $self->value->reset;
43             }
44             }
45              
46             sub clone
47             {
48 16895     16895 1 23642 my ($self) = @_;
49              
50             return $self->new(
51 16895         248059 $self->%{qw(value weight)}
52             );
53             }
54              
55             sub merge
56             {
57 1     1 1 4 my ($self, $with) = @_;
58              
59 1 50       8 croak "cannot merge a state: values mismatch"
60             if $self->value ne $with->value;
61              
62 1         19 return $self->new(
63             weight => $self->weight + $with->weight,
64             value => $self->value,
65             );
66             }
67              
68             sub clone_with
69             {
70 16898     16898 1 33426 my ($self, %transformers) = @_;
71              
72 16898         29089 my $cloned = $self->clone;
73 16898         1018949 for my $to_transform (keys %transformers) {
74 16898 50 33     70814 if ($self->can($to_transform) && exists $cloned->{$to_transform}) {
75 16898         38919 $cloned->{$to_transform} = $transformers{$to_transform}->($cloned->{$to_transform});
76             }
77             }
78              
79 16898         37094 return $cloned;
80             }
81              
82             1;
83              
84             =head1 NAME
85              
86             Quantum::Superpositions::Lazy::State - a weighted state implementation
87              
88             =head1 DESCRIPTION
89              
90             This is a simple implementation of a state that contains a weight and a value.
91             The states are meant to be immutable, so once created you cannot change the
92             value or weight of a state (without cloning it).
93              
94             =head1 METHODS
95              
96             =head2 new
97              
98             my $state = Quantum::Superpositions::Lazy::State->new(
99             weight => 2,
100             value => "on"
101             );
102              
103             A generic Moose constructor. Accepts two arguments: I of numeric type
104             (positive), which is optional and 1 by default, and I of any type
105             (defined), which is required.
106              
107             =head2 weight
108              
109             Returns the weight.
110              
111             =head2 value
112              
113             Returns the value.
114              
115             =head2 reset
116              
117             Resets the state of a superposition inside of the I attribute, if that
118             value is indeed a superposition.
119              
120             =head2 clone
121              
122             Creates a new state with the parameters of the current one.
123              
124             =head2 clone_with
125              
126             # doubles the weight on the cloned state
127             $state->clone_with(weight => sub { shift() * 2 });
128              
129             Clones the objects with I and then applies some transformators on top of
130             the object fields.
131              
132             =head2 merge
133              
134             Merges two states into one. Only possible for values that are the same
135             (compared as strings with I). The weights are added together on the
136             resulting state.
137              
138             =head1 SEE ALSO
139              
140             L
141