From a12222eac5584e0f2a8c31e770b41aebe65feea9 Mon Sep 17 00:00:00 2001 From: Stephen Rudolph Date: Fri, 12 Feb 2016 16:16:54 -0600 Subject: [PATCH 1/4] Persistent queues no longer treated as lists --- src/clj/com/rpl/specter/impl.cljx | 10 +++++++- test/com/rpl/specter/core_test.cljx | 38 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/clj/com/rpl/specter/impl.cljx b/src/clj/com/rpl/specter/impl.cljx index 55ee39b..9542a7f 100644 --- a/src/clj/com/rpl/specter/impl.cljx +++ b/src/clj/com/rpl/specter/impl.cljx @@ -505,9 +505,17 @@ (defn all-select [structure next-fn] (into [] (r/mapcat next-fn structure))) +#+cljs +(defn queue? [coll] + (= (type coll) (type cljs.core.PersistentQueue/EMPTY))) + +#+clj +(defn queue? [coll] + (= (type coll) (type clojure.lang.PersistentQueue/EMPTY))) + (defn all-transform [structure next-fn] (let [empty-structure (empty structure)] - (cond (list? empty-structure) + (cond (and (list? empty-structure) (not (queue? empty-structure))) ;; this is done to maintain order, otherwise lists get reversed (doall (map next-fn structure)) diff --git a/test/com/rpl/specter/core_test.cljx b/test/com/rpl/specter/core_test.cljx index 596bc93..5f1b745 100644 --- a/test/com/rpl/specter/core_test.cljx +++ b/test/com/rpl/specter/core_test.cljx @@ -740,3 +740,41 @@ (is (= [2 3 [[[4]] :b 0] {:a 4 :b 10}] (s/transform [CustomWalker number?] inc [1 2 [[[3]] :b -1] {:a 3 :b 10}]))) ) + +#+cljs +(defn make-queue [coll] + (reduce + #(conj %1 %2) + cljs.core.PersistentQueue/EMPTY + coll)) + +#+clj +(defn make-queue [coll] + (reduce + #(conj %1 %2) + clojure.lang.PersistentQueue/EMPTY + coll)) + +(defspec transform-idempotency + (for-all+ + [v1 (gen/vector gen/int) + l1 (gen/list gen/int) + m1 (gen/map gen/keyword gen/int)] + (let [s1 (set v1) + q1 (make-queue v1) + v2 (s/transform s/ALL identity v1) + m2 (s/transform s/ALL identity m1) + s2 (s/transform s/ALL identity s1) + l2 (s/transform s/ALL identity l1) + q2 (s/transform s/ALL identity q1)] + (and + (= v1 v2) + (= (type v1) (type v2)) + (= m1 m2) + (= (type m1) (type m2)) + (= s1 s2) + (= (type s1) (type s2)) + (= l1 l2) + (seq? l2) ;; Transformed lists are only guaranteed to impelment ISeq + (= q1 q2) + (= (type q1) (type q2)))))) \ No newline at end of file From 1b26aaff1b4869461de0d4892d907737f79c236c Mon Sep 17 00:00:00 2001 From: Stephen Rudolph Date: Fri, 12 Feb 2016 20:27:32 -0600 Subject: [PATCH 2/4] Fixed CLJS queue usage --- src/clj/com/rpl/specter/impl.cljx | 2 +- test/com/rpl/specter/core_test.cljx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clj/com/rpl/specter/impl.cljx b/src/clj/com/rpl/specter/impl.cljx index 9542a7f..bd90edb 100644 --- a/src/clj/com/rpl/specter/impl.cljx +++ b/src/clj/com/rpl/specter/impl.cljx @@ -507,7 +507,7 @@ #+cljs (defn queue? [coll] - (= (type coll) (type cljs.core.PersistentQueue/EMPTY))) + (= (type coll) (type #queue []))) #+clj (defn queue? [coll] diff --git a/test/com/rpl/specter/core_test.cljx b/test/com/rpl/specter/core_test.cljx index 5f1b745..5810b5f 100644 --- a/test/com/rpl/specter/core_test.cljx +++ b/test/com/rpl/specter/core_test.cljx @@ -745,7 +745,7 @@ (defn make-queue [coll] (reduce #(conj %1 %2) - cljs.core.PersistentQueue/EMPTY + #queue [] coll)) #+clj From 095fc00319a741fabcbacf45a00e0f380ab08ceb Mon Sep 17 00:00:00 2001 From: Stephen Rudolph Date: Fri, 12 Feb 2016 20:29:40 -0600 Subject: [PATCH 3/4] Added newline back --- test/com/rpl/specter/core_test.cljx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/com/rpl/specter/core_test.cljx b/test/com/rpl/specter/core_test.cljx index 5810b5f..5f8f431 100644 --- a/test/com/rpl/specter/core_test.cljx +++ b/test/com/rpl/specter/core_test.cljx @@ -777,4 +777,4 @@ (= l1 l2) (seq? l2) ;; Transformed lists are only guaranteed to impelment ISeq (= q1 q2) - (= (type q1) (type q2)))))) \ No newline at end of file + (= (type q1) (type q2)))))) From ddea0a223dce38becf162e738b4d575e07f31cce Mon Sep 17 00:00:00 2001 From: Stephen Rudolph Date: Fri, 26 Feb 2016 17:10:09 -0600 Subject: [PATCH 4/4] Moving CLJS code to use transformers instead of reducers --- src/clj/com/rpl/specter/impl.cljx | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/clj/com/rpl/specter/impl.cljx b/src/clj/com/rpl/specter/impl.cljx index bd90edb..2fb618e 100644 --- a/src/clj/com/rpl/specter/impl.cljx +++ b/src/clj/com/rpl/specter/impl.cljx @@ -8,7 +8,7 @@ [select* transform* collect-val]]) (:require [com.rpl.specter.protocols :as p] [clojure.walk :as walk] - [clojure.core.reducers :as r] + #+clj [clojure.core.reducers :as r] [clojure.string :as s] #+clj [com.rpl.specter.defhelpers :as dh] ) @@ -502,9 +502,18 @@ (assoc structure akey (next-fn (get structure akey)) )) +#+clj (defn all-select [structure next-fn] (into [] (r/mapcat next-fn structure))) +#+cljs +(defn next-fn-mapcat-transformation [next-fn] + (mapcat #(next-fn %1))) + +#+cljs +(defn all-select [structure next-fn] + (into [] (next-fn-mapcat-transformation next-fn) structure)) + #+cljs (defn queue? [coll] (= (type coll) (type #queue []))) @@ -513,6 +522,7 @@ (defn queue? [coll] (= (type coll) (type clojure.lang.PersistentQueue/EMPTY))) +#+clj (defn all-transform [structure next-fn] (let [empty-structure (empty structure)] (cond (and (list? empty-structure) (not (queue? empty-structure))) @@ -526,6 +536,19 @@ (->> structure (r/map next-fn) (into empty-structure)) ))) +#+cljs +(defn next-fn-map-transformation [next-fn] + (map #(next-fn %1))) + +#+cljs +(defn all-transform [structure next-fn] + (let [empty-structure (empty structure)] + (if (and (list? empty-structure) (not (queue? empty-structure))) + ;; this is done to maintain order, otherwise lists get reversed + (doall (map next-fn structure)) + (into empty-structure (next-fn-map-transformation next-fn) structure) + ))) + (deftype AllStructurePath []) (extend-protocol p/StructurePath