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

Re^2: small steps toward Perl literacy, temp vars and parentheses

by BlaisePascal (Monk)
on Jun 16, 2004 at 21:54 UTC ( #367449=note: print w/replies, xml ) Need Help??

in reply to Re: small steps toward Perl literacy, temp vars and parentheses
in thread small steps toward Perl literacy, temp vars and parentheses

I don't believe the OP's issue came from reading chained operations, but more from the switch between left-chaining and right-chaining in the revised code.

His one-liner:

%h = ( map { $_, $h{$_} } sort { $a<=>$b } keys %h)[0,1];

was structured like:

 %h = ( op3 op2 op1 %h) op4;

where %h was acted upon by four operators in turn (keys, sort, map, and an array slice). But the order of operation was not linear. It flowed to the left, then sharply cut back to the right. In pseudo-scheme, that would have been:

(set! 'h (hash-new (array-slice (1 2) (flatten-map (lambda (x) (list x (hash-get h x))) (sort stringcomp (hash-keys h))))))
and the dataflow would have been clear. A while ago on the Perl 6 language lists there was discussion of 'gozinta' operators which would have allowed you to adjust the order of things. I don't remember the exact syntax right now (and it may have been squashed, I forget), but his code could have been done something like so:
%h ==> keys ==> sort { $a<==>$b } ==> map { $_, %h{$_} } ==> [0,1] ==> + %h;
%h <== [0,1] <== map { $_, %h{$_} } <== sort { $a<==>$b } <== keys <== + %h;
Either way would have worked, and the dataflow is obvious without having to switch left/right more often.

Replies are listed 'Best First'.
Re^3: small steps toward Perl literacy, temp vars and parentheses
by Jenda (Abbot) on Jun 16, 2004 at 22:18 UTC

    If we wanted a statement that could be read from right to left completely we could use

    %h = ( $_, $h{$_}) for ($_) = sort {$a <=> $b} keys %h;
    "Take the keys of %h, sort them numericaly, take the first one and set the %h hash to the key and its value."

    But I think this one is even more confusing. Maybe this

    %h = ( $_, $h{$_}) for ($_,) = sort {$a <=> $b} keys %h;
    would be better, the comma giving a hint that we do expect the sort to return several values, but we use just the first one.

    Anyway in this case I would use some temp variable. Even though I did work with some functional languages and liked it :-)

    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      I would definitely use for my ($min_key) = there; using a for that modifies $_ beyond the scope of the for itself is just begging to mess someone else up.
Re^3: small steps toward Perl literacy, temp vars and parentheses
by dragonchild (Archbishop) on Jun 17, 2004 at 03:07 UTC
    Yes, Perl6 will let you say which direction you want the result to go. So, you can have your chains read left-to-right or right-to-left, at your discretion.

    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2023-05-28 02:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found