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
(:use [com.rpl.specter.protocols :only [StructurePath comp-paths*]])
(:use [com.rpl.specter.protocols :only [StructurePath]])
(: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
;;beforehand
(defn comp-paths [& paths]
(comp-paths* (vec paths)))
(i/comp-paths* (vec paths)))
;; Selector functions
@ -130,7 +130,7 @@
(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))
@ -141,7 +141,7 @@
e.g. (selected? :vals ALL even?) keeps the current element only if an
even number exists for the :vals key"
[& selectors]
(let [s (comp-paths* selectors)]
(let [s (i/comp-paths* selectors)]
(fn [structure]
(->> structure
(select s)
@ -149,13 +149,13 @@
not))))
(defn not-selected? [& path]
(complement (selected? (comp-paths* path))))
(complement (selected? (i/comp-paths* path))))
(defn transformed
"Navigates to a view of the current value by transforming it with the
specified selector and update-fn."
[selector update-fn]
(let [compiled (comp-paths* selector)]
(let [compiled (i/comp-paths* selector)]
(view
(fn [elem]
(compiled-transform compiled update-fn elem)
@ -184,10 +184,10 @@
(i/filter-transform aset structure next-fn)))
(defn collect [& selector]
(i/->SelectCollector select (comp-paths* selector)))
(i/->SelectCollector select (i/comp-paths* selector)))
(defn collect-one [& selector]
(i/->SelectCollector select-one (comp-paths* selector)))
(i/->SelectCollector select-one (i/comp-paths* selector)))
(defn putval
"Adds an external value to the collected vals. Useful when additional arguments
@ -208,7 +208,7 @@
[& conds]
(->> conds
(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
i/->ConditionalPath
))
@ -223,4 +223,4 @@
"A path that branches on multiple paths. For updates,
applies updates to the paths in order."
[& 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
:refer [mk-optimized-invocation]]))
(:use [com.rpl.specter.protocols :only
[comp-paths*
select* transform* collect-val select-full* transform-full*]])
[select* transform* collect-val]])
(:require [com.rpl.specter.protocols :as p]
[clojure.walk :as walk]
[clojure.core.reducers :as r]
[clojure.string :as s])
)
(defprotocol StructureValsPathComposer
(comp-paths* [paths]))
#?(
:clj
(do
@ -97,9 +99,6 @@
(defn collector-impl [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]
{: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]
(let [cfn (->> this
collector-impl
@ -200,7 +182,6 @@
(coerce-path [this]
(cond (structure-path? this) (coerce-structure-path 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))
)))
@ -251,7 +232,7 @@
(transformer structure (fn [structure] (next-fn vals structure))))
))))
(extend-protocol p/StructureValsPathComposer
(extend-protocol StructureValsPathComposer
nil
(comp-paths* [sp]
(coerce-path sp))
@ -275,7 +256,6 @@
(defn coerce-structure-vals-direct [this]
(cond (structure-path? this) (coerce-structure-path-direct this)
(satisfies? p/Collector this) (coerce-collector this)
(satisfies? p/StructureValsPath this) (coerce-structure-vals-path this)
(instance? TransformFunctions this) (coerce-structure-vals this)
:else (throw-illegal (no-prot-error-str this))
))

View file

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

View file

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