in reply to Re^7: Is there any difference between prototype % and @ ?
in thread Is there any difference between prototype % and @ ?
BrowserUk: Your implementation of hmap and hgrep is a little tricky, but I certainly wouldn't describe it as 'quite obfuscated'! However...
sub hmap (&\%) { my( $code, $href ) = @_; map{ local @_; $code->( @_ = each %$href ) } 1 .. keys %$href; }
In the code above, I don't see the point of local-izing @_ and then assigning each to it. Passing the return of each %$href 'directly' to the coderef seems to work just as well.
>perl -wMstrict -MData::Dump -le "sub hmap (&\%) { my( $code, $href ) = @_; map{ $code->(each %$href) } 1 .. keys %$href; } ;; my %h = 'a'..'z'; ;; print join ' ', hmap { qq{$_[0] => $_[1]} } %h; ;; my %orig = ( cat => 22, dog => 23, category => 66, catalyst => 77, cataclysm => 88, dogma => 89, dogstar => 92, ); my %h2 = hmap { $_[0], $_[1] + $_[1] } %orig; dd \%h2; " w => x e => f a => b m => n s => t y => z u => v c => d k => l q => r +g => h i => j o => p { cat => 44, cataclysm => 176, catalyst => 154, category => 132, dog => 46, dogma => 178, dogstar => 184, }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^9: Is there any difference between prototype % and @ ?
by BrowserUk (Patriarch) on Feb 23, 2013 at 15:20 UTC |