Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re (tilly) 1: (Golf) Building a Better Binary Tree

by jynx (Priest)
on Oct 09, 2001 at 21:42 UTC ( [id://117810]=note: print w/replies, xml ) Need Help??


in reply to Re (tilly) 1: (Golf) Building a Better Binary Tree
in thread (Golf) Building a Better Binary Tree


A question,

You can eliminate calling $s as a function (and the argument passing) and it will still work for the test cases i've been using. Is there a reason not to do that? That is, is there a reason to use the full function call when passing the function ref will work?

If you pulled that out, you would get:

#23456789012345678901234567890123456789012345 $x=0;$s=shift;$x={$_,[$x,0]}for sort$s @_;%$x
at 45 characters.

jynx

update: alas, this is a false optimization. Thank you tilly for correcting my eyesight :)

Replies are listed 'Best First'.
Re (tilly) 3: (Golf) Building a Better Binary Tree
by tilly (Archbishop) on Oct 09, 2001 at 22:27 UTC
    I haven't tested under Perl 5.6, but under 5.005 your code does not work.

    Remember that the comparison function specified depends on the arguments passed to it, and not on $a and $b. Therefore the comparison function is not a valid sort sub and you need to do that transform.

    Try this to see what I mean.

    print Dumper h(sub {$_[0] <=> $_[1]}, 1..6, -3);
    With your code that fails. (BTW your other solution also seems to have used this incorrect optimization.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-29 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found