Chapter 3
Unifying Data with Sequences

Programs manipulate data. At the lowest level, programs work with structures such as strings, lists, vectors, maps, sets, and trees. At a higher level, these same data structure abstractions crop up again and again. For example:

  • XML data is a tree.
  • Database result sets can be viewed as lists or vectors.
  • Directory hierarchies are trees.
  • Files are often viewed as one big string or as a vector of lines.

In Clojure, all these data structures can be accessed through a single abstraction: the sequence (or seq).

A seq (pronounced “seek”) is a logical list. It’s logical because Clojure does not tie sequences to the concrete implementation details of the list data structure. Instead, the seq is an abstraction that can be used everywhere.

Collections that can be viewed as seqs are called seq-able (pronounced “SEEK-a-bull”). In this chapter, you’ll meet a variety of seq-able collections:

  • All Clojure collections
  • All Java collections
  • Java arrays and strings
  • Regular expression matches
  • Directory structures
  • I/O streams
  • XML trees

You’ll also meet the sequence library, a set of functions that can work with any seq-able. Because so many things are sequences, the sequence library is much more powerful and general than the collection API in most languages. The sequence library includes functions to create, filter, and transform data. These functions act as the collection API for Clojure, and they also replace many of the loops you would write in an imperative language.

In this chapter, you will become a power user of Clojure sequences. You’ll see how to use a common set of very expressive functions with a wide range of datatypes. Then, in the next chapter (Chapter 4, Functional Programming), you’ll learn the functional style in which the sequence library is written.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.162.201