ddash.algorithm.index

Returns an optional index of an element.

Members

Functions

indexOf
auto indexOf(Range range, T value, size_t fromIndex = 0)

Finds the first element in a range that equals some value

indexWhere
auto indexWhere(Range range, size_t fromIndex = 0)

Returns optional index of the first element predicate returns true for.

lastIndexOf
auto lastIndexOf(Range range, T value, size_t fromIndex = 0)

Finds the first element in a range that equals some value

lastIndexWhere
auto lastIndexWhere(Range range, size_t fromIndex = 0)

Returns optional index of the last element predicate returns true for.

Examples

1 import optional: some, none;
2 
3 auto arr1 = [1, 2, 3, 4];
4 
5 assert(arr1.indexWhere!(a => a % 2 == 0) == some(1));
6 assert(arr1.lastIndexWhere!(a => a % 2 == 0) == some(3));
7 
8 assert(arr1.indexWhere!(a => a == 5) == none);
9 assert(arr1.lastIndexWhere!(a => a % 2 == 0)(2) == some(1));
10 
11 auto arr2 = [1, 2, 1, 2];
12 
13 assert(arr2.indexOf(2) == some(1));

Meta