Sequences and laziness
"A seq is like a logical cursor." | ||
--Rich Hickey |
Sequences (commonly known as seqs) are a way to sequentially consume a succession of data. As with iterators, they let a user begin consuming elements from the head and proceed realizing one element after another. However, unlike iterators, sequences are immutable. Also, since sequences are only a view of the underlying data, they do not modify the storage structure of the data.
What makes sequences stand apart is they are not data structures per se; rather, they are a data abstraction over a stream of data. The data may be produced by an algorithm or a data source connected to an I/O operation. For example, the resultset-seq
function accepts a java.sql.ResultSet
JDBC instance as an argument and produces lazily realized rows of data as seq
.
Clojure data structures can be turned into sequences using the seq
function. For example, (seq [:a :b :c :d]
) returns a sequence. Calling seq
over an empty...