Click here to get back home

advice on good perl idiom

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
advice on good perl idiom Mike Hunter 06-03-2008
Posted by Mirco Wahab on June 3, 2008, 12:55 pm
Please log in for more thread options
Mike Hunter wrote:
> Recently I was coding and wanted to see which of 4 strings had the
> largest result when passed to a function. This is what I ened up
> writing:
> my @roundArr;
> push @roundArr, [$A, check($goodPrefix.$A)];
> push @roundArr, [$C, check($goodPrefix.$C)];
> push @roundArr, [$T, check($goodPrefix.$T)];
> push @roundArr, [$G, check($goodPrefix.$G)];
>
> @roundArr = sort {$b->[1] <=> $a->[1]} @roundArr;
> print $roundArr[0]->[0]." wins!\n";
> The code works fine, but I can't help but think there's a better idiom
> for what I'm trying to do. I get a little bit of a creepy feeling by
> using an array, I feel like I should be doing something with a hash. I
> guess there's no way of getting around having to associate the given
> check() result with the particular input.

This can be expressed as a "Schwartzian Transform":

...

print +(
map $_->[0],
sort {$b->[1] <=> $a->[1]}
map [$_, check($goodPrefix.$_)],
$A,$C,$T,$G
)[0] . " wins!\n"


...


Regards

Mirco

Posted by Willem on June 4, 2008, 2:01 am
Please log in for more thread options
Mike wrote:
) Hey folks,
)
) Recently I was coding and wanted to see which of 4 strings had the
) largest result when passed to a function. This is what I ened up
) writing:
)
) while (...)
) {
)
) my @roundArr;
) push @roundArr, [$A, check($goodPrefix.$A)];
) push @roundArr, [$C, check($goodPrefix.$C)];
) push @roundArr, [$T, check($goodPrefix.$T)];
) push @roundArr, [$G, check($goodPrefix.$G)];
)
) @roundArr = sort {$b->[1] <=> $a->[1]} @roundArr;
) print $roundArr[0]->[0]." wins!\n";
)
) }
)
) The code works fine, but I can't help but think there's a better idiom
) for what I'm trying to do. I get a little bit of a creepy feeling by
) using an array, I feel like I should be doing something with a hash. I
) guess there's no way of getting around having to associate the given
) check() result with the particular input.

You only want the best result, right ? So there's no need to be
remembering the other results, let alone sorting them:

sub largest {
my $fun = shift;
my $maxStr = shift;
my $maxRes = $fun->($maxStr);
for (@_) {
my $res = $fun->($_);
if ($res > $maxRes) {
        ($maxStr, $maxRes) = ($_, $res);
}
}
return $maxStr;
}

my $winner = largest(sub { check($goodPrefix.shift) }, $A, $C, $T, $G);
print "$winner wins!\n";

) Any thoughts on some awesome one-liner that I can't see?

If you don't count the 'largest' function, it's a one-liner. :-)


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Similar ThreadsPosted
Good documentation or good source examples for Image::Magick or converter from commandline to perl April 1, 2008, 5:23 pm
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you are, April 21, 2005, 1:25 pm
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you are, April 22, 2005, 3:33 pm
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you are, April 24, 2005, 6:50 pm
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you are, May 1, 2005, 11:27 am
Perl/regex Advice August 5, 2004, 4:42 pm
advice for a gui for my perl scripts. April 29, 2005, 11:37 am
Acceptible idiom ? October 11, 2004, 12:36 pm
Concise idiom sought March 25, 2006, 9:14 am
idiom for managing passed arguments? October 18, 2005, 12:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap