9

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?

4
  • Sadly I feel like none of the answers are really complete here. I still don't understand the benefit of having both. Views seem pretty useless now as they're missing many methods, but I could be missing something...
    – Dici
    Nov 16, 2021 at 23:15
  • Some hints there but still not comprehensive: baeldung.com/scala/stream-vs-views-vs-iterators
    – Dici
    Nov 16, 2021 at 23:18
  • @Dici the benefit is the memoization of LazyList. List.view doesn't provide memoization. Sometimes memoization isn't necessary, because it takes up memory, so you can use LazyList. Dec 21, 2022 at 9:42
  • I had asked a question later on to which answers were useful: stackoverflow.com/questions/69997294/…. I still think one can do most things with only iterators and lazy lists, but yeah there are some use cases for views. Not sure it's worth the added complexity to the language, but well.
    – Dici
    Dec 23, 2022 at 0:02

3 Answers 3

8

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>)
3
  • That makes sense, but I was talking from the stand point of already having a view and a list, not from the stand point of what happens before you obtain them, but good answer! I haven't thought about this either. Jan 4, 2020 at 3:46
  • hey_you! that's interesting. Usually folks care about every bit of laziness, as described by jwh. I guess "re-evaluated" means transforms like map, etc.
    – som-snytt
    Jan 4, 2020 at 7:08
  • Just wondering, because I'm new to Scala, what should my "go to" Seq collection be? Vector? @som-snytt great point about forcing btw! Jan 4, 2020 at 23:29
5

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.

1

The difference is that LazyList can be generated from huge/infinite sequence, so you can do something like:

val xs = (1 to 1_000_000_000).to(LazyList)

And that won't run out of memory. After that you can operate on the lazy list with transformers. You won't be able to do the same by creating a List and taking a view from it. Having said that, SeqView has a much reacher set of methods compared to LazyList and that's why you can actually take a view of a LazyList like:

val xs = (1 to 1_000_000_000).to(LazyList)
val listView = xs.view
2
  • But will lazy list run out of memory when I iterate over all its elements due to memoization? Oct 6, 2022 at 13:50
  • 1
    @pavel_orekhov Yep, but if you need to apply several filters and transforms on it and then access just a small subset of elements - then you are good.
    – Telek
    Oct 6, 2022 at 15:26

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.