Sequence vs LazyList

前端 未结 2 921
灰色年华
灰色年华 2020-12-30 04:44

I can\'t wrap my head around the differences between sequence and LazyList. They\'re both lazy and potentially infinite. While seq<\'T> is

2条回答
  •  天命终不由人
    2020-12-30 05:05

    LazyList computes each element only once regardless of how many times the list is traversed. In this way, it's closer to a sequence returned from Seq.cache (rather than a typical sequence). But, other than caching, LazyList behaves exactly like a list: it uses a list structure under the hood and supports pattern matching. So you might say: use LazyList instead of seq when you need list semantics and caching (in addition to laziness).

    Regarding both being infinite, seq's memory usage is constant while LazyList's is linear.

    These docs may be worth a read.

提交回复
热议问题