note
nothingmuch
reduce is also known fold, and foldr and foldl can be used to implement any of grep, map, join, sum, etc etc etc.
<p>
In Haskell Perl's join can be reimplemented as this:
<c>
join _ [] = [] -- this means that join on the empty list is the empty string
join delim strings = foldl1 (\left right -> left ++ delim ++ right ) strings -- this is join implemented with reduce
-- also this could be written as
join delim strings = foldl1 ((++) . (++ delim)) strings
-- or more perlishly
join delim strings = foldl1 (\left right -> concat [left, delim, right]) strings
-- or with autocurrying fun
join = foldl1 . ((++) .) . flip (++)
</c>
And and similarly we can implement this with List::Util's reduce very elegantly too:
<c>
sub join {
my ( $delim, @strings ) = @_;
reduce { $a . $delim . $b } @strings;
}
but in this case the concatenation operator is not used directly as the a curried higher order function
</c>
[http://www.cs.nott.ac.uk/~gmh/fold.pdf|A tutorial on the universality and expressiveness of fold] is a wonderful article on this topic. There are some [http://cale.yi.org/autoshare/folds.png|diagrams] to assist you in understanding the article, too.
<p>
Since you seemed to like to reference c2.com, see [c2://The Wheel Gets Reinvented].
<p>
Lastly, the argument that adding reduce to Perl will break code is wrong - <c>err</c>, <c>lock</c> and others were added post factum as weak keywords - keywords that are only available if there is no sub by that name in the current package already.
<p>
<div class="pmsig"><div class="pmsig-205152">
-nuffin<br>zz zZ Z Z #!perl
</div></div>
531739
531739