Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by
Since 1.8, we have {{map-entry?}} predicate, but no corresponding constructor function. Implementation could be as simple as:


(defn map-entry
  "Creates a new map entry with key k and value v.

  See also: map-entry?, key, val"
  [k v]
  (clojure.lang.MapEntry/create k v))

5 Answers

0 votes
by

Comment made by: mfikes

Perhaps the existence of other kinds of map entries would matter to users of such a constructor.

`
user=> (let [me (first (sorted-map :a 1))]

     [(map-entry? me) (type me)])

[true clojure.lang.PersistentTreeMap$BlackVal]
`

0 votes
by

Comment made by: gshayban

I don't see the point. What would having a map entry constructor achieve?

0 votes
by

Comment made by: alexmiller

Mike: no, I don't think that should matter

Ghadi: there is code out in the wild that invokes MapEntry/create because it's faster in some cases - this would certainly be preferable.

0 votes
by

Comment made by: jcr

Ghadi: another consideration is clj\cljs portability. Speaking of which...

Alex: should I create a ticket for clojurescript too? Seems like the body of the function would be:

(cljs.core/MapEntry. k v nil)

0 votes
by
...