in reply to RFC: Should join subsume reduce?
In Haskell Perl's join can be reimplemented as this:
And and similarly we can implement this with List::Util's reduce very elegantly too:join _ [] = [] -- this means that join on the empty list is the empty +string join delim strings = foldl1 (\left right -> left ++ delim ++ right ) s +trings -- 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 (++)
A tutorial on the universality and expressiveness of fold is a wonderful article on this topic. There are some diagrams to assist you in understanding the article, too.sub join { my ( $delim, @strings ) = @_; reduce { $a . $delim . $b } @strings; } but in this case the concatenation operator is not used directly as th +e a curried higher order function
Since you seemed to like to reference c2.com, see The Wheel Gets Reinvented.
Lastly, the argument that adding reduce to Perl will break code is wrong - err, lock 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.
|
|---|