concept sequence in category clojure

appears as: sequence, sequence, Sequences, sequence, A sequence, sequences,  sequence,  sequences
Clojure: The Essential Reference

This is an excerpt from Manning's book Clojure: The Essential Reference.

An interesting cond->> usage is similar to the non-conditional ->> thread-last macro but with optional steps. Sequences are a good fit for pipeline-like processing because they take their input data at the end. cond->> ads the conditionals for enhanced flexibility. Here’s for example a function for a hypothetical audio signal processing application:

  • They are also cached: the first access to the sequence elements produces a cached version of each item. Subsequent access to the sequence does not require further computation.
  • They often apply "chunking" to improve performance. Chunking consists of processing a few more elements than requested, assuming the caller will soon move forward and access the rest of the sequence.
  • Clojure makes large use of sequences and as a consequence there are many functions devoted to them. The book dedicates two chapters to the topic, one about the way sequences are produced and another about their processing. The following diagram shows producers and consumers of sequences in the standard library.

    Figure 9.1. The different built-in facilities to create sequences.
    28 sequence generators

    sequence has additional features. Used with a single collection argument, it works similarly to seq with the only difference being the treatment of the empty collections:

    (seq nil)
    ;; nil
    (sequence nil)  ; #1
    ;; ()
    (seq [])
    ;; nil
    (sequence [])   ; #2
    ;; ()

    After the addition of transducers to the standard library, sequence also offers the possibility to apply a transducer:

    (sequence (map str) [1 2 3] [:a :b :c]) ; #1
    ;; ("1:a" "2:b" "3:c")

    sequence is also the only transducer-aware function to support multiple collection inputs:

    The Joy of Clojure, Second Edition

    This is an excerpt from Manning's book The Joy of Clojure, Second Edition.

    In Clojure, you can use the apply function to send a sequence of numbers to a function as if they were sent as arguments:

    Finding items in a sequence using a set and the some function

    As we’ll explain in section 5.5.3, trying to find a value in a vector using the contains? function doesn’t work the way we’d hope. Instead, the some function takes a predicate and a sequence. It applies the predicate to each element in turn, returning the first truthy value returned by the predicate or else nil:

    (some #{:b} [:a 1 :b 2])
    ;=> :b
    
    (some #{1 :b} [:a 1 :b 2])
    ;=> 1

    Using a set as the predicate supplied to some allows you to check whether any of the truthy values in the set are contained within the given sequence. This is a frequently used Clojure idiom for searching for containment within a sequence.

    A sequence is a sequential collection that represents a series of values that may or may not exist yet. They may be values from a concrete collection or values that are computed as necessary. A sequence may also be empty.

    Clojure has a simple API called seq for navigating collections. It consist of two functions: first and rest. If the collection has anything in it, (first coll) returns the first element; otherwise it returns nil. (rest coll) returns a sequence of the items other than the first. If there are no other items, rest returns an empty sequence and never nil. As you may recall from sections 3.1 and 3.2, the way that Clojure treats nil versus empty collections motivates the iteration patterns used in the language. Clojure functions that promise to return sequences, such as map and filter, work the same way as rest. A seq is any object that implements the seq API, thereby supporting the functions first and rest. You might consider it an immutable variant of an enumerator or iterator where the lack of internal state allows near limitless concurrent and parallel iteration over the same seq.

    Figure 6.5. Internal structure of qsort. Each filter and remove lazily returns items from its parent sequence only as required. So, to return the first two items of the seq returned by qsort, no remove steps are required from either level A or level B. To generate the sequence (4), a single remove step at level B is needed to eliminate everything less than 3. As more items are forced from the seq returned by qsort, more of the internal filter and remove steps are run.