How to convert List to LazyList?
Asked Answered
J

2

7

How to make a list lazy, i.e. create a LazyList given a usual one? I tried to find suitable method in Scala documentation, but there is no such function.

Jipijapa answered 30/7, 2020 at 20:30 Comment(8)
list.to(LazyList) - in any case, what is the point? it is already computed.Scarify
myList.view will create a lazy SeqView so that future traversals will be lazy.Almsgiver
This looks like an XY problem. Why do you want to convert a List into a LazyList? There's no use of that laziness nowMacedonian
@LuisMiguelMejíaSuárez Lazy computations using the list as input? List(1,2,3,...,100).to(LazyList).map(veryExpensiveComputation).foldLeft(...).takeUntil...Puffin
@JamesMoore sure, although using view or iterator would have been better in this case. That is why I asked OP what was the end goal.Scarify
You want to convert a List into a LazyList so that transformations done afterwards have the LazyList semantics (memoized and lazy)Fermata
Use case: I have a List[java.io.Path] which you want to turn into a LazyList[Json] by reading the files to subsequently do various transformation on the Json that does not depend on the other input files/paths.Bismarck
Another non XY problem use case: You need to use a third party library that expects only a LazyListOnly
B
1

One way would be to unpack the list into a LazyList. For example:

LazyList(List(1,2,3) :_*)

Bismarck answered 7/7, 2023 at 16:33 Comment(2)
Are you sure this works like you think for very large lists? IOW, how is it different, more preferable, or less desirable than using the .to(LazyList) approach in my answer: https://mcmap.net/q/1564229/-how-to-convert-list-to-lazylistMisdirect
From your own example, substituting val validChars = LazyList(('A' to 'Z') :_*) works fine. Perhaps you can state what specifically you think is wrong with it? As far as style, it's just different to yours.Bismarck
M
0

To those Asking, "What's the point of wrapping an already computed Seq or List with a LazyList?"

Answer: Inferred type propagation

Example without LazyList which causes an OOME (OutOfMemoryException):

val validChars = //List[Char]
  ('A' to 'Z')
    .toList

val allCodes =
    for {
      a <- validChars
      b <- validChars
      c <- validChars
      d <- validChars
      e <- validChars
      f <- validChars
      g <- validChars
      h <- validChars
      i <- validChars
      j <- validChars
    } yield s"$a$b$c$d$e$f$g$h$i"

Example with LazyList enabling all sorts of stream-like approaches without generating an OOME:

val validChars = //LazyList[Char]
  ('A' to 'Z')
    .to(LazyList)

val allCodes =
    for {
      a <- validChars
      b <- validChars
      c <- validChars
      d <- validChars
      e <- validChars
      f <- validChars
      g <- validChars
      h <- validChars
      i <- validChars
      j <- validChars
    } yield s"$a$b$c$d$e$f$g$h$i"

Another option is to change the first line of the for comprehension to:

a <- validChars.to(LazyList)
Misdirect answered 13/5, 2024 at 16:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.