Created
August 9, 2023 20:54
-
-
Save vendethiel/18c206f9863813162ede20b283e50822 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub needs-change($o) { $o->name eq 'Seek' } | |
sub change($o) { | |
my %props = %o{qw/name level x y/}; | |
my @perks = @$o->perks; | |
push @perks, $perk_to_add; | |
%props{perks} = \@perks; | |
bless %props, ref($o); | |
} | |
my @updated = map { needs-change($_) ? change($_) : $_ } @objects; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lens definition -- needs to be done one per key, not every time you have such an operation! | |
my $perks = immutable::lens->make(sub { $_[0]->{perks} }, sub { clone $_[0], perks => $_[1] }); | |
# Actual code | |
sub with_name($name) { lens->grep({$_->name eq $name}); } | |
my @updated = with_name()->compose($perks)->modify(@objects, { [@$_[0], $new_perk] }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment