ArrayUtil.findIf convenience function

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5002 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Tom Conkling
2008-04-17 19:04:18 +00:00
parent f017673964
commit f0bf02796e
+13
View File
@@ -118,6 +118,19 @@ public class ArrayUtil
return -1; // never found
}
/**
* Returns 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 { }
*
* @return the matching element, or null if no matching element was found
*/
public static function findIf (arr :Array, predicate :Function) :*
{
var index :int = (arr != null ? indexIf(arr, predicate) : -1);
return (index >= 0 ? arr[index] : null);
}
/**
* Returns the first index of the supplied element in the array. Note that if the element
* implements Equalable, an element that is equals() will have its index returned, instead