Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
reduce is also known fold, and foldr and foldl can be used to implement any of grep, map, join, sum, etc etc etc.

In Haskell Perl's join can be reimplemented as this:

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 (++)
And and similarly we can implement this with List::Util's reduce very elegantly 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
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.

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.

-nuffin
zz zZ Z Z #!perl

In reply to Re: RFC: Should join subsume reduce? by nothingmuch
in thread RFC: Should join subsume reduce? by Roy Johnson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-24 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found