public
Last active

  • Download Gist
default-map.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
(comment
(:b (DefaultMap. :foo {:a 1}))
; => :foo
(:a (DefaultMap. :foo {:a 1}))
; => 1
(merge-with conj (DefaultMap. [] {}) {:a 1} {:a 2} {:a 3})
; => {:a [1 2 3]}
)
 
;;; method implementations basically taken from clojure.core/emit-defrecord
(deftype DefaultMap [default __map]
Object
(hashCode [this] (reduce hash-combine
(keyword (str *ns*) "DefaultMap")
default
__map))
(equals [this other]
(boolean (or (identical? this other)
(when (identical? (class this) (class other))
(every? true? (map = [default __map]
[(.default other)
(.__map other)]))))))
clojure.lang.IObj
(meta [this] (.meta __map))
(withMeta [this m]
(DefaultMap. default (.withMeta __map m)))
clojure.lang.ILookup
(valAt [this k] (.valAt __map k default))
(valAt [this k else] (.valAt __map k else))
clojure.lang.IKeywordLookup
(getLookupThunk [this k]
(reify clojure.lang.ILookupThunk
(get [thunk target]
(if (identical? (class target) (class this))
(.valAt this k)))))
clojure.lang.IPersistentMap
(count [this] (.count __map))
(empty [this] (DefaultMap. default (.empty __map)))
(cons [this e] (DefaultMap. default (.cons __map e)))
(equiv [this o] (.equals this o))
(containsKey [this k] true)
(entryAt [this k] (.entryAt __map k))
(seq [this] (.seq __map))
(assoc [this k v]
(DefaultMap. default (.assoc __map k v)))
(without [this k]
(DefaultMap. default (.without __map k))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.