Seq describes a lazy operation, allowing them to efficiently chain
use of all the higher-order collection methods (such as map and filter)
by not creating intermediate collections.
Seq is immutable — Once a Seq is created, it cannot be
changed, appended to, rearranged or otherwise modified. Instead, any
mutative method called on a Seq will return a new Seq.
Seq is lazy — Seq does as little work as necessary to respond to any
method call. Values are often created during iteration, including implicit
iteration when reducing or converting to a concrete data structure such as
a List or JavaScript Array.
For example, the following performs no work, because the resulting
Seq's values are never iterated:
Once the Seq is used, it performs only the work necessary. In this
example, no intermediate arrays are ever created, filter is called three
times, and map is only called once:
oddSquares.get(1); // 9
Any collection can be converted to a lazy Seq with Seq().
Seq
describes a lazy operation, allowing them to efficiently chain use of all the higher-order collection methods (such asmap
andfilter
) by not creating intermediate collections.Seq is immutable — Once a Seq is created, it cannot be changed, appended to, rearranged or otherwise modified. Instead, any mutative method called on a
Seq
will return a newSeq
.Seq is lazy —
Seq
does as little work as necessary to respond to any method call. Values are often created during iteration, including implicit iteration when reducing or converting to a concrete data structure such as aList
or JavaScriptArray
.For example, the following performs no work, because the resulting
Seq
's values are never iterated:Once the
Seq
is used, it performs only the work necessary. In this example, no intermediate arrays are ever created, filter is called three times, and map is only called once:Any collection can be converted to a lazy Seq with
Seq()
.Seq
allows for the efficient chaining of operations, allowing for the expression of logic that can otherwise be very tedious:As well as expressing logic that would otherwise seem memory or time limited, for example
Range
is a special kind of Lazy sequence.Seq is often used to provide a rich collection API to JavaScript Object.