Subject: | Feature request: firstidx_monotone |
Sometimes you use firstidx when you know that the result is 'monotone',
that is, the condition is false for the first N elements (N >= 0) and then
true for any remaining elements in the list. For example
my @sorted = sort @strings;
my $pos = firstidx { $_ lt 'xyz' } @sorted;
In such situations you could expect a speedup from doing a binary search
rather than a linear scan. How about a drop-in replacement:
my $pos = firstidx_monotone { $_ lt 'xyz' } @sorted;
This could use a binary search internally and so be a lot faster than
plain firstidx, and also much faster than implementing a binary search
in pure Perl. It is also a lot simpler than the very general interface
provided by Search::Binary.
I would document it formally as 'does the same thing as firstidx,
but has the precondition that for any list index i, 0 <= i < length,
if condition(i) then condition(i + 1).'