Syntactic Confectionery Delight PerlMonks

### comment on

 Need Help??

I'm not sure why I used splice where I could have used unshift. Probably for the same reason you used it instead of push — too fixated on splicing. :-)

You misunderstood the purpose of the padding in my code, btw. Remember that I splice before invoking the callback — which means that without padding, I'd be throwing away the values that should have been processed in the first iteration. I'm unsure what kind of pretzel logic led me to that solution, I should simply have used the list returned from splice.

```sub mapn (&@) {
my ( \$callback, \$n ) = splice @_, 0, 2;
map \$callback->( splice @_, 0, \$n ), 0 .. @_ / \$n -1;
}

One thing about your solution that irks me since my days as an assembler programmer is multiplying a constant with a counter in a loop. You get the same effect if you simply iteratively add the constant to a counter. Of course, it doesn't make a particular difference in efficiency in Perl, but I find that it can still improve clarity and reduce repetition to do it that way.

```sub mapnz (&@) {
my ( \$callback, \$n ) = splice @_, 0, 2;
my ( \$i, \$j ) = ( 0 ) x 2;
map {
\$i = \$j;
\$callback->( @_[ \$i .. ( \$j += \$n ) - 1 ] );
} 0 .. @_ / \$n - 1;
}

As well, the non-padding case can be dealth with less cumbersomely than my and BrowserUk's previous attempts do, which rely on that really ugly ternary.

The splicing flavour (see 2nd update):

```sub mapn (&@) {
my ( \$callback, \$n ) = splice @_, 0, 2;
map {
\$n = @_ if \$n > @_;
\$callback->( splice @_, 0, \$n );
} 0 .. @_ / \$n - 1;
}

Analogously, the index-based flavour:

```sub mapn (&@) {
my ( \$callback, \$n ) = splice @_, 0, 2;
my ( \$i, \$j ) = ( 0 ) x 2;
map {
\$i = \$j;
\$n = @_ - \$i if \$i + \$n > @_;
\$callback->( @_[ \$i .. ( \$j += \$n ) - 1 ] );
} 0 .. @_ / \$n - 1;
}

I think I'd still prefer the splicing version.

Update: the index-based flavour had \$n = \$#_ - \$i if \$i + \$n > \$#_; which is an off-by-one error, since the range goes to ( \$j += \$n ) - 1. It has indeed got to be completely analogous to the splicing flavour, using @_ instead. Fixed.

Update: I am amused at myself. The "padding" splicing version actually didn't pad, because splice, of course, only returns as many elements as there are left, if you ask for more. The padding version turns out to be much simpler than the "non-padding" version I concocted before — no special cases at all!

```sub mapnz (&@) {
my ( \$callback, \$n ) = splice @_, 0, 2;
push @_, ( undef ) x ( -@_ % \$n );
map \$callback->( splice @_, 0, \$n ), 0 .. @_ / \$n - 1;
}

This is Perl as I love it. Beautiful and simple. :-)

Makeshifts last the longest.

In reply to Re^6: Fold a list using map splices by Aristotle
in thread Fold a list using map splices by hiseldl

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, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2021-04-15 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?