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             $Quantum::Superpositions::Lazy::State::VERSION = '1.12';
3 15     15   183 use v5.24;
  15         49  
4 15     15   67 use warnings;
  15         27  
  15         301  
5 15     15   67 use Moo;
  15         27  
  15         65  
6 15     15   8837 use Quantum::Superpositions::Lazy::Util qw(is_collapsible);
  15         37  
  15         873  
7 15     15   7015 use Types::Common::Numeric qw(PositiveOrZeroNum);
  15         1156678  
  15         117  
8 15     15   6430 use Types::Standard qw(Defined Bool);
  15         36  
  15         78  
9 15     15   10433 use Carp qw(croak);
  15         35  
  15         845  
10              
11 15     15   6316 use namespace::clean;
  15         147802  
  15         94  
12              
13             has "weight" => (
14             is => "ro",
15             isa => PositiveOrZeroNum,
16             default => sub { 1 },
17             );
18              
19             # TODO: should this assert for definedness?
20             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             default => sub {
31             is_collapsible $_[0]->value
32             },
33             );
34              
35             sub reset
36             {
37 1     1 1 3 my ($self) = @_;
38              
39 1 50       15 if ($self->collapsible) {
40 1         11 $self->value->reset;
41             }
42             }
43              
44             sub clone
45             {
46 16895     16895 1 22254 my ($self) = @_;
47              
48             return $self->new(
49 16895         239714 $self->%{qw(value weight)}
50             );
51             }
52              
53             sub merge
54             {
55 1     1 1 3 my ($self, $with) = @_;
56              
57 1 50       6 croak "cannot merge a state: values mismatch"
58             if $self->value ne $with->value;
59              
60 1         18 return $self->new(
61             weight => $self->weight + $with->weight,
62             value => $self->value,
63             );
64             }
65              
66             sub clone_with
67             {
68 16898     16898 1 33526 my ($self, %transformers) = @_;
69              
70 16898         28178 my $cloned = $self->clone;
71 16898         1006337 for my $to_transform (keys %transformers) {
72 16898 50 33     72846 if ($self->can($to_transform) && exists $cloned->{$to_transform}) {
73 16898         41108 $cloned->{$to_transform} = $transformers{$to_transform}->($cloned->{$to_transform});
74             }
75             }
76              
77 16898         36559 return $cloned;
78             }
79              
80             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 of numeric type
102             (positive), which is optional and 1 by default, and I 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 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 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). The weights are added together on the
134             resulting state.
135              
136             =head1 SEE ALSO
137              
138             L
139