note
Aristotle
The index variables there kept tickling me as not right. This is Perl, not C, there <em>has</em> to be a way to do this without explicit indices and no copying. Guess what. <tt>:)</tt> It's actually simpler than I was expecting and definitely goes in my "cool snippets" file.
<code>
sub mapn (&@) {
my ($cb, $n) = splice @_, 0, 2;
splice @_, 0, 0, (undef)x$n;
map{
splice @_, 0, $n; $cb->(@_[0 .. (@_ < $n ? $#_ : $n-1)]);
} 0 .. (@_/$n)-1;
}
sub mapnz (&@) {
my ($cb, $n) = splice @_, 0, 2;
splice @_, 0, 0, (undef)x$n;
map{
splice @_, 0, $n; $cb->(@_[0 .. $n-1]);
} 0 .. (@_/$n)-1;
}
</code>
<p>[BrowserUk]++ for the inspiration!</p>
<p><b>Update:</b> fixed <tt>mapnz</tt></p>
<p align="right"><em>Makeshifts last the longest.</em></p>
236799
237490