[svn] r7649: nemerle/trunk/lib: list.n nstring.n

VladD2 svnadmin at nemerle.org
Sat May 5 01:49:44 CEST 2007


Log:
1. Add public AppendSeq[T](this builder : NStringBuilder, ...) methods to std lib. 
2. Simple refactoring.

Author: VladD2
Date: Sat May  5 01:49:39 2007
New Revision: 7649

Modified:
   nemerle/trunk/lib/list.n
   nemerle/trunk/lib/nstring.n

Modified: nemerle/trunk/lib/list.n
==============================================================================
--- nemerle/trunk/lib/list.n	(original)
+++ nemerle/trunk/lib/list.n	Sat May  5 01:49:39 2007
@@ -551,7 +551,6 @@
       result
     }
 
-    
     // private helpers
     concat_helper (sep : string, sb : System.Text.StringBuilder) : void
     {
@@ -644,10 +643,10 @@
      * def result = RemoveDuplicates ([1, 2, 2, 3, 4, 4]);
      *     // result = [1, 2, 3, 4]
      */
-    public static RemoveDuplicates ['a] (l : list ['a]) : list ['a]
+    public static RemoveDuplicates ['a] (lst : list ['a]) : list ['a]
     {
-      def loop (l, acc){
-        match (l){
+      def loop (lst, acc){
+        match (lst){
           | [] => acc.Reverse ();
           | [x] => (x :: acc).Reverse ();
           | x :: ((y :: _) as xs) =>
@@ -659,7 +658,7 @@
         }
       }
 
-      loop (l, [])
+      loop (lst, [])
     }
 
     
@@ -739,9 +738,7 @@
     /**
      * Returns true if the given list is empty.
      */
-    public IsEmpty ['a] (l : list ['a]) : bool {
-      l.IsEmpty
-    }
+    public IsEmpty ['a] (lst : list ['a]) : bool { lst.IsEmpty }
     
     /**
      * Returns length of given list. Time O(n), Mem O(1).

Modified: nemerle/trunk/lib/nstring.n
==============================================================================
--- nemerle/trunk/lib/nstring.n	(original)
+++ nemerle/trunk/lib/nstring.n	Sat May  5 01:49:39 2007
@@ -27,6 +27,7 @@
  */
 
 using Nemerle.Collections;
+using SCG = System.Collections.Generic;
  
 namespace Nemerle.Utility
 {
@@ -36,9 +37,9 @@
      * Splits the string at positions of occurrence of one
      * of the characters from the given array.
      */
-    public Split (str : string, params sep : array [char]) : list [string]
+    public Split (str : string, params seperators : array [char]) : list [string]
     {
-      def seplen = sep.Length - 1;
+      def seplen = seperators.Length - 1;
       
       mutable last = str.Length - 1;
       mutable res = [];
@@ -46,7 +47,7 @@
       for (mutable i = str.Length - 1; i >= 0; --i)
       {
         def isseparator (j) {
-          if (sep[j] == str[i]) {
+          if (seperators[j] == str[i]) {
             when (last - i > 0)
               res = str.Substring (i + 1, last - i) :: res;
             last = i - 1;
@@ -264,71 +265,130 @@
   {
     /// <summary>Appends the string representation of a specified list items to the end of a <see cref="NStringBuilder"/> instance.</summary>
     /// <returns>A reference to the NStringBuilder instance after the append operation has completed.</returns>
-    /// <param name="sb">A <see cref="NStringBuilder"/> instance pointer. </param>
+    /// <param name="builder">A <see cref="NStringBuilder"/> instance pointer. </param>
     /// <param name="l">A list. </param>
     /// <param name="sep">The string used as element separator. </param>
-    public AppendList['a] (this sb : NStringBuilder, l : list ['a], sep : string) : NStringBuilder
+    public AppendSeq[T] (this builder : NStringBuilder, seq : SCG.IEnumerable [T], seperator : string) : void
+    {
+      mutable secondTime = false;
+      
+      foreach (elem in seq)
+      {
+        when (secondTime)
+        {
+          secondTime = true;
+          builder.Append(seperator);
+        }
+        
+        builder.Append(elem);
+      }
+    }
+
+    /// <summary>Appends the string representation of a specified list items to the end of a <see cref="NStringBuilder"/> instance.</summary>
+    /// <returns>A reference to the NStringBuilder instance after the append operation has completed.</returns>
+    /// <param name="builder">A <see cref="NStringBuilder"/> instance pointer. </param>
+    /// <param name="l">A list. </param>
+    /// <param name="sep">The string used as element separator. </param>
+    public AppendSeq[T] (
+      this builder   : NStringBuilder, 
+           seq       : SCG.IEnumerable [T], 
+           seperator : string,
+           convert   : T -> string
+    )
+      : void
+    {
+      mutable secondTime = false;
+      
+      foreach (elem in seq)
+      {
+        when (secondTime)
+        {
+          secondTime = true;
+          builder.Append(seperator);
+        }
+        
+        def str = convert(elem);
+        builder.Append(str);
+      }
+    }
+
+    /// <summary>Appends the string representation of a specified list items to the end of a <see cref="NStringBuilder"/> instance.</summary>
+    /// <returns>A reference to the NStringBuilder instance after the append operation has completed.</returns>
+    /// <param name="builder">A <see cref="NStringBuilder"/> instance pointer. </param>
+    /// <param name="l">A list. </param>
+    /// <param name="sep">The string used as element separator. </param>
+    public AppendSeq[T] (this builder : NStringBuilder, seq : list [T], seperator : string) : void
+    {
+      _ = AppendList (builder, seq, seperator);
+    }
+
+    /// <summary>Appends the string representation of a specified list items to the end of a <see cref="NStringBuilder"/> instance.</summary>
+    /// <returns>A reference to the NStringBuilder instance after the append operation has completed.</returns>
+    /// <param name="builder">A <see cref="NStringBuilder"/> instance pointer. </param>
+    /// <param name="l">A list. </param>
+    /// <param name="seperator">The string used as element separator. </param>
+    public AppendList[T] (this builder : NStringBuilder, l : list [T], seperator : string) : NStringBuilder
     {
       match (l) {
-        | [x] => sb.Append (x)
-        | x :: xs => AppendList (sb.Append (x).Append (sep), xs, sep)
-        | [] => sb
+        | [x] => builder.Append (x)
+        | x :: xs => AppendList (builder.Append (x).Append (seperator), xs, seperator)
+        | [] => builder
       }
     }
 
     /// <summary>Appends the string representation of a specified list items to the end of a <see cref="NStringBuilder"/> instance.</summary>
     /// <returns>A reference to the NStringBuilder instance after the append operation has completed.</returns>
-    /// <param name="sb">A <see cref="NStringBuilder"/> instance pointer. </param>
+    /// <param name="builder">A <see cref="NStringBuilder"/> instance pointer. </param>
     /// <param name="l">A list. </param>
     /// <param name="append">A function used to append elements. </param>
-    /// <param name="sep">The string used as element separator. </param>
-    public AppendList['a] (this sb : NStringBuilder, l : list ['a], append : NStringBuilder * 'a -> NStringBuilder, sep : string) : NStringBuilder
+    /// <param name="seperator">The string used as element separator. </param>
+    public AppendList[T] (this builder : NStringBuilder, l : list [T], append : NStringBuilder * T -> NStringBuilder, seperator : string) : NStringBuilder
     {
       match (l) {
-        | [x] => append (sb, x)
-        | x :: xs => AppendList (append (sb, x).Append (sep), xs, append, sep)
-        | [] => sb
+        | [x] => append (builder, x)
+        | x :: xs => AppendList (append (builder, x).Append (seperator), xs, append, seperator)
+        | [] => builder
       }
     }
 
     /// <summary>Appends to the end of a <see cref="NStringBuilder"/> instance if a condition is true.</summary>
     /// <returns>A reference to the NStringBuilder instance after the append operation has completed.</returns>
-    /// <param name="sb">A <see cref="NStringBuilder"/> instance pointer. </param>
+    /// <param name="builder">A <see cref="NStringBuilder"/> instance pointer. </param>
     /// <param name="condition">true to cause a message to be written; otherwise, false. </param>
     /// <param name="append">A function used to append elements. </param>
-    public AppendWhen (this sb : NStringBuilder, condition : bool, append : NStringBuilder -> NStringBuilder) : NStringBuilder
+    public AppendWhen (this builder : NStringBuilder, condition : bool, append : NStringBuilder -> NStringBuilder) : NStringBuilder
     {
       if (condition)
-        append (sb)
+        append (builder)
       else
-        sb
+        builder
     }
 
     /// <summary>Appends to the end of a <see cref="NStringBuilder"/> instance if a condition is false.</summary>
     /// <returns>A reference to the NStringBuilder instance after the append operation has completed.</returns>
-    /// <param name="sb">A <see cref="NStringBuilder"/> instance pointer. </param>
+    /// <param name="builder">A <see cref="NStringBuilder"/> instance pointer. </param>
     /// <param name="condition">true to cause a message to be written; otherwise, false. </param>
     /// <param name="append">A function used to append elements. </param>
-    public AppendUnless (this sb : NStringBuilder, condition : bool, append : NStringBuilder -> NStringBuilder) : NStringBuilder
+    public AppendUnless (this builder : NStringBuilder, condition : bool, append : NStringBuilder -> NStringBuilder) : NStringBuilder
     {
       if (condition)
-        sb
+        builder
       else
-        append (sb)
+        append (builder)
     }
 
     /// <summary>Appends the string representation of a specified item to the end of a <see cref="NStringBuilder"/> instance number of times.</summary>
     /// <returns>A reference to the NStringBuilder instance after the append operation has completed.</returns>
-    /// <param name="sb">A <see cref="NStringBuilder"/> instance pointer. </param>
+    /// <param name="builder">A <see cref="NStringBuilder"/> instance pointer. </param>
     /// <param name="count">The number of times when the item should be written. </param>
     /// <param name="a">The item. </param>
-    /// <param name="sep">The string used as element separator. </param>
-    public AppendNTimes['a] (this sb : NStringBuilder, count : int, a : 'a, sep : string) : NStringBuilder
+    /// <param name="seperator">The string used as element separator. </param>
+    public AppendNTimes[T] (this builder : NStringBuilder, count : int, a : T, seperator : string) : NStringBuilder
     {
       def loop (cnt) {
-        | 1 => sb.Append(a)
-        | x when x > 0 => loop(x - 1).Append(sep).Append(a)
-        | _ => sb
+        | 1 => builder.Append(a)
+        | x when x > 0 => loop(x - 1).Append(seperator).Append(a)
+        | _ => builder
       }
 
       loop (count)
@@ -336,16 +396,16 @@
 
     /// <summary>Appends to the end of a <see cref="NStringBuilder"/> instance number of times.</summary>
     /// <returns>A reference to the NStringBuilder instance after the append operation has completed.</returns>
-    /// <param name="sb">A <see cref="NStringBuilder"/> instance pointer. </param>
+    /// <param name="builder">A <see cref="NStringBuilder"/> instance pointer. </param>
     /// <param name="count">The number of times when the function should be invoked. </param>
     /// <param name="append">A function used to append elements. </param>
-    /// <param name="sep">The string used as element separator. </param>
-    public AppendNTimes (this sb : NStringBuilder, count : int, append : NStringBuilder -> NStringBuilder, sep : string) : NStringBuilder
+    /// <param name="seperator">The string used as element separator. </param>
+    public AppendNTimes (this builder : NStringBuilder, count : int, append : NStringBuilder -> NStringBuilder, seperator : string) : NStringBuilder
     {
       def loop (cnt) {
-        | 1 => append(sb)
-        | x when x > 0 => append(loop(x - 1).Append(sep))
-        | _ => sb
+        | 1 => append(builder)
+        | x when x > 0 => append(loop(x - 1).Append(seperator))
+        | _ => builder
       }
 
       loop (count)



More information about the svn mailing list