Scala linked stackoverflow list

Using scala, I added about 100,000 nodes to the linked list. When I use the length of a function like mylist.length. I get the error "java.lang.StackOverflowError", is my list large for processing? A list is just string objects.

+5
source share
4 answers

It seems that the library implementation is not tail recursive override def length: Int = if (isEmpty) 0 else next.length + 1. This seems to be something that could be discussed on the mailing list to see if an extension ticket should be opened.

You can calculate the length as follows:

def length[T](l:LinkedList[T], acc:Int=0): Int =
  if (l.isEmpty) acc else length(l.tail, acc + 1)
+11
source

Scala n, . , .

+1

/, JVM.

scala JAVA_OPTS="-Xmx512M -Xms16M -Xss16M" MyClass.scala

-Xss<size>  maximum native stack size for any thread
-Xms<size>  set initial Java heap size
-Xmx<size>  set maximum Java heap size

.

. scala.

0

, length? , ( ). .

Despite the fact that this is absolutely supervisory, which can be easily fixed in the standard library using the tail recursive function. Hope we can get it on time for 2.9.0.

0
source

All Articles