Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduction operator using user-defined function error

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?

like image 836
lisprogtor Avatar asked Dec 19 '20 20:12

lisprogtor


1 Answers

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.

like image 100
raiph Avatar answered Sep 26 '22 02:09

raiph