Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^8: Is there any difference between prototype % and @ ?

by AnomalousMonk (Canon)
on Feb 23, 2013 at 15:13 UTC ( #1020315=note: print w/replies, xml ) Need Help??

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 (Pope) on Feb 23, 2013 at 15:20 UTC

    You're right! It's a left-over from starting with a copy of hgrep where I needed to keep a copy of the pair in order to return it conditionally.

    Thanks for pointing out the simplification.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1020315]
[shmem]: well finally.
[stevieb]: nice! I just finished a GPS "take me home" device last week, and did a bunch of software updates to it yesterday. I also created a pseudo chip with an Arduino to simulate an IC, where it responds to register read/writes over the I2C bus...
[stevieb]: ...from an I2C master. It's ugly and there are many changes I'm going to make, but I had not done anything like it before. It's designed for my RPi:: automated test platform; a system that does CI on *all* my RPi modules.
[shmem]: pseudo chip?

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-06-25 22:38 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.