It occurred to me today that join and List::Util reduce are not only similar functions, but they operate in separate problem spaces. The first argument to join is a string; the first argument to reduce is a coderef. So I thought that it would be a useful improvement to Perl to make join behave like reduce if you pass it a coderef.

Is there any reason that would be a bad idea? Is it an appealing idea? Worth submitting to P5P?

Update: Having received some input, I can flesh out the proposal a bit. I propose that there be a join BLOCK LIST form that would be easily distinguishable from the join LIST form with no possibility of affecting existing code.

The proposal to add reduce as a built-in has already been rejected by P5P on the grounds that it could break existing code. My proposal would not do so. So while reduce is a better name, going that route is a non-starter. My proposal is a zero-impact alternative.

The argument that it's a completely unrelated (or too dissimilar) to the old join has some merit, but it's a matter of perspective. There's a string-join and a functional-join. Both put the separator between all the elements of the list. String-join concatenates the whole mess; functional-join effectively evaluates it as a big expression (where the BLOCK acts essentially like an infix operator). Considering that string-join is a special case of reduce, it is not too much of a stretch (IMO) to have a specific and a general form with the same name.


Caution: Contents may have been coded under pressure.

In reply to 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.