From 325dce876dafaf38a093e5f4cdda9c10ed484e20 Mon Sep 17 00:00:00 2001 From: Robert Zubeck Date: Fri, 13 Jul 2007 02:03:25 +0000 Subject: [PATCH] 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 --- src/as/com/threerings/util/ArrayUtil.as | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/as/com/threerings/util/ArrayUtil.as b/src/as/com/threerings/util/ArrayUtil.as index b6335644e..3b36141c2 100644 --- a/src/as/com/threerings/util/ArrayUtil.as +++ b/src/as/com/threerings/util/ArrayUtil.as @@ -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) {