concept sort - set in category clojure

appears as: sorted-set, sorted-set, sorted-sets
Clojure: The Essential Reference

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

  • sets: similarly to hash-maps, they have no notion of ordering (see sorted-set for that purpose), so the same uncertainty factor applies:
  • sorted-set is the ordered version of a set. The ordering is based on a comparator. The comparator is used to establish the relative ordering of the items in the set.
  • sorted-set is useful to maintain order as the set gets updated incrementally, as each new item is inserted in order following the comparator. In the next example we let users update a core dictionary of words with their own custom words and spellings. If we store the dictionary in a sorted-set there is no need to sort after each update:

    (require '[clojure.string :refer [split-lines]])
    
    (def dict ; #1
      (atom
        (->> "/usr/share/dict/words"
          slurp
          split-lines
          (into (sorted-set)))))
    
    (defn new-word [w] ; #2
      (println "Could not find the word:" w)
      (println "Add word to dictionary? [y/n]")
      (when (= "y" (read-line))
        (swap! dict conj w)
        (take 5 (subseq @dict >= w)))) ; #3
    
    (defn spell-check [w]
      (if (contains? @dict w) ; #4
        (println "Word spelled correctly")
        (new-word w)))
    
    (defn ask-word []
      (println "Please type word:")
      (when-let [w (read-line)]
        (spell-check w)))
    
    (ask-word) ; #5
    ;; Please type word:
    ;; google
    ;; Could not find the word: google
    ;; Add word to dictionary? [y/n]
    ;; y
    ;; word added (google googly googol googolplex googul)
    
    (ask-word)
    ;; Please type word:
    ;; google
    ;; Word spelled correctly