Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by BrowserUk (Pope)
on Feb 23, 2013 at 07:03 UTC ( #1020281=note: print w/ replies, xml ) Need Help??


in reply to Re^6: Is there any difference between prototype % and @ ?
in thread Is there any difference between prototype % and @ ?

You might remember that map, sort and grep can be chained and they always accept and return lists...

Hm. Passing a listified hash (mixed keys and values in random order) to any of those constructs is a completely pointless exercise.

That is after all, why you are having to define an hgrep yourself.

Better methinks to pass hgrep and hmap a reference to the hash, call the callback with pairs, and have them return a list.

At least then they serve a purpose:

#! perl -slw use strict; use Data::Dump qw[ pp ]; sub hgrep (&\%) { my( $code, $href ) = @_; map{ local @_; $code->( @_ = each %$href ) ? @_ : () } 1 .. keys %$href; } sub hmap (&\%) { my( $code, $href ) = @_; map{ local @_; $code->( @_ = each %$href ) } 1 .. keys %$href; } my %h = 'a'..'z'; print join ' ', hgrep{ $_[0] =~ /[a-m]/ } %h; print join ' ', hmap{ "$_[0] => $_[1]" } %h; my %orig = ( cat => 22, dog => 23, category => 66, catalyst => 77, cataclysm => 88, dogma => 89, dogstar => 92, ); my %h2 = hgrep{ $_[0] =~ /^cat/ } %orig; pp\%h2; __END__ C:\test>hfp e f a b m n c d k l g h i j 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 => 22, cataclysm => 88, catalyst => 77, category => 66 }

hsort is left as an exercise.


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.


Comment on Re^7: Is there any difference between prototype % and @ ?
Download Code
Re^8: Is there any difference between prototype % and @ ?
by LanX (Canon) on Feb 23, 2013 at 14:08 UTC
    > > It's meant to be compatible with the standard interface!

    This can't be chained at least not easily w/o more transformations in the middle.

    your input is a hashref your output a list ...

    and your code is quite obfuscated...

    ... though ++ for creative use of each within map.

    Cheers Rolf

      This can't be chained at least not easily w/o more transformations in the middle.

      Show me an example of one of these mythical chains over a listified hash? (One that makes some sense!)

      and your code is quite obfuscated...

      Hm. You find that "obfuscated"? I always rated you as one of the more adept perlers.

      Besides, isn't a big part of the purpose of subroutines to hide the "difficult stuff", in order to simplify the code that calls it?

      though ++ for creative use of each within map.

      Thanks. The same method works with hsort to make sorting hashes a breeze:

      sub hsort (&\%) { my( $code, $href ) = @_; sort( $code map[ each %$href ], 1 .. keys %$href ); } my %orig = ( cat => 22, dog => 23, category => 66, catalyst => 77, cataclysm => 88, dogma => 89, dogstar => 92, ); ## Oh look! A chain :) print 'sorted by key: ', map "$_->[0]=>$_->[1] ", hsort{ $a->[0] cmp $ +b->[0] } %orig; print 'sorted by val: ', map "$_->[0]=>$_->[1] ", hsort{ $a->[1] <=> $ +b->[1] } %orig; C:\test>hFP.pl sorted by key: cat=>22 cataclysm=>88 catalyst=>77 category=>66 dog=>23 + dogma=>89 dogstar=>92 sorted by val: cat=>22 dog=>23 category=>66 catalyst=>77 cataclysm=>88 + dogma=>89 dogstar=>92

      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.
        > Show me an example of one of these mythical chains over a listified hash?

        it's a common idiom to use map to create listified hashes map { $_ => 0 } LIST

        > (One that makes some sense!)

        orthogonality always makes sense!

        (at least in the long run ... have a look into the python world)

        > Hm. You find that "obfuscated"?

        local @_ ? are you serious?

        > I always rated you as one of the more adept perlers.

        can we have a technical discussion w/o talking about persons?

        And cause you rated me, you'll maybe believe if I say that I already spend hours and days meditating about the problem to distinguish between arguments which are given as a plain list and a "list sigil" like %H or @A.

        Prototypes are just not mature enough!!!

        The optimal solution would ideally do lazy calculations. I did a talk about such constructs in Riga and GPW. But this is too much of a thread drift now.

        Cheers Rolf

Re^8: Is there any difference between prototype % and @ ?
by AnomalousMonk (Abbot) on Feb 23, 2013 at 15:13 UTC

    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, }

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-09-20 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (152 votes), past polls