Helper function for searching through an array based on some predicate function.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4760 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Robert Zubeck
2007-07-13 02:03:25 +00:00
parent 68b65bad41
commit 325dce876d
+17
View File
@@ -57,6 +57,23 @@ public class ArrayUtil
}
}
/**
* Returns the index of the first item in the array for which the predicate function
* returns true, or -1 if no such item was found. The predicate function should be of type:
* function (element :*) :Boolean { }
*/
public static function indexIf (arr :Array, predicate :Function) :int
{
if (arr != null) {
for (var ii :int = 0; ii < arr.length; ii++) {
if (predicate(arr[ii])) {
return ii;
}
}
}
return -1; // never found
}
public static function indexOf (arr :Array, element :Object) :int
{
if (arr != null) {