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

Re (tilly) 1: Closest-value-in-list Golf!

by tilly (Archbishop)
on May 29, 2001 at 18:40 UTC ( #83951=note: print w/ replies, xml ) Need Help??


in reply to Closest-value-in-list Golf!

First of all I don't think that strict is a very useful restriction in golf. For instance a useful trick is to use punctuation variables like $, because Perl parses $,for differently than $_for. But using $, is strict-compliant in letter even though it definitely is not in spirit!

That said the natural approach at 46 letters happens to be strict-compliant:

sub g{ (sort{abs($a-$_[1])<=>abs$b-$_[1]}@{$_[0]})[0] }


Comment on Re (tilly) 1: Closest-value-in-list Golf!
Download Code
uneven abs!
by larryk (Friar) on May 29, 2001 at 18:52 UTC
    how come you use ( ) on one abs but not the other? :-)
    sub g {(sort{abs$a-$_[1]<=>abs$b-$_[1]}@{$_[0]})[0]} # 44 chars
    Update:
    pop and no ref...
    sub g {$n=pop;(sort{abs$a-$n<=>abs$b-$n}@_)[0]} # 39 chars
    fantastic - that's exactly half my original

    "Argument is futile - you will be ignorralated!"

      For future reference, I found your spec unclear. In the end I settled on "2 parameters" as being the clearest statement. But it would help in the future if you gave sample test code using the function. Then there can be no doubt.

      However if I am allowed to rearrange the arguments, I find it more natural that any special arguments should come first in the list. Giving a 38 character solution:

      sub g { (sort{abs$a-$_[0]<=>abs$b-$_[0]}@_)[1] }
        Apologies for my ambiguous description. Please take a look at the update I have made and tell me if I make any more sense.

        ps. 38 chars - good job!

        "Argument is futile - you will be ignorralated!"

Re: Re (tilly) 1: Closest-value-in-list Golf!
by suaveant (Parson) on May 29, 2001 at 19:17 UTC
    Update my bad... sorry, missed the or listref bit of golf, my response here was useless
                    - Ant

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-07-28 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (199 votes), past polls