Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Gratuitous use of Perl Prototypes

by BrowserUk (Pope)
on Nov 09, 2004 at 15:42 UTC ( #406360=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Gratuitous use of Perl Prototypes
in thread Gratuitous use of Perl Prototypes

I'd argue that zip() isn't that readable to begin with! *grins*

I guess I could rename it '' :)

Or maybe you'd prefer map_one_element_from_each_of_two_arrays_at_a_time()?

Or should that be map_one_element_at_a_time_from_each_of_two_arrays()?

Plus, can't you use some variant of reduce() or mapcar() to achieve the same thing?

Not that I am aware off. Anoyone know a better implementation?

Also, isn't the subref prototype prone to memory leaks?

Again. Not that I am aware of. Maybe the problem lies in the module?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon


Comment on Re^3: Gratuitous use of Perl Prototypes
Select or Download Code
Re^4: Gratuitous use of Perl Prototypes
by dragonchild (Archbishop) on Nov 09, 2004 at 16:08 UTC
    *snickers* It wasn't the name I was complaining about - it was your complete lack of variable names. :-)

    And, now that I'm thinking about it, your version isn't prone to memory leaks because you don't have any lexical variables (which may be a good reason for writing it the way you did). It was the use of lexicals that, potentially, could create memory leaks in Error.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Ah! I see what you mean. (I should have realised that, given you were advocating mapcar() (What the...?) :).

      The main reason for my avoiding lexicals in a utility sub of this type is that I want to retain the aliasing nature of @_. If you look closely at the output from the second and third print statements, you'll see that they have side-effects. (Which will probably be considered as undesirable, but again, it is sometimes useful.)

      Perhaps these are more palatable?

      #! perl -slw use strict; use List::Util qw[ min ]; local $, = ' | '; use constant { CODEBLOCK => 0, ARRAY_ONE => 1, ARRAY_TWO => 2, }; sub zip (&\@\@) { map { $_[CODEBLOCK]->( $_[ARRAY_ONE][ $_ ], $_[ARRAY_TWO][ $_ ] ) } 0 .. ( @{ $_[ARRAY_ONE]} < @{ $_[ARRAY_TWO] } ? $#{ $_[ARRAY_ONE] } : $#{ $_[ARRAY_TWO] } ); } my @x = 'a' .. 'z'; my @n = 1 .. 10; print zip{ "@_" } @x, @n; print zip{ $_[ 0 ] .= $_[ 1 ] } @x, @n; print zip{ $_[ 0 ] .= $_[ 1 ] } @x, @n; sub zipn (&\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) { map { my $i = $_; $_[CODEBLOCK]->( map{ $_[ $_ ][ $i ] } 1 .. $#_ ) } 0 .. min map{ $#{ $_ } } @_[ 1 .. $#_ ]; } my @a = split'','ARC'; my @b = 1 .. 3; my @c = split'','ADP'; my @d = ( 1, 2, 0 ); print "\n---\n"; print for zipn{ join '', @_ } @a, @b, @c, @d; __END__ [17:21:15.53] P:\test>406231 a 1 | b 2 | c 3 | d 4 | e 5 | f 6 | g 7 | h 8 | i 9 | j 10 a1 | b2 | c3 | d4 | e5 | f6 | g7 | h8 | i9 | j10 a11 | b22 | c33 | d44 | e55 | f66 | g77 | h88 | i99 | j1010 --- A1A1 R2D2 C3P0

      Now, if anyone can tell me how to make zipn truely generic in the number of arrays it can deal with I'd be very interested. 20 is probably more than enough for any purpose I am likely to encounter, but I hate arbitrary limits.

      Also, if anyone can see how to retain the alias status of the parameters to zipn, that would be really cool.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-09-01 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls