Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: More functional programming utilities

by ivancho (Hermit)
on Jun 08, 2005 at 12:07 UTC ( [id://464647]=note: print w/replies, xml ) Need Help??


in reply to Re: More functional programming utilities
in thread More functional programming utilities

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...
  • Comment on Re^2: More functional programming utilities

Replies are listed 'Best First'.
Re^3: More functional programming utilities
by BrowserUk (Patriarch) on Jun 08, 2005 at 13:01 UTC

    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 ?
        first, a suggestion - why don't you put around your end result map, so we can easily chain multimaps.

        In part because I didn't think of doing that. But now you've made me think about it, I think that I would leave it as it is. Adding it internally would make the user that wants the the list rather than a LoL have to wrap the call in @{ multimap [ ... ] } to get at it.

        As is, to get the LoL, you do [ multimap [ ...] ].

        In the former I would be creating a list only for the user to discard it. In the latter, he creates it if he needs it. Which I think is a good reason to leave it as is?

        'multimap' may not be the best name for that function..

        I just went with the OPs name, but I agree that it may not best capture the semantics. I'd favour something shorter. 'map' is well understood in Perl circles so I think that should be in there somewhere.

        • 'mmap' -- sounds like a memory thing.
        • 'mapn' -- I have one called that that passes n elements at a time from a single list
        • 'maps' -- Hmm.Not quite.
        • 'mapreduce' -- A Southern Mother's ill-feelings?
        • Shame we can't use single quotes in function names like Haskell -- map'm -- Too cute?
        • 'mapm'?
        • 'mapall'?
        • 'decons' or should that be 'discons'?
        • 'heads' -- Kinda fits, but ...?
        What is Data::Dumper::SLC?

        It's my Just_a_Dumper dumper. No attempt at making the output evallable. No attempt to fold self-referrencial structures (though it does economically detect and flag them). It streams on-the-fly (avoiding accumlating anything in memory) to STDOUT (or an option filehandle) and wraps at 80 or a specifyable limit. It writes as compact a representation as is comensurate with being readable.

        When I asked here if anyone knew of such an animal, the basic respose was that nobody except myself ever needed such a thing, so I never got around to documenting or packaging it.


        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found