more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
authorwenzelm
Tue, 14 May 2013 13:46:33 +0200
changeset 51981 a8ffd3692f57
parent 51980 fe16d1128a14
child 51982 fb4352e89022
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
src/Pure/library.scala
--- a/src/Pure/library.scala	Tue May 14 12:46:26 2013 +0200
+++ b/src/Pure/library.scala	Tue May 14 13:46:33 2013 +0200
@@ -8,6 +8,8 @@
 package isabelle
 
 
+import scala.collection.mutable
+
 import java.util.Locale
 import java.util.concurrent.{Future => JFuture, TimeUnit}
 
@@ -32,10 +34,21 @@
   /* separated chunks */
 
   def separate[A](s: A, list: List[A]): List[A] =
-    list match {
-      case x :: xs if !xs.isEmpty => x :: s :: separate(s, xs)
-      case _ => list
+  {
+    val result = new mutable.ListBuffer[A]
+    var first = true
+    for (x <- list) {
+      if (first) {
+        first = false
+        result += x
+      }
+      else {
+        result += s
+        result += x
+      }
     }
+    result.toList
+  }
 
   def separated_chunks(sep: Char, source: CharSequence): Iterator[CharSequence] =
     new Iterator[CharSequence] {