added view and viewfn

This commit is contained in:
Nathan Marz 2015-04-18 12:16:51 -04:00
parent 0bf63ea546
commit ca1940a026
3 changed files with 23 additions and 1 deletions

View file

@ -102,6 +102,11 @@
(defn keypath [akey] (->KeyPath akey)) (defn keypath [akey] (->KeyPath akey))
(defn view [afn] (->ViewPath afn))
(defmacro viewfn [& args]
`(view (fn ~@args)))
(extend-type clojure.lang.Keyword (extend-type clojure.lang.Keyword
StructurePath StructurePath
(select* [kw structure next-fn] (select* [kw structure next-fn]

View file

@ -232,7 +232,6 @@
(key-update akey structure next-fn) (key-update akey structure next-fn)
)) ))
(deftype SelectorValsPath [sel-fn selector] (deftype SelectorValsPath [sel-fn selector]
ValPath ValPath
(select-val [this structure] (select-val [this structure]
@ -257,3 +256,11 @@
(vec res) (vec res)
res res
)))) ))))
(deftype ViewPath [view-fn]
StructurePath
(select* [this structure next-fn]
(-> structure view-fn next-fn))
(update* [this structure next-fn]
(-> structure view-fn next-fn)
))

View file

@ -210,3 +210,13 @@
(is (= 3 (select-one :b {:a 1 :b 3}))) (is (= 3 (select-one :b {:a 1 :b 3})))
(is (= 5 (select-one (comp-structure-paths :a :b) {:a {:b 5}}))) (is (= 5 (select-one (comp-structure-paths :a :b) {:a {:b 5}})))
) )
(defspec view-test
(for-all+
[i gen/int
afn (gen/elements [inc dec])]
(= (first (select (view afn) i))
(first (select (viewfn [i] (afn i)) i))
(afn i)
(update (view afn) identity i)
)))