问题描述
raku 网页说,应在归约运算符中为用户定义的函数使用额外的括号:https://docs.raku.org/language/operators#Reduction_metaoperators
但是,当我将函数作为变量传递时出现错误(我使用的是 Rakudo Star 2010.10):
> sub lessthan ($a,$b) { $a < $b }
&lessthan
> my @a = ((1,2,3),(6,5,4))
[(1 2 3) (6 5 4)]
> sub x (@arrOfArr,&func) { say @arrOfArr.grep( { [[&func]] ($_[0],$_[1],$_[2]) } ); }
&x
> x(@a,&lessthan)
((1 2 3) (6 5 4)) # <----------------------------------- this is not what I expected
> say @a.grep( { [<] ($_[0],$_[2]) } );
((1 2 3)) # <------------------------------------------- this is what I want
那么,我在这里做错了什么?
解决方法
有两个问题。
首先,您需要将 is assoc<chain>
特征添加到您的子项中。
但它仍然不起作用,因为有一个错误需要修复:Reduction with a function reference fails to honour chain
associativity。