Prototypes in perl5 have gotten a bit of a bad reputation, some of which is deserved. If people ask me why we should use them at all, however, I do have an example that always respond with map2, which is like the built-in map except that it accepts two arrays as arguments (instead of a single list).
=item my @result = map2 BLOCK ARRAY, ARRAY; Similar to c<map>, but operate on two lists at once. Inside the BLOCK, the value are passed in as C<$a> and C<$b> (as in C<sort>). my @a = ( 1, 2, 5, 7, ); my @b = ( 3, 4, 6, 8 ); my @sums = map2 { $a+$b } @a, @b; my @prods = map2 { $a*$b } @a, @b; One use is to initialize hashes from two arrays. You can do this in straight perl with: my %hash; @hash{@hash_keys} = @values; With this subroutine, you could also do: my %hash = map2 { ( $a => $b ) } @hash_keys, @values; If the arrays are not the same length, the shorter array is used to limit how far we go in each array. BLOCK is evaluated in list context. =cut sub map2 ( & \@ \@ ) { my ( $code_ref, $a_ref, $b_ref ) = @_; my $rv_len = ( @$a_ref < @$b_ref ) ? @$a_ref : @$b_ref; my @rv; for ( my $i = 0; $i < $rv_len; ++$i ) { local ( $a, $b ) = ( $a_ref->[$i], $b_ref->[$i] ); push @rv, $code_ref->(); } return @rv; }
I know I saw something like this from Abigail (or is that Abigail-II?) in the late '90s, but I have since reimplemented it a few times. As I said, it serves as a handy example to demonstrate how prototypes make some things possible that would otherwise be difficult to impossible to code.
Update (2004-05-02 00:15 -0700): Incorporated some suggestions from Zaxo to fix some misleading variable names. Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A classic use of prototypes: map2
by Zaxo (Archbishop) on May 02, 2004 at 05:47 UTC | |
by tkil (Monk) on May 02, 2004 at 07:04 UTC | |
by Zaxo (Archbishop) on May 02, 2004 at 07:10 UTC | |
by tye (Sage) on May 02, 2004 at 15:27 UTC | |
|
Re: A classic use of prototypes: map2
by Errto (Vicar) on May 02, 2004 at 18:54 UTC | |
by tkil (Monk) on May 02, 2004 at 20:41 UTC |