6

I want to modify an array (I'm using splice in this example, but it could be any operation that modifies the array) and return the modified array - unlike slice, which returns the items pulled out of the array. I can do it easily by storing a block in an array, as follows:

my $l = -> $a { splice($a,1,3,[1,2,3]); $a };
say (^6).map( { $_ < 4 ?? 0 !! $_ } ).Array;
# [0 0 0 0 4 5]
say (^6).map( { $_ < 4 ?? 0 !! $_ } ).Array.$l;
# [0 1 2 3 4 5]

How do I inline the block represented by $l into a single expression? The obvious substitution doesn't work:

say (^6).map( { $_ < 4 ?? 0 !! $_ } ).Array.(-> $a { splice($a,1,3,[1,2,3]); $a })
Invocant requires a type object of type Array, but an object instance was passed.  Did you forget a 'multi'?

Any suggestions?

dmaestro12
  • 883
  • 7
  • 15

1 Answers1

8

Add one & at the right spot.

say (^6).map( { $_ < 4 ?? 0 !! $_ } ).Array.&(-> $a { splice($a,1,3,[1,2,3]); $a })
# OUTPUT«[0 1 2 3 4 5]␤»