Address PR comments

* Behave the same as select-keys for non-existent keys
 * Remove variadic input redirection
 * Use reduce to remove data instead of apply
This commit is contained in:
Beau Fabry 2016-04-19 17:24:00 -07:00
parent b3c707092e
commit eb20e86f9c
2 changed files with 13 additions and 17 deletions

View file

@ -218,24 +218,20 @@
))) )))
(defpath (defpath
submap* ^{:doc "Navigates to the specified submap (using select-keys).
In a transform, that submap in the original map is changed to the new
value of the submap."}
submap
[m-keys] [m-keys]
(select* [this structure next-fn] (select* [this structure next-fn]
(next-fn (merge (zipmap m-keys (repeat nil)) (select-keys structure m-keys)))) (next-fn (select-keys structure m-keys)))
(transform* [this structure next-fn] (transform* [this structure next-fn]
(let [submap (merge (zipmap m-keys (repeat nil)) (select-keys structure m-keys)) (let [submap (select-keys structure m-keys)
newmap (next-fn submap)] newmap (next-fn submap)]
(merge (apply dissoc structure m-keys) (merge (reduce dissoc structure m-keys)
newmap)))) newmap))))
(defn submap
"Navigates to the specified submap (by attempting a get for each key).
In a transform, that submap in the original map is changed to the new
value of the submap."
[& m-keys]
(submap* m-keys))
(defpath (defpath
walker walker
[afn] [afn]

View file

@ -574,16 +574,16 @@
)))) ))))
(deftest submap-test (deftest submap-test
(is (= [{:foo 1, :baz nil}] (is (= [{:foo 1}]
(s/select [(s/submap :foo :baz)] {:foo 1 :bar 2}))) (s/select [(s/submap [:foo :baz])] {:foo 1 :bar 2})))
(is (= {:foo 1, :barry 1} (is (= {:foo 1, :barry 1}
(s/setval [(s/submap :bar)] {:barry 1} {:foo 1 :bar 2}))) (s/setval [(s/submap [:bar])] {:barry 1} {:foo 1 :bar 2})))
(is (= {:bar 1, :foo 2, :baz nil} (is (= {:bar 1, :foo 2}
(s/transform [(s/submap :foo :baz) s/ALL s/LAST (comp not nil?)] inc {:foo 1 :bar 1}))) (s/transform [(s/submap [:foo :baz]) s/ALL s/LAST] inc {:foo 1 :bar 1})))
(is (= {:a {:new 1} (is (= {:a {:new 1}
:c {:new 1 :c {:new 1
:old 1}} :old 1}}
(s/setval [s/ALL s/LAST (s/submap)] {:new 1} {:a nil, :c {:old 1}})))) (s/setval [s/ALL s/LAST (s/submap [])] {:new 1} {:a nil, :c {:old 1}}))))
(deftest nil->val-test (deftest nil->val-test
(is (= {:a #{:b}} (is (= {:a #{:b}}