Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Last active May 24, 2024 13:38
Show Gist options
  • Save ab5tract/7b6eda84697bd75c8b8cea61f68e7656 to your computer and use it in GitHub Desktop.
Save ab5tract/7b6eda84697bd75c8b8cea61f68e7656 to your computer and use it in GitHub Desktop.
reduce with postcircumfix
sub (+values) {
my $iter := values.iterator;
- nqp::if(
- nqp::eqaddr((my $result := $iter.pull-one),IterationEnd),
+ return nqp::if(
+ nqp::eqaddr(nqp::decont(my $result = $iter.pull-one),IterationEnd),
op.(), # identity
nqp::if(
- nqp::eqaddr((my $value := $iter.pull-one),IterationEnd),
+ nqp::eqaddr(nqp::decont(my $value = $iter.pull-one),IterationEnd),
nqp::if(
nqp::isle_i(op.arity,1),
op.($result), # can call with 1 param
$result # what we got
),
nqp::stmts(
- ($result := op.($result,$value)),
+ ($result = op.($result,$value)),
nqp::until(
- nqp::eqaddr(($value := $iter.pull-one),IterationEnd),
- ($result := op.($result,$value))
+ nqp::eqaddr(nqp::decont($value = $iter.pull-one),IterationEnd),
+ ($result = op.($result, $value))
),
$result # final result
)
my %hash;
my @path = <a b c>;
#my @path = ('a', Any, 'b', Any);
my $value = 42;
my $slot := (%hash, |@path).reduce: &postcircumfix:<{ }>;
dd :$slot, :contoto($slot.VAR.^name), :type($slot.WHAT.raku);
$slot = $value; # ERROR: Cannot assign to an immutable value
say %hash; # EXPECTED: {a => {b => {c => 42}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment