liveBook · Manning

Rsubseq

As explained in Clojure: The Essential Reference

rsubseq is a Clojure function that returns a sequence of elements within specified bounds from a sorted collection in reverse order.

rsubseq returns a sequence of elements selected from a sorted collection, generating matches in reverse order—from the last matching element backward. It supports one bound or lower and upper bounds. section 9.1.3

Use the function with these contracts:

  • Pass a sorted-set or sorted-map implementing clojure.lang.Sorted. section 9.1.3
  • Use <, <=, >, or >= as the comparator; make each boundary key comparable with the collection contents. section 9.1.3
  • Call (rsubseq sc test key) for one bound or (rsubseq sc start-test start-key end-test end-key) for two bounds. section 9.1.3
  • Expect ClassCastException when the collection is not sorted or a boundary key is not comparable. section 9.1.3

Reverse range selection

(rsubseq (apply sorted-map (range 10)) <= 5) ;; ([4 5] [2 3] [0 1]) (rsubseq (apply sorted-set (range 10)) > 2 < 8) ;; (7 6 5 4 3)

Finding the greatest matching element

(defn greatest< [coll x] (first (rsubseq coll < x))) (def coll (sorted-map "a" 5 "f" 23 "z" 12 "g" 1 "b" 0)) (greatest< coll "f") ;; ["b" 0]

Listing 9.3. → Sequential selection, Searching, Range selection
(subseq
  ([sc test key])
  ([sc start-test start-key end-test end-key]))

(rsubseq
  ([sc test key])
  ([sc start-test start-key end-test end-key]))

Browse the concise arity listing for subseq and rsubseq before applying their bound syntax. section 9.1.3

How Clojure: The Essential Reference covers it

How deep it goes
Explains inputs, comparators, arities, output order, exceptions, searches, and range examples without detailing tree internals.
Examples
Demonstrates reverse bounds, greatest-key searches, sorted maps, sorted sets, and word autocomplete with subseq.
What it assumes
Assumes familiarity with Clojure sequences, sorted-set, sorted-map, comparators, and comparable keys.
What you can do after
Apply it via Clojure’s sorted collections to search backward or retrieve elements within comparator-defined bounds.
How it connects
Connects sorted collections, sorted-set, sorted-map, subseq, sequences, comparators, and range selection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.