Non-strict scanLeft view

I have a problem. scanLeft works with Iterator, Stream and view. I'm not sure where this difference comes from. Let's look at this example:

scala> (1 to 4).iterator.scanLeft(0)((a,b) => { println(b) ; a + b}).take(2).toList
1
2
res1: List[Int] = List(0, 1)

scala> (1 to 4).toStream.scanLeft(0)((a,b) => { println(b) ; a + b}).take(2).toList
1
res2: List[Int] = List(0, 1)

scala> (1 to 4).view.scanLeft(0)((a,b) => { println(b) ; a + b}).take(2).toList
1
2
3
4
res4: List[Int] = List(0, 1)

The strangest thing with a view. He acted as if he were not lazy. However, using .map is fine.

scala> (1 to 4).view.map{ b => println(b) ; b}.take(2).toList
1
2
res9: List[Int] = List(1, 2)

Can someone tell me the reason? thanks in advance

+4
source share
1 answer

This is a mistake in the views. There are many such errors. See https://issues.scala-lang.org/browse/SI-4332 for more details . (My comment is 04 / Jan / 2013, I noticed the same thing as you, oh scanLeft.)

Due to their many quality problems, I never use representations in my own code, nor do I recommend them to others.

- ; . https://github.com/scala/slip/pull/17. , .

+4

Source: https://habr.com/ru/post/1606501/


All Articles