Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between List.view and LazyList?

I am new to Scala and I just learned that LazyList was created to replace Stream, and at the same time they added the .view methods to all collections.

So, I am wondering why was LazyList added to Scala collections library, when we can do List.view?

I just looked at the Scaladoc, and it seems that the only difference is that LazyList has memoization, while View does not. Am I right or wrong?

like image 435
pavel_orekhov Avatar asked Jan 04 '20 02:01

pavel_orekhov


2 Answers

Stream elements are realized lazily except for the 1st (head) element. That was seen as a deficiency.

A List view is re-evaluated lazily but, as far as I know, has to be completely realized first.

def bang :Int = {print("BANG! ");1}

LazyList.fill(4)(bang)  //res0: LazyList[Int] = LazyList(<not computed>)
Stream.fill(3)(bang)    //BANG! res1: Stream[Int] = Stream(1, <not computed>)
List.fill(2)(bang).view //BANG! BANG! res2: SeqView[Int] = SeqView(<not computed>)
like image 151
jwvh Avatar answered Oct 30 '22 01:10

jwvh


In 2.13, you can't force your way back from a view to the original collection type:

scala> case class C(n: Int) { def bump = new C(n+1).tap(i => println(s"bump to $i")) }
defined class C

scala> List(C(42)).map(_.bump)
bump to C(43)
res0: List[C] = List(C(43))

scala> List(C(42)).view.map(_.bump)
res1: scala.collection.SeqView[C] = SeqView(<not computed>)

scala> .force
            ^
       warning: method force in trait View is deprecated (since 2.13.0): Views no longer know about their underlying collection type; .force always returns an IndexedSeq
bump to C(43)
res2: scala.collection.IndexedSeq[C] = Vector(C(43))

scala> LazyList(C(42)).map(_.bump)
res3: scala.collection.immutable.LazyList[C] = LazyList(<not computed>)

scala> .force
bump to C(43)
res4: res3.type = LazyList(C(43))

A function taking a view and optionally returning a strict realization would have to also take a "forcing function" such as _.toList, if the caller needs to choose the result type.

I don't do this sort of thing at my day job, but this behavior surprises me.

like image 28
som-snytt Avatar answered Oct 30 '22 03:10

som-snytt