Skip to content

X LazySeq

John McClean edited this page Sep 11, 2018 · 2 revisions

LazySeq

LazySeq is a totally Lazy LinkedList, both the head and the tail are lazily evaluated. It contrasts with Seq (which is an eager LinkedList) and ReactiveSeq (which represents a lazy stream of ephemeral data). LazySeq is both lazy and a persistent shared memory data structure.

A LazySeq consists of a lazily evaluated head and a lazily evaluated tail.

lazyseq

Each node of a LazySeq consists either of a Cons (which represents a value that is present) or Nil which represents the termination of the List (a LazySeq which only consists of a Nil node is an empty List).

nilandcons

Lazy operations

Operations such as map, flatMap and filter are performed entirely lazily. By way of comparision the map and flatMap operations on the Stream data structure in Scala (and in similar Java libraries) perform an eager operation on the head. In many cases this can lead to Runtime issues such as infinite loops. The following Scala code runs forever

Stream.continually(1)
      .filter(i=>false)
      .take(0)
      .isEmpty

Where as this cyclops code completes with the expected value of true

LazySeq.generate(()->1)
       .filter(i->false)
       .take(0)
       .isEmpty()

Making illegal states unrepresentable

It is core goal of Cyclops X to minimize the Runtime errors possible by making illegal states unrepresentable. We prevent IndexOutOfBoundsExceptions, for example, by either return an Option type when extracting values from a List or by providing a default value to be used in the case that no value exists at the specified index.

e.g.

LazySeq.of(1,2,3)
       .get(1000); //Option.none

LazySeq.of(1,2,3)
       .getOrElse(1000,-1) //-1

Lazy Folds

See Lazy Folds for more information on how to perform lazy, terminating foldRight operations on LazySeq.

Clone this wiki locally