Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`reduce` needlessly deconts #2000

Open
smls opened this issue Jun 30, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@smls
Copy link
Contributor

commented Jun 30, 2018

Motivation

I tried to do a multi-dimensional Hash access with the help of reduce, as follows:

my %hash;
my @path = <a b c>;
my $value = 42;

my $slot := (%hash, |@path).reduce: &postcircumfix:<{ }>;

$slot = $value;  # ERROR: Cannot assign to an immutable value
say %hash;       # EXPECTED: {a => {b => {c => 42}}}

For non-existent elements, postcircumfix { } returns Scalar containers which autovivify when subscripted or assigned to. Therefore, it is critical in this example to pass these Scalars on and not decont them.

Unfortunately, reduce decontainerizes its result before returning it, rendering this example non-functional.

Investigation

It looks like reduce does preserve the containers when passing values from one invocation of the callback to the next, and only deconts the final result, as the following example demonstrates:

my $x = 1;
my $result := ($x, 2, 3, 4).reduce: -> $a is raw, $ {
    say ($a, $a.VAR.^name);
    $a
};
say ($result, $result.VAR.^name);

Output:

(1 Scalar)
(1 Scalar)
(1 Scalar)
(1 Int)

Maybe it's just a missing is rw on sub reduce?

Expected Behavior

reduce should return the result exactly as the last invocation of the callback produced it, without deconting.

Environment

This is Rakudo version 2018.06-25-ge9351cbaa built on MoarVM version 2018.06
implementing Perl 6.c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.