[svn] r7255: nemerle/trunk/lib/narray.n

VladD2 svnadmin at nemerle.org
Fri Jan 12 12:32:22 CET 2007


Log:
Add FindIndex() method into lib.

Author: VladD2
Date: Fri Jan 12 12:32:20 2007
New Revision: 7255

Modified:
   nemerle/trunk/lib/narray.n

Modified: nemerle/trunk/lib/narray.n
==============================================================================
--- nemerle/trunk/lib/narray.n	(original)
+++ nemerle/trunk/lib/narray.n	Fri Jan 12 12:32:20 2007
@@ -26,6 +26,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+using Nemerle.Assertions;
 using Nemerle.Collections;
 using SCG = System.Collections.Generic;
 
@@ -61,6 +62,22 @@
         yield convert(elem);
     }
 
+    /// Convert a sequence of one type to sequence of another type with filtration.
+    /// Convertion execute in lazy manner.
+    public MapLazyFiltered[From, To] (
+      this source : SCG.IEnumerable [From],
+      matchAndConvert : From -> bool * To
+    )
+      : SCG.IEnumerable [To]
+    {
+      foreach(elem in source)
+      {
+        def (isMatch, convertedValue) = matchAndConvert(elem);
+        when (isMatch)
+          yield convertedValue;
+      }
+    }
+
     /// Filter elements of sequence in lazy manner.
     public FilterLazy [T] (this source : SCG.IEnumerable [T], predicate : T -> bool) : SCG.IEnumerable [T]
     {
@@ -71,6 +88,33 @@
     // 
     // Lazy functions
 
+    public FindIndex[T]([NotNull] this source : array[T], [NotNull] isMatch : T -> bool) : int
+    {
+      for (mutable i = 0; i < source.Length; i++)
+        when (isMatch(source[i]))
+          Nemerle.Imperative.Return(i);
+
+      -1;
+    }
+
+    public FindIndex[T]([NotNull] this source : SCG.IList[T], [NotNull] isMatch : T -> bool) : int
+    { // doubling for performance reason
+      for (mutable i = 0; i < source.Count; i++)
+        when (isMatch(source[i]))
+          Nemerle.Imperative.Return(i);
+
+      -1;
+    }
+
+    public FindIndex[T]([NotNull] this source : SCG.List[T], [NotNull] isMatch : T -> bool) : int
+    { // doubling for performance reason
+      for (mutable i = 0; i < source.Count; i++)
+        when (isMatch(source[i]))
+          Nemerle.Imperative.Return(i);
+
+      -1;
+    }
+
     public FoldLeft [TAccumulator, T] (
       this source : SCG.IEnumerable [T], 
       mutable ini : TAccumulator,



More information about the svn mailing list