[svn] r7607: nemerle/trunk/lib: list.n narray.n option.n

VladD2 svnadmin at nemerle.org
Sun Apr 22 05:53:27 CEST 2007


Log:
1. Lib refactoring.
2. Add MapFiltered to list[].

Author: VladD2
Date: Sun Apr 22 05:53:25 2007
New Revision: 7607

Modified:
   nemerle/trunk/lib/list.n
   nemerle/trunk/lib/narray.n
   nemerle/trunk/lib/option.n

Modified: nemerle/trunk/lib/list.n
==============================================================================
--- nemerle/trunk/lib/list.n	(original)
+++ nemerle/trunk/lib/list.n	Sun Apr 22 05:53:25 2007
@@ -314,8 +314,12 @@
       }
     }
     
-    public Map['b] (f : 'a -> 'b) : list ['b] {
-      NCL.Map (this, f)
+    public Map['b] (convert : 'a -> 'b) : list ['b] {
+      NCL.Map (this, convert)
+    }
+
+    public MapFiltered['b] (predicate : 'a -> bool, convert : 'a -> 'b) : list ['b] {
+      NCL.MapFiltered (this, predicate, convert)
     }
 
     public RevMap['b] (f : 'a -> 'b) : list ['b] {
@@ -908,14 +912,28 @@
       }
     }
 
-    public Map['a, 'b] (l : list ['a], f : 'a -> 'b) : list ['b] {
-      $[ f(x) | x in l ]
+    public MapFiltered['a, 'b] (lst : list ['a], predicate : 'a -> bool, convert : 'a -> 'b) : list ['b] {
+      $[ convert(x) | x in lst, predicate (x) ]
+    }
+
+    public Map['a, 'b] (lst : list ['a], convert : 'a -> 'b) : list ['b] {
+      $[ convert(x) | x in lst ]
+    }
+
+    public RevMap['a, 'b] (l : list ['a], convert : 'a -> 'b) : list ['b] {
+      def loop (acc : list ['b], x : list ['a]) : list ['b] {
+        match (x) {
+          | h :: t => loop (convert (h) :: acc, t)
+          | [] => acc
+        }
+      };
+      loop ([], l)
     }
 
-    public RevMap['a, 'b] (l : list ['a], f : 'a -> 'b) : list ['b] {
+    public RevMapFiltered['a, 'b] (l : list ['a], predicate : 'a -> bool, convert : 'a -> 'b) : list ['b] {
       def loop (acc : list ['b], x : list ['a]) : list ['b] {
         match (x) {
-          | h :: t => loop (f (h) :: acc, t)
+          | h :: t => if (predicate (h)) loop (convert (h) :: acc, t) else loop (acc, t)
           | [] => acc
         }
       };

Modified: nemerle/trunk/lib/narray.n
==============================================================================
--- nemerle/trunk/lib/narray.n	(original)
+++ nemerle/trunk/lib/narray.n	Sun Apr 22 05:53:25 2007
@@ -479,12 +479,10 @@
       def sb = System.Text.StringBuilder();
       foreach (elem in source)
       {
-        _ = sb.Append (elem);
-
-        unless (isFirstTime)
-          _ = sb.Append (separator);
+        if (isFirstTime) isFirstTime = false;
+        else             _ = sb.Append (separator);
 
-        isFirstTime = false;
+        _ = sb.Append (elem);
       }
 
       sb.ToString()

Modified: nemerle/trunk/lib/option.n
==============================================================================
--- nemerle/trunk/lib/option.n	(original)
+++ nemerle/trunk/lib/option.n	Sun Apr 22 05:53:25 2007
@@ -124,16 +124,18 @@
     {
       | (Some (a), Some (b)) => a.Equals (b)
       | (None, None)         => true
+      | (null, null)         => throw System.ArgumentException ("option can't be null")
       | _                    => false
     }
 
     /**
      * Safe optional value mapping.
      */
-    public Map ['a, 'b] (x : option ['a], f : 'a -> 'b) : option ['b]
+    public Map ['a, 'b] (this x : option ['a], f : 'a -> 'b) : option ['b]
     {
       match (x) {
         | Some (x) => Some (f (x))
+        | null     => throw System.ArgumentException ("option can't be null")
         | None => None ()
       }
     }    
@@ -141,11 +143,11 @@
     /**
      * Same as ignore (Map (x, f)).
      */
-    public Iter ['a] (x : option ['a], f : 'a -> void) : void
+    public Iter ['a] (this x : option ['a], f : 'a -> void) : void
     {
       match (x) {
         | Some (x) => f (x)
-        | None => ()
+        | null | None => ()
       }
     }    
 
@@ -167,6 +169,7 @@
     {
       match (x) {
         | None => true
+        | null => throw System.ArgumentException ("option can't be null")
         | _ => false
       }
     }  
@@ -175,12 +178,11 @@
      * Unwraps an optional value; throws an argument exception
      * if the value is not present.
      */
-    public UnSome ['a] (x : option ['a]) : 'a
+    public UnSome ['a] (this x : option ['a]) : 'a
     {
       match (x) {
         | Some (x) => x
-        | None =>
-          throw System.ArgumentException ("Option.UnSome")
+        | _        => throw System.ArgumentException ("Option.UnSome")
       }
     }
   }



More information about the svn mailing list