remove unnecessary StructureValsPath protocol and move path composer protocol into impl

This commit is contained in:
Nathan Marz 2015-09-09 15:20:58 -04:00
parent 33e0055372
commit f800a510dd
4 changed files with 17 additions and 44 deletions

View file

@ -1,5 +1,5 @@
(ns com.rpl.specter (ns com.rpl.specter
(:use [com.rpl.specter.protocols :only [StructurePath comp-paths*]]) (:use [com.rpl.specter.protocols :only [StructurePath]])
(:require [com.rpl.specter.impl :as i]) (:require [com.rpl.specter.impl :as i])
) )
@ -8,7 +8,7 @@
;;all over the place. The apply to the vals + structure can also be avoided since the number of vals is known ;;all over the place. The apply to the vals + structure can also be avoided since the number of vals is known
;;beforehand ;;beforehand
(defn comp-paths [& paths] (defn comp-paths [& paths]
(comp-paths* (vec paths))) (i/comp-paths* (vec paths)))
;; Selector functions ;; Selector functions
@ -130,7 +130,7 @@
(defn codewalker [afn] (i/->CodeWalkerStructurePath afn)) (defn codewalker [afn] (i/->CodeWalkerStructurePath afn))
(defn filterer [& path] (i/->FilterStructurePath (comp-paths* path))) (defn filterer [& path] (i/->FilterStructurePath (i/comp-paths* path)))
(defn keypath [akey] (i/->KeyPath akey)) (defn keypath [akey] (i/->KeyPath akey))
@ -141,7 +141,7 @@
e.g. (selected? :vals ALL even?) keeps the current element only if an e.g. (selected? :vals ALL even?) keeps the current element only if an
even number exists for the :vals key" even number exists for the :vals key"
[& selectors] [& selectors]
(let [s (comp-paths* selectors)] (let [s (i/comp-paths* selectors)]
(fn [structure] (fn [structure]
(->> structure (->> structure
(select s) (select s)
@ -149,13 +149,13 @@
not)))) not))))
(defn not-selected? [& path] (defn not-selected? [& path]
(complement (selected? (comp-paths* path)))) (complement (selected? (i/comp-paths* path))))
(defn transformed (defn transformed
"Navigates to a view of the current value by transforming it with the "Navigates to a view of the current value by transforming it with the
specified selector and update-fn." specified selector and update-fn."
[selector update-fn] [selector update-fn]
(let [compiled (comp-paths* selector)] (let [compiled (i/comp-paths* selector)]
(view (view
(fn [elem] (fn [elem]
(compiled-transform compiled update-fn elem) (compiled-transform compiled update-fn elem)
@ -184,10 +184,10 @@
(i/filter-transform aset structure next-fn))) (i/filter-transform aset structure next-fn)))
(defn collect [& selector] (defn collect [& selector]
(i/->SelectCollector select (comp-paths* selector))) (i/->SelectCollector select (i/comp-paths* selector)))
(defn collect-one [& selector] (defn collect-one [& selector]
(i/->SelectCollector select-one (comp-paths* selector))) (i/->SelectCollector select-one (i/comp-paths* selector)))
(defn putval (defn putval
"Adds an external value to the collected vals. Useful when additional arguments "Adds an external value to the collected vals. Useful when additional arguments
@ -208,7 +208,7 @@
[& conds] [& conds]
(->> conds (->> conds
(partition 2) (partition 2)
(map (fn [[c p]] [(comp-paths* c) (comp-paths* p)])) (map (fn [[c p]] [(i/comp-paths* c) (i/comp-paths* p)]))
doall doall
i/->ConditionalPath i/->ConditionalPath
)) ))
@ -223,4 +223,4 @@
"A path that branches on multiple paths. For updates, "A path that branches on multiple paths. For updates,
applies updates to the paths in order." applies updates to the paths in order."
[& paths] [& paths]
(i/->MultiPath (->> paths (map comp-paths*) doall))) (i/->MultiPath (->> paths (map i/comp-paths*) doall)))

View file

@ -2,14 +2,16 @@
#?(:cljs (:require-macros [com.rpl.specter.prot-opt-invoke #?(:cljs (:require-macros [com.rpl.specter.prot-opt-invoke
:refer [mk-optimized-invocation]])) :refer [mk-optimized-invocation]]))
(:use [com.rpl.specter.protocols :only (:use [com.rpl.specter.protocols :only
[comp-paths* [select* transform* collect-val]])
select* transform* collect-val select-full* transform-full*]])
(:require [com.rpl.specter.protocols :as p] (:require [com.rpl.specter.protocols :as p]
[clojure.walk :as walk] [clojure.walk :as walk]
[clojure.core.reducers :as r] [clojure.core.reducers :as r]
[clojure.string :as s]) [clojure.string :as s])
) )
(defprotocol StructureValsPathComposer
(comp-paths* [paths]))
#?( #?(
:clj :clj
(do (do
@ -97,9 +99,6 @@
(defn collector-impl [this] (defn collector-impl [this]
(find-protocol-impl! p/Collector this)) (find-protocol-impl! p/Collector this))
(defn structure-vals-path-impl [this]
(find-protocol-impl! p/StructureValsPath this))
)) ))
@ -113,25 +112,8 @@
(defn collector-impl [obj] (defn collector-impl [obj]
{:collect-val (mk-optimized-invocation p/Collector obj collect-val 1) {:collect-val (mk-optimized-invocation p/Collector obj collect-val 1)
}) })
(defn structure-vals-path-impl [obj]
{:select-full* (mk-optimized-invocation p/StructureValsPath obj select-full* 3)
:transform-full* (mk-optimized-invocation p/StructureValsPath obj transform-full* 3)
})
)) ))
(defn coerce-structure-vals-path [this]
(let [pimpl (structure-vals-path-impl this)
selector (:select-full* pimpl)
transformer (:transform-full* pimpl)]
(->TransformFunctions
StructureValsPathExecutor
(fn [vals structure next-fn]
(selector this vals structure next-fn))
(fn [vals structure next-fn]
(transformer this vals structure next-fn)))
))
(defn coerce-collector [this] (defn coerce-collector [this]
(let [cfn (->> this (let [cfn (->> this
collector-impl collector-impl
@ -200,7 +182,6 @@
(coerce-path [this] (coerce-path [this]
(cond (structure-path? this) (coerce-structure-path this) (cond (structure-path? this) (coerce-structure-path this)
(satisfies? p/Collector this) (coerce-collector this) (satisfies? p/Collector this) (coerce-collector this)
(satisfies? p/StructureValsPath this) (coerce-structure-vals-path this)
:else (throw-illegal (no-prot-error-str this)) :else (throw-illegal (no-prot-error-str this))
))) )))
@ -251,7 +232,7 @@
(transformer structure (fn [structure] (next-fn vals structure)))) (transformer structure (fn [structure] (next-fn vals structure))))
)))) ))))
(extend-protocol p/StructureValsPathComposer (extend-protocol StructureValsPathComposer
nil nil
(comp-paths* [sp] (comp-paths* [sp]
(coerce-path sp)) (coerce-path sp))
@ -275,7 +256,6 @@
(defn coerce-structure-vals-direct [this] (defn coerce-structure-vals-direct [this]
(cond (structure-path? this) (coerce-structure-path-direct this) (cond (structure-path? this) (coerce-structure-path-direct this)
(satisfies? p/Collector this) (coerce-collector this) (satisfies? p/Collector this) (coerce-collector this)
(satisfies? p/StructureValsPath this) (coerce-structure-vals-path this)
(instance? TransformFunctions this) (coerce-structure-vals this) (instance? TransformFunctions this) (coerce-structure-vals this)
:else (throw-illegal (no-prot-error-str this)) :else (throw-illegal (no-prot-error-str this))
)) ))

View file

@ -1,9 +1,5 @@
(ns com.rpl.specter.protocols) (ns com.rpl.specter.protocols)
(defprotocol StructureValsPath
(select-full* [this vals structure next-fn])
(transform-full* [this vals structure next-fn]))
(defprotocol StructurePath (defprotocol StructurePath
(select* [this structure next-fn]) (select* [this structure next-fn])
(transform* [this structure next-fn])) (transform* [this structure next-fn]))
@ -11,5 +7,3 @@
(defprotocol Collector (defprotocol Collector
(collect-val [this structure])) (collect-val [this structure]))
(defprotocol StructureValsPathComposer
(comp-paths* [paths]))

View file

@ -6,8 +6,7 @@
(:use (:use
#?(:clj [clojure.test :only [deftest is]]) #?(:clj [clojure.test :only [deftest is]])
#?(:clj [clojure.test.check.clojure-test :only [defspec]]) #?(:clj [clojure.test.check.clojure-test :only [defspec]])
#?(:clj [com.rpl.specter.test-helpers :only [for-all+]]) #?(:clj [com.rpl.specter.test-helpers :only [for-all+]]))
[com.rpl.specter.protocols :only [comp-paths*]])
(:require #?@(:clj [[clojure.test.check.generators :as gen] (:require #?@(:clj [[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]] [clojure.test.check.properties :as prop]]
:cljs [[cljs.test.check :as tc] :cljs [[cljs.test.check :as tc]
@ -252,7 +251,7 @@
(= (afn i) (s/transform nil afn i))))) (= (afn i) (s/transform nil afn i)))))
(deftest nil-comp-test (deftest nil-comp-test
(is (= [5] (s/select (comp-paths* nil) 5)))) (is (= [5] (s/select (com.rpl.specter.impl/comp-paths* nil) 5))))
(defspec putval-test (defspec putval-test
(for-all+ (for-all+