Ugly way to solve it Though, I hate mutable data. package Talk; use Moo; has speaker => ( is => ’rw’, requires => 1 ); has name => ( is => ’rw’, requires => 1 );
Another way : use Lens Lens acts like an accessor # getter (will get ’hiratara’) (talk . speaker . name)->($schedule); # setter $new_schedule = (talk . speaker . name)->($schedule, ’hiratara’);
costate comonad in Perl package Comonad::Costate; use Moo; extends ’Comonad’; has func => (is => ’ro’); has state => (is => ’ro’); sub counit my $self = shift; $self->func
costate comonad in Perl sub coflatten { my $self = shift; my $class = ref $self; my $self_func = $self->func; $class->new( func => sub { my $state = shift; $class->new( func => $self_func, state => $state, ); }, state => $self->state, ); }
costate comonad coalgebra costate comonad coalgebra is the combination of setter and getter. This is Lens. A → (AS × S) ≃ (A → AS) × (A → S) • A → AS is setter • A → S is getter
Lens is more powerful $schedule = (talk . speaker . name)->($schedule, ’hiratara’); # Setter and Getter of a part of string $schedule = (talk . speaker . name . substr_lens(3, 2)) ->($schedule, ’@’); # $schedule->talk->speaker->name will be "[email protected]"
CONCLUSION • Lens is a good setter for immutable data • https://github.com/hiratara/p5-Lens To tell the truth, haskell’s lens implementation has the type: Let (−(V))V, −(S): SetSet → Set be functors (−(V))V ⇒ −(S)