Subject: | documentation issue with reduce_u |
Date: | Sun, 05 Jul 2020 08:30:54 +0000 |
To: | bug-List-MoreUtils [...] rt.cpan.org |
From: | Jörg Sommrey <jo [...] sommrey.de> |
There is an issue with the documentation of reduce_u:
reduce_u BLOCK LIST
Reduce LIST by calling BLOCK in scalar context for each element of LIST.
$a contains the progressional result and is initialized with 1. $b
contains the current processed element of LIST and $_ contains the index
of the element in $b.
This function has been added if one might need the extra of the index
value but need an individual initialization.
This is contradictory: "is initialized with 1" vs. "if one [...] need
an individual initialization"
and does not describe the actual behaviour.
Furthermore, the usage of this function is not as convenient as it
could be. An
example with List::Util:reduce and List::MoreUtils::reduce_u:
$s1 = do {my $i = 0; reduce {$a . substr $b, $i++, 1} '', qw(abc 123 xyz)};
$s2 = reduce_u {($a // '') . substr $b, $_, 1} qw(abc 123 xyz);
Both need an extra effort to get an individual identity AND indexing.
My expectation for
reduce_u was like this:
sub reduce_i (&@) {
my $code = shift;
my $i = 0;
local $_;
reduce {$_ = $i++; $code->()} @_;
}
$s3 = reduce_i {$a . substr $b, $_, 1} '', qw(abc 123 xyz);
This would fit into the _u family of functions as it returns undef
when called with less than 2 elements.
Best regards,
-jo