Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Fold a list using map splices

by BrowserUk (Patriarch)
on Feb 21, 2003 at 17:00 UTC ( [id://237490]=note: print w/replies, xml ) Need Help??


in reply to Fold a list using map splices

I've used the splice solution, but it offended me that you have to copy the array just to destroy it. I came up with a version of my mapn function that avoids the splice (and therefore the need to copy as well), by using slices and a pointer.

There are two versions of the function mapn and mapnz. With the former, number of values produced in the last row is just how ever many are left over.

With the latter, it pads (with nulls, hence the 'z') the last list of returned values to be the same size as the rest of the rows. This can be useful for things like tables, where although most browsers will tolorate there being less <td></td> pairs in the last row, they do not render this very well. By using mapnz in conjunction with an 8-char patch I made to CGI.pm so that empty TD's have a &nbsp; inserted, forces most browsers to render the borders around missing values.

#! perl -slw use strict; use CGI qw[td Tr :table caption]; sub mapn (&@) { my($c, $n, $s) = (shift, shift, 0); map{ $s+=$n; $c->( @_[($s-$n) .. ($s < @_ ? $s-1 : @_-1) ] ); } 0 .. (@_/$n); } sub mapnz (&@) { my($c, $n, $s) = (shift, shift, 0); map{ $s+=$n; $c->( @_[($s-$n) .. ($s - 1) ] ); } 0 .. (@_/$n); } my @tabular_data = map{ 1000+int rand 8999 } 1 .. 31; print table( {border=>1}, $/, mapn{ Tr( td( \@_ ) ).$/; } 5, @tabular_data ); print table( {border=>1}, $/, caption('Columns: ' . $_), $/, mapnz{ Tr( td( \@_ ) ).$/ } $_, @tabular_data ) for 3 .. 8; __END__ C:\test>236799 <table border="1"> <tr><td>4978</td> <td>7782</td> <td>3230</td> <td>3524</td> <td>7570< +/td></tr> <tr><td>9627</td> <td>4424</td> <td>7968</td> <td>2523</td> <td>1838< +/td></tr> <tr><td>9843</td> <td>8797</td> <td>5034</td> <td>8499</td> <td>3683< +/td></tr> <tr><td>5591</td> <td>2402</td> <td>6131</td> <td>2649</td> <td>5917< +/td></tr> <tr><td>1333</td> <td>7874</td> <td>7031</td> <td>2269</td> <td>3766< +/td></tr> <tr><td>2151</td> <td>1350</td> <td>7235</td> <td>5986</td> <td>1447< +/td></tr> <tr><td>2627</td></tr> </table> <table border="1"> <caption>Columns: 3</caption> <tr><td>4978</td> <td>7782</td> <td>3230</td></tr> <tr><td>3524</td> <td>7570</td> <td>9627</td></tr> <tr><td>4424</td> <td>7968</td> <td>2523</td></tr> <tr><td>1838</td> <td>9843</td> <td>8797</td></tr> <tr><td>5034</td> <td>8499</td> <td>3683</td></tr> <tr><td>5591</td> <td>2402</td> <td>6131</td></tr> <tr><td>2649</td> <td>5917</td> <td>1333</td></tr> <tr><td>7874</td> <td>7031</td> <td>2269</td></tr> <tr><td>3766</td> <td>2151</td> <td>1350</td></tr> <tr><td>7235</td> <td>5986</td> <td>1447</td></tr> <tr><td>2627</td> <td>&nbsp;</td> <td>&nbsp;</td></tr> </table>

Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clerk.

Replies are listed 'Best First'.
Re^2: Fold a list using map splices
by Aristotle (Chancellor) on Feb 22, 2003 at 00:03 UTC
    The index variables there kept tickling me as not right. This is Perl, not C, there has to be a way to do this without explicit indices and no copying. Guess what. :) It's actually simpler than I was expecting and definitely goes in my "cool snippets" file.
    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; }

    BrowserUk++ for the inspiration!

    Update: fixed mapnz

    Makeshifts last the longest.

      Nice twist++. Took me a while to work out why i doesn't destroy the input array. Not sure that I share your distaste for the index variable...'sides...mine's quicker:).

      You have a typo? ... an out-by-one error on your version of mapnz, it produces one too many rows. You either need to subtract 1 as you do in the mapn version or start your range from 1.


      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.

        I was pretty sure yours would be quicker - but by how much is that?

        Thanks for the heads up on the one-off error..

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://237490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-28 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found