Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

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


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

So how do you chain your implementations of hgrep and hmap? hgrep {...} hmap {...} %h ???

You don't. It doesn't make sense to chain the output of a function producing a list to the input of a function taking a hash.

If you need to post-process the output from hmap with grep, use grep.

There is no way to have hmap return a hash, or a hash reference, because the user decides what is returned from the callback. And what tehy return may make no sense as a hash at all.

If you need to post process the output from hgrep with map; use map.

Whilst it would be possible to construct a new hash from those pairs for which the user callback returns true; if you do so, and the user wants to post process the output as a list with map; or print it as a list; or assign it to an array or hash; then they then have to deconstruct the hash(ref) to do so.

Ie. the h stands for head (of the chain) as well as hash.

I'm saying it's safer to respect symmetry because nobody can anticipate all use cases, not even you.

And symmetry for its own sake is just pretty, pretty. Orthogonality where it makes sense ... makes sense; but otherwise it does not. And so far; neither you nor I have come up with a use case. But we are both aware of the many use cases for the output being a list.

In an ideal world, it would be a list of pairs. But you cannot initialise another hash from a list of pairs; and nothing else in Perl knows what to do with them either. I guess that hgrep hmap could output a list of pairs, and accept a list of pairs; and then have another function that flattens them (for assignment to a new hash. But it adds a 'final' function to the chain which is a cost; and as yet I see no benefit accruing from it.

That not to say that there isn't some potential use for using context (eg. wantarray) to decide whether to produce a normal list or (say) a hashref. Given a use case.

But I can see no point in building a hash(ref) for output if the next function in the chain is going to immediately deconstruct it to a list or list of pairs.


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^13: Is there any difference between prototype % and @ ?
Download Code
Re^14: Is there any difference between prototype % and @ ?
by LanX (Canon) on Feb 24, 2013 at 03:34 UTC
    > And so far; neither you nor I have come up with a use case.

    trivial example

    %salary=( boss => 1_000, secretary => 300, admin => 600, janitor => 150, ); print map {"$_ earns $salary{$_} \n" } sort { $salary{$a} <=> $salary{$b} } grep { $salary{$_} > 500 } keys %salary;

    translating it to hmap, hsort and hgrep is left as an excercise.

    Cheers Rolf

      sub hpairs (\%) { my $href = shift; map[ each %$href ], 1 .. keys %$href; } my %salary = ( boss => 1_000, secretary => 300, admin => 600, janitor => 150, ); print for map { "$_->[0] earns $_->[1]" } sort { $a->[1] <=> $b->[1] } grep { $_->[1] > 500 } hpairs %salary; __END__ admin earns 600 boss earns 1000

      Or prettier:

      sub hpairs (\%) { use enum 'KEY', 'VAL'; my $href = shift; map[ each %$href ], 1 .. keys %$href; } my %salary = ( boss => 1_000, secretary => 300, admin => 600, janitor => 150, ); print for map { "$_->[KEY] earns $_->[VAL]" } sort { $a->[VAL] <=> $b->[VAL] } grep { $_->[VAL] > 500 } hpairs %salary;

      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.
        nice, but

        1. doesn't even try to use the hmap or hgrep you defended for so long
        2. you needed to introduce a completely new function hpairs
        3. still needs a unhpairs to flatten LoA before assigning after chain to a new hash
        4. my hmap could already be used instead of your hpairs (and unhpairs) in this approach ...

        Let's count, you're going to introduce 5 new functions where I only need 3 to cover all discussed cases...

        You really hate symmetry, don't you?

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-07-25 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls