in reply to Re^2: More functional programming utilities
in thread More functional programming utilities
I don't like the prototype in this case either, it heralds too many limitations in use. The practical ones of artificially limiting the number of arrays it can operate on, but more fundementally, the fact that you can only use real arrays.
With the caveat of requiring the user to pass an AoA ref, the function becomes much more flexible and makes writing transpose LOL trivial:
#! perl -slw use strict; use Data::Dumper::SLC; sub multimap (&$) { my( $code, $aref ) = @_; return unless ref $aref eq 'ARRAY'; map { my $i = $_; $code->( map{ $_->[ $i ] } @$aref ) } 0 .. $#{ $aref->[ 0 ] }; } my @l = 'a' .. 'h'; my @u = 'A' .. 'H'; my @n = 1 .. 8; print for multimap { join ', ', @_ } [ \( @u, @n, @l ) ]; sub transpose { return multimap{ [ @_ ] } $_[ 0 ]; } my @LoL = ([1,2,3],[2,3,5]); my @transposed = transpose \@LoL; Dump \@transposed; __END__ P:\test>464573 A, 1, a B, 2, b C, 3, c D, 4, d E, 5, e F, 6, f G, 7, g H, 8, h [ [ '1', '2', ], [ '2', '3', ], [ '3', '5', ], ]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: More functional programming utilities
by ivancho (Hermit) on Jun 09, 2005 at 02:24 UTC | |
by BrowserUk (Patriarch) on Jun 09, 2005 at 03:31 UTC | |
by kaif (Friar) on Jun 09, 2005 at 03:46 UTC |