如何链接到Perl 6中的内联块?

时间:2022-08-09 22:04:22

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:

我想修改一个数组(我在这个例子中使用splice,但它可以是修改数组的任何操作)并返回修改后的数组 - 与slice不同,它返回从数组中拉出的项目。我可以通过在数组中存储块来轻松完成,如下所示:

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:

如何将$ l表示的块内联到单个表达式中?显而易见的替代不起作用:

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?

1 个解决方案

#1


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]␤»

#1


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]␤»