Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by hardburn (Abbot)
on Jun 16, 2004 at 21:17 UTC ( [id://367433]=note: print w/replies, xml ) Need Help??


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

In the second bit of code there is a lot of contextual info to tell one to jump to

I've learned to stop worrying about this and enjoy writing LISP in Perl :)

Removing my tounge from my cheek, I think people shouldn't expect Perl to just be interpreted C. People who have a strong C background may have problems reading chained operations like that, but it's standard operating procedure in LISP and other functional languages. So when somebody tells you that the code you shown above is sloppy, you'll know which community they come from.

Now, I think you are correct to say that the index isn't a very pleasing way to end the statement. Maybe this will do it (untested):

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

Of course, now you have to explain to The Complainer the difference between lists and arrays.

----
send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Replies are listed 'Best First'.
Re^2: small steps toward Perl literacy, temp vars and parentheses
by BlaisePascal (Monk) on Jun 16, 2004 at 21:54 UTC
    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;
    or...
    %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.

      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 :-)

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

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-20 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found