Book Image

Clojure High Performance Programming, Second Edition - Second Edition

By : Shantanu Kumar
Book Image

Clojure High Performance Programming, Second Edition - Second Edition

By: Shantanu Kumar

Overview of this book

Table of Contents (15 chapters)
Clojure High Performance Programming Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 collection returns nil.

Sequences...