Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

clojure
community creator

What is the map-invert method in Clojure maps?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

What is a map in Clojure?

A map is a collection that assigns keys to values.

What is a map-invert map method in Clojure?

The map-invert method is used to turn keys to values and values to keys in a map.

Syntax

(map-invert hmap)

Parameter

The map-invert method receives one parameter:

hmap: The map we are working with.

Return value

The map-invertmethod returns an inverted map.

Example

(ns clojure.examples.example
(:require [clojure.set :as set])
(:gen-class))
(defn invert []
(def example (hash-map "z" "98" "x" "90" "u" "53"))
(println (set/map-invert example)))
(invert)
map-invert method example

Explanation

Notice how in the code above the output now has an inverted map.

Line 2: We include the set package like so;(:require [clojure.set :as set])

Lines 3–4: We define the function invert.

In line 5: We use the keyword def to define a variable example and we assign our map to it.

In line 6: We use the map-invert method to invert the map example keys and values using the method as; (set/map-invert example)

In line 7: We call the invert function.

RELATED TAGS

clojure
community creator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring