in reply to Golf: overtone calculator

Here's my retooling of BrowserUk/Arien's solution, which falls one character shy of matching:
sort{$a-$b}keys%{{map{map{1e-3*int"@_"*1e3*$x/$_,0}1..($x=$_)}1..pop}}
It had promise, I swear! I was sure that the old "keys on temporary hash" trick would work, but alas, denied. I futzed with the clever sprintf() replacement, and could not find any slack. Very elegant.

By the way, I just have to interject that Perl 5 prototypes are evil. I'd suggest not using them.

Update: Added missing bracket per Django's catch.

Replies are listed 'Best First'.
Re: Re: Golf: overtone calculator
by locked_user mtve (Deacon) on Sep 03, 2002 at 13:15 UTC

    Little optimization:

    sort{$a-$b}keys%{{map{map{1e-3*int"@_.e3"*$'/$_,0}/^/..$_}1..pop}}
Re: Re: Golf: overtone calculator
by Django (Pilgrim) on Sep 02, 2002 at 10:32 UTC

    There's a closing curly missing at the end. Apart from that: elegant indeed!

    Why do you consider Perl 5 prototypes as evil? I like the ability to use subs like built-ins.

    ~Django
    "Why don't we ever challenge the spherical earth theory?"

      They are poison. You should only use them when you have to, like forcing an array or subroutine reference. Here's two typical examples:
      sub my_grep(&) { ... } sub my_pop(\@) { ... }
      Putting in scalars (i.e. ($$)) is asking for trouble since it converts any arrays to scalars automatically. I found this really quite worrying, and you can see my brief rant in Function Prototypes and Array vs. List (Pt. 2).

      This all comes from a technique of jamming arrays into functions, which I do all the time to be efficient. Things like this:
      my $sth = $dbh->prepare("SELECT id,name,age FROM foo"); $sth->execute(); while (my $row = $sth->fetchrow()) { $self->some_function(@$row); }
      Now, if you've prototyped your function, you're going to get the number 3 every time. This is put in to the 'id' field, so you might actually think it's valid data, too.

      Instead, just leave those things off. That's why merlyn says "...and this is why we tell people DO NOT USE PROTOTYPES".

        Nice rant, tadman; well-explained... except for one minor point: in your example, the prototype doesn't apply, because the function is being called as a method... I think. I remember reading that, somewhere, but rereading the relevant part of perlsub it only mentions functions called with a & or via a subref.


        Confession: It does an Immortal Body good.