What is the dissoc maps method in Clojure?

What is a map in Clojure?

A map is a collection that assigns keys to values.

What is the dissoc map method in Clojure?

The dissoc method is used to remove key-value pairs from a map.

Syntax

(dissoc hmap key)

Parameter

The dissoc method receives the following parameters:

  • hmap: The map we are working with.
  • key: The key used to get the key-value pair that is being removed.

Return value

The dissoc method returns the map without the removed key-value pairs.

Example

(ns clojure.examples.example
(:gen-class))
(defn diss []
(def example (hash-map "z" "98" "x" "90" "u" "53"))
(println (dissoc example "x")))
(diss)

Explanation

  • Line 3: We define a function diss.
  • Line 4: We define a variable example using the keyword def and we assign our map to it.
  • Line 5: We remove a key-value that has key x from the example map using the method dissoc. We then print the output using the println method.
  • Line 6: We call the diss function.

We can see from the output that the map now has two key-value pairs instead of three.

Free Resources