Subject: | first_map |
I found myself writing this today:
use List::Utils qw(first);
sort {
first { $_ }
map { $a->[$_] cmp $b->[$_] } @order_by_indexes
} @rows;
which is the equivalent of
sort {
$a->[$order_by_indexes[0]] cmp $a->[$order_by_indexes[0]] ||
$a->[$order_by_indexes[1]] cmp $b->[$order_by_indexes[1]] ||
...
} @rows;
but its less efficient because there's no short-circuit. It does all
the comparisons whether they're needed or not.
It would be nice if there was a function like first() except instead of
returning the first element for which the block returns true it would
return the result of that first true block. In short, a first() for map
instead of grep. first_map() seems the obvious name.
If you like the idea, I'll code up an implementation.