The raku webpage says that extra bracket should be used for user-defined functions within reduction operator: https://docs.raku.org/language/operators#Reduction_metaoperators
However, I am getting errors when I pass the function as a variable (I am using 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], $_[1], $_[2]) } );
((1 2 3)) # <------------------------------------------- this is what I want
So, what am I doing wrong here?
There are two issues.
First, you need to add the is assoc<chain>
trait to your sub.
But it still won't work, because there's a bug that needs to be fixed: Reduction with a function reference fails to honour chain
associativity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With