Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

fix to reduce on custom maps with clojure 1.4 #3

Merged
merged 2 commits into from almost 2 years ago

2 participants

Vadim Geshel Zach Tellman
Vadim Geshel

In 1.4 reduce uses the new CollReduce protocol. This change adds implementation of this protocol to map-template in a backwards-compatible way.

Zach Tellman ztellman merged commit 503d40c into from April 24, 2012
Zach Tellman ztellman closed this April 24, 2012
Zach Tellman
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
4  project.clj
@@ -3,5 +3,7 @@
3 3
   :dependencies [[org.clojure/clojure "1.3.0"]]
4 4
   :repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"}
5 5
   :multi-deps {"1.2" [[org.clojure/clojure "1.2.1"]]
  6
+               "1.3" [[org.clojure/clojure "1.3.0"]]
6 7
                "1.4a" [[org.clojure/clojure "1.4.0-alpha4"]]
7  
-               "1.4s" [[org.clojure/clojure "1.4.0-master-SNAPSHOT"]]})
  8
+               "1.4s" [[org.clojure/clojure "1.4.0-master-SNAPSHOT"]]
  9
+               "1.4" [[org.clojure/clojure "1.4.0"]]})
40  src/potemkin/map.clj
@@ -59,8 +59,8 @@
59 59
             clojure.lang.IPersistentCollection
60 60
             (equiv [this x]
61 61
               (and
62  
-                (map? x)
63  
-                (= x (into {} this))))
  62
+               (map? x)
  63
+               (= x (into {} this))))
64 64
             (cons [this o]
65 65
               (if-let [[k v] (seq o)]
66 66
                 (assoc this k v)
@@ -74,18 +74,38 @@
74 74
             (seq [this]
75 75
               (map #(MapEntry. % (.valAt this % nil)) (potemkin.protocols/keys* this ~unwrapped-data)))
76 76
 
  77
+            ~@(when (or (> (:major *clojure-version*) 1)
  78
+                       (and (= (:major *clojure-version*) 1)
  79
+                            (>= (:minor *clojure-version*) 4)))
  80
+               `(clojure.core.protocols.CollReduce
  81
+                 (coll-reduce
  82
+                   [this f]
  83
+                   (reduce f (seq this)))
  84
+                 (coll-reduce
  85
+                   [this f val#]
  86
+                   (reduce f val# (seq this)))))
  87
+            
  88
+            ;; clojure.core.protocols.CollReduce
  89
+            ;; (coll-reduce
  90
+            ;;   [this f]
  91
+            ;;   (reduce f (seq this)))
  92
+
  93
+            ;; (coll-reduce
  94
+            ;;   [this f val#]
  95
+            ;;   (reduce f val# (seq this)))
  96
+            
77 97
             Object
78 98
             (hashCode [this]
79 99
               (reduce
80  
-                (fn [acc [k v]]
81  
-                  (unchecked-add acc (bit-xor (hash k) (hash v))))
82  
-                0
83  
-                (seq this)))
  100
+               (fn [acc [k v]]
  101
+                 (unchecked-add acc (bit-xor (hash k) (hash v))))
  102
+               0
  103
+               (seq this)))
84 104
             (equals [this x]
85 105
               (or (identical? this x)
86  
-                (and
87  
-                  (map? x)
88  
-                  (= x (into {} this)))))
  106
+                  (and
  107
+                   (map? x)
  108
+                   (= x (into {} this)))))
89 109
             (toString [this]
90 110
               (str (into {} this)))
91 111
 
@@ -159,7 +179,7 @@
159 179
             ~@(map
160 180
                 (fn [n]
161 181
                   `(invoke [this ~@(repeat n '_)]
162  
-                     ~(throw-arity n)))
  182
+                           ~(throw-arity n)))
163 183
                 (range 3 21))
164 184
 
165 185
             (applyTo [this args]
10  test/potemkin/test/map.clj
@@ -53,7 +53,15 @@
53 53
     (is (= [[:a 1]] s)))
54 54
 
55 55
   (let [m (conj m [:a 1])]
56  
-    (is (= {:a 1} m))))
  56
+    (is (= {:a 1} m)))
  57
+
  58
+  (let [m (-> m (assoc :a 1) (assoc :b 2))
  59
+        s (reduce
  60
+           (fn [s [k v]]
  61
+             (+ s v))
  62
+           0
  63
+           m)]
  64
+    (is (= 3 s))))
57 65
 
58 66
 (deftest test-maps
59 67
   (test-basic-map-functionality (SimpleMap. {}))
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.