Skip to content

Instantly share code, notes, and snippets.

@michalmarczyk
Created July 8, 2010 17:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalmarczyk/468332 to your computer and use it in GitHub Desktop.
Save michalmarczyk/468332 to your computer and use it in GitHub Desktop.
(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