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.
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
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