Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raku (née Perl 6) reduce function and reduction metaoperator give different results

Tags:

reduction

raku

my @s=<1 2 3 2 3 4>;
say reduce {$^a < $^b}, @s;
say [<] @s;
# --------
# True
# False

My question is two fold: Firstly, why does the reduction metaoperator processes the < operator differently? It looks like the reduction metaop is estimatedly using a variable which, at the first change of true to false, retains that change because:

say [\<] @s;
# ----------
# (True True True False False False)

Secondly, I'd like to use this inside the reduce function, too, i.e. introducing some code inside the curly brackets of reduce function so that it gives the same result as the reduction meta operator. How can I do it? Thank you.

like image 332
Terry Avatar asked Oct 09 '19 10:10

Terry


1 Answers

Both the meta-operator and reduce take into account the associativity of the operator passed to them; in the case of <, the operator is chaining.

When passing a custom block, reduce cannot see inside of it, and so it falls back to the default of left-associative; it then compares the later values against the boolean returned from the reducer, which numifies to 0 or 1, which is why the result ends up as True in the end.

You can get the semantics you desire by declaring your reduction function as having chaining associativity:

my @s1 =<1 2 3 2 3 4>;
my @s2 =<1 2 3 4>;
sub my-reducer($a, $b) is assoc('chain') {
    $a < $b
}
say reduce &my-reducer, @s1;    # False
say reduce &my-reducer, @s2;    # True
like image 76
Jonathan Worthington Avatar answered Oct 21 '22 17:10

Jonathan Worthington