liveBook · Manning

Dissoc-in

As explained in Clojure: The Essential Reference

dissoc-in is a function that removes a value at a path of keys from a nested associative data structure.

dissoc-in removes the value at a path of keys or indexes from nested associative data. Clojure has no standard dissoc-in, so it must be defined when nested removal is needed. section 11.3.1

Removing a nested map entry

(def m {:a {:b 2 :c {:d 4 :e 5}}}) (dissoc-in m [:a :c :d]) ;; {:a {:b 2, :c {:e 5}}} section 11.3.1

A practical implementation should account for these behaviors:

  • A simple recursive version works for maps by descending through the path and applying dissoc at the final key. section 11.3.1
  • A map-only version fails when the final collection is a vector; vector removal requires removing the indexed element rather than calling dissoc. section 11.3.1
  • A robust version dispatches on map? and vector?, using remove-at with subvec for vector paths. section 11.3.1
Listing 11.9. → Adding keys, Replacing values, Removing keys
(assoc
 ([map key val])
 ([map key val & kvs]))

(assoc-in
  [map ks val])

(dissoc
  ([map])
  ([map key])
  ([map key & ks]))

Compare dissoc-in with the related shallow and nested associative-update functions. section 11.3.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines recursive map handling and vector-aware removal, while leaving malformed paths and broader validation unspecified.
Examples
Uses nested maps, mixed map-vector structures, remove-at, subvec, and a ClassCastException from map-only removal.
What it assumes
Assumes familiarity with Clojure maps, vectors, dissoc, assoc, update-in, get, subvec, and recursive functions.
What you can do after
Build it from scratch using recursive descent and type-specific removal for maps and vectors.
How it connects
Connects dissoc-in with dissoc, assoc-in, get-in, update-in, subvec, and persistent vectors.

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.