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

VladD2 svnadmin at nemerle.org
Wed Jan 10 15:28:15 CET 2007


Log:
Add lazy variants of Map/Filter function.

Author: VladD2
Date: Wed Jan 10 15:28:14 2007
New Revision: 7240

Modified:
   nemerle/trunk/lib/narray.n

Modified: nemerle/trunk/lib/narray.n
==============================================================================
--- nemerle/trunk/lib/narray.n	(original)
+++ nemerle/trunk/lib/narray.n	Wed Jan 10 15:28:14 2007
@@ -33,38 +33,79 @@
 {
   public module NCollectionsUtils
   {
+    // Lazy functions
+    //
+
+    /// Convert a sequence of one type to sequence of another type.
+    /// Convertion execute in lazy manner.
+    public MapLazy[From, To] (
+      this source : SCG.IEnumerable [From],
+      convert : From -> To
+    )
+      : SCG.IEnumerable [To]
+    {
+      foreach(elem in source)
+        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],
+      isMatch : From -> bool,
+      convert : From -> To
+    )
+      : SCG.IEnumerable [To]
+    {
+      foreach(elem when isMatch(elem) in source)
+        yield convert(elem);
+    }
+
+    /// Filter elements of sequence in lazy manner.
+    public FilterLazy [T] (this source : SCG.IEnumerable [T], predicate : T -> bool) : SCG.IEnumerable [T]
+    {
+      foreach(elem when predicate(elem) in source)
+        yield elem;
+    }
+
+    // 
+    // Lazy functions
+
     public FoldLeft [TAccumulator, T] (
       this source : SCG.IEnumerable [T], 
-      mutable ini : TAccumulator, f : T * TAccumulator -> TAccumulator
+      mutable ini : TAccumulator,
+      convert : T * TAccumulator -> TAccumulator
     )
       : TAccumulator
     {
       foreach (value in source)
-        ini = f(value, ini);
+        ini = convert(value, ini);
       
       ini
     }
 
     public FoldRight [TAccumulator, T] (
       this source : SCG.IEnumerable [T],
-      mutable ini : TAccumulator, f : T * TAccumulator -> TAccumulator
+      mutable ini : TAccumulator,
+      convert : T * TAccumulator -> TAccumulator
     )
       : TAccumulator
     {
       def ary = source.ToArray ();
       for (mutable i = ary.Length - 1; i <= 0; i--)
-        ini = f(ary[i], ini);
+        ini = convert(ary[i], ini);
       
       ini
     }
 
     public Fold [TAccumulator, T] (
       this source : SCG.IEnumerable [T],
-      mutable ini : TAccumulator, f : T * TAccumulator -> TAccumulator
+      mutable ini : TAccumulator,
+      convert : T * TAccumulator -> TAccumulator
     )
       : TAccumulator
     {
-      FoldLeft (source, ini, f)
+      FoldLeft (source, ini, convert)
     }
 
     public BinarySearch [TElem] (
@@ -131,46 +172,46 @@
     /**
      * Convert a collection of one type to an array of another type.
      */
-    public MapToArray[From, To] (this source : SCG.ICollection[From], f : From -> To) : array [To]
+    public MapToArray[From, To] (this source : SCG.ICollection[From], convert : From -> To) : array [To]
     {
       match (source)
       {
         | null               => array (0);
-        | ary is array[From] => ary.Map (f);
-        | _                  => MapCollectionToArray (source, f);
+        | ary is array[From] => ary.Map (convert);
+        | _                  => MapCollectionToArray (source, convert);
       }
     }
 
-    private MapCollectionToArray[From, To] (source : SCG.ICollection[From], f : From -> To) : array [To]
+    private MapCollectionToArray[From, To] (source : SCG.ICollection[From], convert : From -> To) : array [To]
     {
       def tmp = array (source.Count);
       source.CopyTo (tmp, 0);
-      tmp.Map (f)
+      tmp.Map (convert)
     }
 
     /**
      * Convert collection of one type to array of another type. (Alias for MapToArray)
      */
-    public ConvertToArray [From, To] (this source : SCG.ICollection[From], f : From -> To) : array [To]
+    public ConvertToArray [From, To] (this source : SCG.ICollection[From], convert : From -> To) : array [To]
     {
-      MapToArray (source, f)
+      MapToArray (source, convert)
     }
     
     /**
      * Convert a sequence of one type to an array of another type.
      */
-    public MapToArray[From, To] (this source : SCG.IEnumerable [From], f : From -> To) : array [To]
+    public MapToArray[From, To] (this source : SCG.IEnumerable [From], convert : From -> To) : array [To]
     {
       match (source)
       {
         | null                          => array (0);
-        | ary is array[From]            => ary.Map (f);
-        | coll is SCG.ICollection[From] => MapCollectionToArray (coll, f);
+        | ary is array[From]            => ary.Map (convert);
+        | coll is SCG.ICollection[From] => MapCollectionToArray (coll, convert);
         | _ =>
           def dest = SCG.List();
 
           foreach (elem in source)
-            dest.Add(f (elem));
+            dest.Add(convert (elem));
             
           dest.ToArray()
       }
@@ -179,9 +220,9 @@
     /**
      * Convert sequence of one type to array of another type. (Alias for MapToArray)
      */
-    public ConvertToArray[From, To] (this source : SCG.IEnumerable [From], f : From -> To) : array [To]
+    public ConvertToArray[From, To] (this source : SCG.IEnumerable [From], convert : From -> To) : array [To]
     {
-      MapToArray(source, f)
+      MapToArray(source, convert)
     }
 
     /** Convert sequence to array with filtration. */
@@ -204,7 +245,7 @@
     public MapToArrayFiltered[From, To] (
       this source : SCG.IEnumerable [From],
       isMatch : From -> bool,
-      f : From -> To
+      convert : From -> To
     ) : array [To]
     {
       match (source)
@@ -214,7 +255,7 @@
           def dest = SCG.List();
 
           foreach (elem when isMatch(elem) in source)
-            dest.Add (f (elem));
+            dest.Add (convert (elem));
             
           dest.ToArray()
       }
@@ -223,13 +264,13 @@
     public ConvertToArrayFiltered[From, To] (
       this source : SCG.IEnumerable [From],
       isMatch : From -> bool,
-      f : From -> To
+      convert : From -> To
     ) : array [To]
     {
-      MapToArrayFiltered(source, isMatch, f)
+      MapToArrayFiltered(source, isMatch, convert)
     }
 
-    public MapToList[From, To] (this source : array [From], f : From -> To) : list [To]
+    public MapToList[From, To] (this source : array [From], convert : From -> To) : list [To]
     {
       match (source)
       {
@@ -238,48 +279,48 @@
           mutable dest = [];
         
           for (mutable i = source.Length - 1; i >= 0; i--)
-            dest ::= f (source[i]);
+            dest ::= convert (source[i]);
             
           dest
       }
     }
 
-    public MapToList[From, To] (this source : SCG.IList [From], f : From -> To) : list [To]
+    public MapToList[From, To] (this source : SCG.IList [From], convert : From -> To) : list [To]
     {
       match (source)
       {
         | null                => [];
-        | ary is array [From] => ary.MapToList(f);
+        | ary is array [From] => ary.MapToList(convert);
         | _ =>
           mutable dest = [];
         
           for (mutable i = source.Count - 1; i >= 0; i--)
-            dest ::= f (source[i]);
+            dest ::= convert (source[i]);
             
           dest
       }
     }
 
-    public MapToList[From, To] (this source : SCG.IEnumerable [From], f : From -> To) : list [To]
+    public MapToList[From, To] (this source : SCG.IEnumerable [From], convert : From -> To) : list [To]
     {
       match (source)
       {
         | null                      => [];
-        | ary is array [From]       => ary.MapToList(f);
-        | iList is SCG.IList [From] => iList.MapToList(f);
+        | ary is array [From]       => ary.MapToList(convert);
+        | iList is SCG.IList [From] => iList.MapToList(convert);
         | _ =>
           def dest = SCG.List();
 
           foreach (elem in source)
-            dest.Add (f (elem));
+            dest.Add (convert (elem));
             
           dest.ToList ()
       }
     }
 
-    public Map[From, To] (this source : SCG.IEnumerable [From], f : From -> To) : list [To]
+    public Map[From, To] (this source : SCG.IEnumerable [From], convert : From -> To) : list [To]
     {
-      source.MapToList (f)
+      source.MapToList (convert)
     }
 
 



More information about the svn mailing list