How to connect to an embedded unit in Perl 6?

I want to change the array (I use splice in this example, but it can be any operation that modifies the array) and returns the changed array - unlike slice , which returns the elements extracted from the array. I can do this easily by storing the 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 embed the block represented by $l in one expression? Obvious substitution does not 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?

+6
source share
1 answer

Add one & to the right place.

 say (^6).map( { $_ < 4 ?? 0 !! $_ } ).Array.&(-> $a { splice($a,1,3,[1,2,3]); $a }) # OUTPUTยซ[0 1 2 3 4 5]โคยป 
+8
source

All Articles