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.10';
4              
5 15     15   218 use v5.24;
  15         43  
6 15     15   70 use warnings;
  15         43  
  15         287  
7 15     15   59 use Moo;
  15         24  
  15         69  
8 15     15   8856 use Quantum::Superpositions::Lazy::Util qw(is_collapsible);
  15         41  
  15         935  
9 15     15   6936 use Types::Common::Numeric qw(PositiveOrZeroNum);
  15         1083054  
  15         114  
10 15     15   6015 use Types::Standard qw(Defined Bool);
  15         33  
  15         92  
11 15     15   9541 use Carp qw(croak);
  15         35  
  15         887  
12              
13 15     15   5879 use namespace::clean;
  15         136723  
  15         89  
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 2 my ($self) = @_;
40              
41 1 50       13 if ($self->collapsible) {
42 1         10 $self->value->reset;
43             }
44             }
45              
46             sub clone
47             {
48 16895     16895 1 20114 my ($self) = @_;
49              
50             return $self->new(
51 16895         211864 $self->%{qw(value weight)}
52             );
53             }
54              
55             sub merge
56             {
57 1     1 1 3 my ($self, $with) = @_;
58              
59 1 50       6 croak "cannot merge a state: values mismatch"
60             if $self->value ne $with->value;
61              
62 1         21 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 29084 my ($self, %transformers) = @_;
71              
72 16898         23433 my $cloned = $self->clone;
73 16898         875560 for my $to_transform (keys %transformers) {
74 16898 50 33     63718 if ($self->can($to_transform) && exists $cloned->{$to_transform}) {
75 16898         34880 $cloned->{$to_transform} = $transformers{$to_transform}->($cloned->{$to_transform});
76             }
77             }
78              
79 16898         32167 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