Subject: | Wishlist: first_index, rfirst(_index), final(_index) |
Extend the family of functions around 'first', to include:
* first_index
my $index = first_index { COND } LIST
returns the first index of LIST for which COND is true; thus satisfying
first { COND } LIST === LIST[ first_index { COND } LIST ]
(analogous to List::MoreUtils::firstidx but named a little clearer)
* rfirst / rfirst_index
my $item = rfirst { COND } LIST
my $index = rfirst_index { COND } LIST
returns rightmost item or index of LIST for which COND is true, when scanned decrementally from the rightmost element first. This latter fact is important in the case of stateful condition functions.
rfirst { COND } LIST === LIST[ rfirst_index { COND } LIST ]
rfirst { COND } LIST === first { COND } reverse LIST
rfirst_index { COND } LIST doesn't have a neat form using first_index on reverse LIST except when the length of the list is known.
Optionally, for completeness:
* final / final_index
my $item = final { COND } LIST
my $index = final_index { COND } LIST
Returns the item or index of LIST for which COND is true for the final time when scanned incrementally, left to right. This might matter if COND is stateful in some way, such that a different answer is achieved in this scanning order.
--
Paul Evans