dlarochelle has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to select the maximum element from a list according to some arbitrary criteria.
Basically what I'd like to be able to do is something like
$max_el = max { complex_calculation($_) } @list;
and have $max_el be the element for which complex_calculation reported the highest value
Unfortunately the max in List::Util doesn't take a block.
I was able to cobble something together with List::Util::reduce but this seems less than ideal.
$max = reduce { complex_calculation($a) > complex_calculation($b) ? $a + : $b };
My problems with this approach are
1.) complex_calculation() is called twice for each element in the list
2.) the code is less readable & less concise than it could be
3.) this seems like I'm reinventing the wheel.
Does anyone know of a CPAN module or library that will encapsulate this type of functionality?
I've googled by couldn't find anything.
Thanks,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: List::Util max with block
by ikegami (Patriarch) on Nov 06, 2009 at 19:39 UTC | |
by ikegami (Patriarch) on Nov 06, 2009 at 20:06 UTC | |
by dlarochelle (Sexton) on Nov 08, 2009 at 16:07 UTC | |
|
Re: List::Util max with block
by keszler (Priest) on Nov 06, 2009 at 20:27 UTC | |
by ikegami (Patriarch) on Nov 06, 2009 at 21:15 UTC | |
|
Re: List::Util max with block
by toolic (Bishop) on Nov 06, 2009 at 19:38 UTC | |
by ikegami (Patriarch) on Nov 06, 2009 at 19:40 UTC |