Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a map entry in Clojure

Tags:

clojure

What is the built-in Clojure way (if any), to create a single map entry?

In other words, I would like something like (map-entry key value). In other words, the result should be more or less equivalent to (first {key value}).

Remarks:

  • Of course, I already tried googling, and only found map-entry? However, this document has no linked resources.
  • I know that (first {1 2}) returns [1 2], which seems a vector. However:
(class (first {1 2}))
; --> clojure.lang.MapEntry
(class [1 2])
; --> clojure.lang.PersistentVector
  • I checked in the source code, and I'm aware that both MapEntry and PersistentVector extend APersistentVector (so MapEntry is more-or-less also a vector). However, the question is still, whether I can create a MapEntry instance from Clojure code.
  • Last, but not least: "no, there is no built in way to do that in Clojure" is also a valid answer (which I strongly suspect is the case, just want to make sure that I did not accidentally miss something).
like image 967
Attilio Avatar asked Jul 17 '17 19:07

Attilio


People also ask

What is a map in CL Clojure?

Clojure - Maps. A Map is a collection that maps keys to values. Two different map types are provided - hashed and sorted. HashMaps require keys that correctly support hashCode and equals. SortedMaps require keys that implement Comparable, or an instance of Comparator.

How to merge two maps into one map entry in MySQL?

Merges two maps entries into one single map entry. Following is the syntax. Parameters − ‘hmap1’ is the map of hash keys and values. ‘hmap2’ is the map of hash keys and values, which needs to be mapped with the first HashMap.

How do I extract the key/value from a map entry?

You can extract the key or value from a map entry using key or val, respectively: Note that, although all Clojure map entries are vectors, not all vectors are map entries. If you try to call key or val on anything that's not a map entry, you'll get a ClassCastException:

How to get a map with all the same key-value pairs?

You can use dissoc to get a map that has all the same key-value pairs as an existing map, with possibly one mapping removed: You can get a sequence of all entries in a map using seq:


3 Answers

"no, there is no built in way to do that in Clojure" is also a valid answer

Yeah, unfortunately that's the answer. I'd say the best you can do is define a map-entry function yourself:

(defn map-entry [k v]
  (clojure.lang.MapEntry/create k v))
like image 129
Sam Estep Avatar answered Sep 18 '22 00:09

Sam Estep


Just specify a class name as follows

(clojure.lang.MapEntry. "key" "val")

or import the class to instantiate by a short name

(import (clojure.lang MapEntry))

(MapEntry. "key" "val")
like image 29
Aleksei Sotnikov Avatar answered Sep 18 '22 00:09

Aleksei Sotnikov


As Rich Hickey says here: "I make no promises about the continued existence of MapEntry. Please don't use it." You should not attempt to directly instantiate an implementation class such clojure.lang.MapEntry. It's better to just use:

(defn map-entry [k v] (first {k v}))
like image 37
Kalle Avatar answered Sep 20 '22 00:09

Kalle