我根据自身定义了一个流(递归定义)。尝试访问流的第二个元素时,会抛出StackOverflowError
。来自scala控制台的代码:
scala> val s1 = Stream.iterate(1)(identity _)
s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> lazy val s2 : Stream[Int]= Stream.cons(1, (s2, s1).zipped.map { _ + _ })
s2: Stream[Int] = <lazy>
scala> s1 take 5 print
1, 1, 1, 1, 1, empty
scala> s2(0)
res4: Int = 1
scala> s2(1)
java.lang.StackOverflowError
at $anonfun$s2$1$$anonfun$apply$1.apply(<console>:9)
at $anonfun$s2$1$$anonfun$apply$1.apply(<console>:9)
at scala.Tuple2$Zipped$$anonfun$map$1.apply(Tuple2.scala:62)
at scala.collection.immutable.Stream.foreach(Stream.scala:255)
at scala.Tuple2$Zipped.map(Tuple2.scala:60)
at $anonfun$s2$1.apply(<console>:9)
at $anonfun$s2$1.apply(<console>:9)
at scala.collection.immutable.Stream$Cons.tail(Stream.scala:556)
at scala.collection.immutable.Stream$Cons.tail(Stream.scala:550)
at scala.collection.immutable.Stream.foreach(Stream.scala:256)
at scala.Tuple2$Zipped.map(Tuple2.scala:60)
at $anonfun$s2$1.apply(<console>:9)
at $anonfun$s2$1.apply(<console>:9)
at scala.collection.immutable.Stream$Cons.tail(Stream.scala:556)
at scala.collection.immutable.Str...
我无法理解堆栈溢出的原因。由于流本质上是惰性的,因此递归映射应该有效。
这种情况有什么问题?
我使用的是Scala版本2.8.0.RC2。
答案 0 :(得分:8)
问题是zipped
不是懒惰的 - 它实际上试图在那里评估该地图。您可以使用zip
执行所需操作。
scala> val s1 = Stream.iterate(1)(identity _)
s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> lazy val s2: Stream[Int] = Stream.cons(1, (s2 zip s1).map {s=>s._1+s._2})
s2: Stream[Int] = <lazy>
scala> s2 take 5 print
1, 2, 3, 4, 5, empty