Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: More functional programming utilities

by BrowserUk (Pope)
on Jun 08, 2005 at 08:39 UTC ( #464588=note: print w/replies, xml ) Need Help??


in reply to More functional programming utilities

Here's your multimap BLOCK LIST, LIST... (with a practical limitation).

#! perl -slw use strict; sub multimap (&\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) { my $code = shift; map { my $i = $_; $code->( map{ $_->[ $i ] } @_ ) } 0 .. $#{ $_[ 0 ] }; } my @l = 'a' .. 'z'; my @u = 'A' .. 'Z'; my @n = 1 .. 26; print for multimap { join ', ', @_ } @u, @n, @l; __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 I, 9, i J, 10, j K, 11, k L, 12, l M, 13, m N, 14, n O, 15, o P, 16, p Q, 17, q R, 18, r S, 19, s T, 20, t U, 21, u V, 22, v W, 23, w X, 24, x Y, 25, y Z, 26, z

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Replies are listed 'Best First'.
Re^2: More functional programming utilities
by ivancho (Hermit) on Jun 08, 2005 at 12:07 UTC
    I must admit, I frowned at your prototype.. but then i checked, and I found out that the author of List::MoreUtils has implemented a couple of his functions in exactly the same way - though his prototypes are with 25 arrays.. I find it mildly disturbing that he did not bother with documenting this...

      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', ], ]

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        This is nice, ++ to follow

        first, a suggestion - why don't you put [] around your end result map, so we can easily chain multimaps.
        there's actually a lot to be said on the topic - for example, 'multimap' may not be the best name for that function.. while 'map' is List -> List ( with proviso on iterators here ), and 'reduce' is List -> Scalar, this new thing is List x List -> List.. It agrees with 'map' on the end List, but it actually provides the dimensionality reduction of 'reduce'..

        now, of course, why stop here - one can imagine some sort of morphing operator multireduce(n,m) ListOfBlocks, AoAoA..A that colapses N-dim structures to M-dim ones..

        but this goes into academic grounds - I can't imagine this being useful, or easily mastered thing..

        lastly, what is Data::Dumper::SLC? I can't find it anywhere.. personal extension ?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2019-12-06 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (156 votes). Check out past polls.

    Notices?