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

d svnadmin at nemerle.org
Sun Jan 29 16:14:55 CET 2006


Log:
Slightly optimize Nth, FoldLeft and FoldRight.


Author: d
Date: Sun Jan 29 16:14:54 2006
New Revision: 6092

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 16:14:54 2006
@@ -333,7 +333,14 @@
       match (xs) {
         | Zero (ps) => def (x, y) = RList [Pair ['a]].Nth (ps, i / 2);
                        if (i % 2 == 0) x else y
-        | One (x, ps) => if (i == 0) x else Nth (Zero (ps), i - 1)
+        | One (x, ps) => 
+          if (i == 0) x 
+          else { 
+            // the three lines below could be substituted with Nth (Zero (ps), i - 1)
+            def (x, y) = RList [Pair ['a]].Nth (ps, (i - 1) / 2);
+            if (i % 2 != 0) x 
+            else y 
+          }
         | Nil => throw System.Exception ("Subscript")
       }
     }
@@ -418,7 +425,8 @@
       match (xs) {
         | Zero (ps) => def f' (a, b) { f (a.snd, f (a.fst, b)) }
                        RList [Pair ['a]].FoldLeft (ps, acc, f')
-        | One (x, ps) => FoldLeft (Zero (ps), f (x, acc), f)
+        | One (x, ps) => def f' (a, b) { f (a.snd, f (a.fst, b)) } 
+                         RList [Pair ['a]].FoldLeft (ps, f (x, acc), f')
         | Nil => acc
       }
     }    
@@ -463,7 +471,8 @@
       match (xs) {
         | Zero (ps) => def f' (a, b) { f (a.fst, f (a.snd, b)) }
                        RList [Pair ['a]].FoldRight (ps, acc, f')
-        | One (x, ps) => f (x, FoldRight (Zero (ps), acc, f))
+        | One (x, ps) => def f' (a, b) { f (a.fst, f (a.snd, b)) }
+                         f (x, RList [Pair ['a]].FoldRight (ps, acc, f'))
         | Nil => acc
       }
     }



More information about the svn mailing list