Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ztellman/potemkin
Browse files Browse the repository at this point in the history
  • Loading branch information
ztellman committed Sep 16, 2014
2 parents 93af330 + cd4d9c9 commit 93c9d97
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ classes
target/**
push
.DS_Store
.nrepl*
.nrepl*
.idea
*.iml
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
(cons (:tag %) (keys %))))
:benchmark :benchmark
:all (constantly true)}
:aot [potemkin.LazyMapEntry]
:jvm-opts ^:replace ["-server"]
:repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"})
12 changes: 12 additions & 0 deletions src/potemkin/LazyMapEntry.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns potemkin.LazyMapEntry
(:import (clojure.lang ILookup MapEntry))
(:gen-class :extends clojure.lang.MapEntry
:init init
:constructors {[clojure.lang.ILookup Object] [Object Object]}
:state state))

(defn -init [map k]
[[k nil] map])

(defn -val [^potemkin.LazyMapEntry this]
(.valAt ^ILookup (.state this) (.key this)))
7 changes: 3 additions & 4 deletions src/potemkin/collections.clj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
(seq [this]
(seq
(map
#(clojure.lang.MapEntry. % (.valAt this % nil))
#(potemkin.LazyMapEntry. this %)
(potemkin.collections/keys* this))))

^{:min-version "1.4.0"}
Expand Down Expand Up @@ -142,9 +142,8 @@
(contains? (.keySet this) k))

(entryAt [this k]
(let [v (.valAt this k ::not-found)]
(when (not= v ::not-found)
(clojure.lang.MapEntry. k v))))
(when (contains? (.keySet this) k)
(potemkin.LazyMapEntry this k)))

(assoc [this k v]
(potemkin.collections/assoc* this k v))
Expand Down
23 changes: 23 additions & 0 deletions test/potemkin/collections_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,26 @@
(is (= {:lower "abc" :upper "ABC"} (dissoc m :string)))
(is (= {:string "foo" :lower "abc" :upper "ABC" :bar "baz"}
(assoc m :string "foo" :bar "baz")))))

(def-map-type LazyMap [m]
(get [_ k default-value]
(if (contains? m k)
(let [v (get m k)]
(if (instance? clojure.lang.Delay v)
@v
v))
default-value))
(assoc [_ k v]
(LazyMap. (assoc m k v)))
(dissoc [_ k]
(LazyMap. (dissoc m k)))
(keys [_]
(keys m)))

(deftest map-entries-are-lazy
(let [was-called (atom false)
d (delay (reset! was-called true))
m (LazyMap. {:d d})]

(doall (keys m))
(is (= false @was-called))))

0 comments on commit 93c9d97

Please sign in to comment.