[svn] r6090: nemerle/trunk/lib/rlist.n

d svnadmin at nemerle.org
Sun Jan 29 10:39:37 CET 2006


Log:
Add enumerators for Pair and RList (thank's to malekith).


Author: d
Date: Sun Jan 29 10:39:36 2006
New Revision: 6090

Modified:
   nemerle/trunk/lib/rlist.n

Modified: nemerle/trunk/lib/rlist.n
==============================================================================
--- nemerle/trunk/lib/rlist.n	(original)
+++ nemerle/trunk/lib/rlist.n	Sun Jan 29 10:39:36 2006
@@ -26,19 +26,26 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+using SCG = System.Collections.Generic;
+
 namespace Nemerle.Collections {
 
   /** An auxillary data-structure for RList used instead of a regular
       tuple (which is a struct) for performance reasons. 
    */
   [Record] 
-  public class Pair ['a] { 
+  public class Pair ['a] : IEnumerable ['a] { 
     public fst : 'a; 
     public snd : 'a; 
 
     public override ToString () : string {
       $"($(this.fst), $(this.snd))"
     }
+
+    public GetEnumerator () : SCG.IEnumerator ['a] {
+      yield (this.fst);
+      yield (this.snd)
+    }
   }
   
   /** RList is short for Random Access List. It is a purely functional
@@ -46,7 +53,7 @@
       found in Chris Okasaki's "Purely Functional Data Structures" 
       (Cambridge University Press, 1999). 
    */
-  public variant RList ['a] {
+  public variant RList ['a] : IEnumerable ['a] {
     | Nil
     | Zero { 
       arg : RList [Pair ['a]] 
@@ -551,6 +558,24 @@
         | Nil => "Nil"
       }
     }
+
+    /* The enumerator for rlists */
+    public GetEnumerator () : SCG.IEnumerator ['a] {
+      match (this) {
+        | Zero (ps) =>
+          foreach (elem in ps) {
+            yield elem.fst;
+            yield elem.snd
+          }
+        | One (x, ps) =>
+          yield x;
+          foreach (elem in ps) {
+            yield elem.fst;
+            yield elem.snd
+          }
+        | Nil => {}
+      }
+    }
   }
 } 
 



More information about the svn mailing list