From ddea0a223dce38becf162e738b4d575e07f31cce Mon Sep 17 00:00:00 2001 From: Stephen Rudolph Date: Fri, 26 Feb 2016 17:10:09 -0600 Subject: [PATCH] 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