Skip to content

@michalmarczyk /default-map.clj
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
(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))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.