liveBook · Manning

Disj

As explained in Clojure: The Essential Reference

disj is a Clojure function that returns a set with one or more specified elements removed.

disj returns a set with one or more specified elements removed. It accepts a set followed by zero or more removal values and supports both ordinary and sorted sets. section 13.4 section 13.0

Its contract includes these practical cases:

  • With only the set argument, disj returns that argument unchanged, including nil. section 13.4
  • With additional arguments, the input must implement IPersistentSet; otherwise disj can throw ClassCastException. section 13.4
  • Removal values may have any type, and missing values simply leave the set unchanged. section 13.4

Removing several elements

(disj #{1 4 6 8} 4 8) ;; #{1 6} (disj (sorted-set-by > 1 4 6) 4) ;; #{6 1}

Detecting unwanted values

Convert the values to a set, remove every allowed value, and test whether anything remains: (defn valid? [allowed values] (empty? (apply disj (set values) allowed))) (valid? [:a :b :c] [:c :c 1 :a]) ;; false (valid? [:a :b :c] [:c :c :a]) ;; true

How Clojure: The Essential Reference covers it

How deep it goes
Defines arities, accepted inputs, return behavior, and exceptions, then demonstrates validation and connection-tracking patterns.
Examples
Uses element removal, allowed-value validation, open-port tracking, and echo-server cleanup.
What it assumes
Assumes familiarity with Clojure sets, collections, atoms, and basic function application.
What you can do after
Apply disj to remove one or many values from persistent sets and compose it with validation or state-management code.
How it connects
Connects disj with sets, sorted sets, conj, into, difference, and set membership predicates.

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.