Finds the first element in a range that equals some value
Returns optional index of the first element predicate returns true for.
Finds the first element in a range that equals some value
Returns optional index of the last element predicate returns true for.
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));
Returns an optional index of an element.