I want to check if every key given in a vector [:e [:a :b] [:c :d]] exists in a map.
{:e 2 :a {:b 3} :c {:d 5}}
I could write the following to check -
(def kvs {:e 2 :a {:b 3} :c {:d 5}})
(every? #(contains? kvs %) [[:e] [:a :b] [:c :d]])
However the above would fail as contains doesnt check the key one level deep like update-in does. How do I accomplish the above ?
An improvement on murtaza's basic approach, which also works when the map has nil or false values:
(defn contains-every? [m keyseqs]
(let [not-found (Object.)]
(not-any? #{not-found}
(for [ks keyseqs]
(get-in m ks not-found)))))
user> (contains-every? {:e 2 :a {:b 3} :c {:d 5}}
[[:e] [:a :b] [:c :d]])
true
user> (contains-every? {:e 2 :a {:b 3} :c {:d 5}}
[[:e] [:a :b] [:c :d :e]])
false
The following does it -
(every? #(get-in kvs %) [[:e] [:a :b] [:c :d]])
Any other answers also welcome !
How about this:
(every? #(if (vector? %)
(contains? (get-in kvs (drop-last %)) (last %))
(contains? kvs %)) [:e [:a :b] [:c :d]])
Related
I am looking to transform a clojure tree structure into a map with its dependencies
For example, an input like:
[{:value "A"}
[{:value "B"}
[{:value "C"} {:value "D"}]
[{:value "E"} [{:value "F"}]]]]
equivalent to:
:A
:B
:C
:D
:E
:F
output:
{:A [:B :E] :B [:C :D] :C [] :D [] :E [:F] :F}
I have taken a look at tree-seq and zippers but can't figure it out!
Here's a way to build up the desired map while using a zipper to traverse the tree. First let's simplify the input tree to match your output format (maps of :value strings → keywords):
(def tree
[{:value "A"}
[{:value "B"} [{:value "C"} {:value "D"}]
{:value "E"} [{:value "F"}]]])
(def simpler-tree
(clojure.walk/postwalk
#(if (map? %) (keyword (:value %)) %)
tree))
;; [:A [:B [:C :D] :E [:F]]]
Then you can traverse the tree with loop/recur and clojure.zip/next, using two loop bindings: the current position in tree, and the map being built.
(loop [loc (z/vector-zip simpler-tree)
deps {}]
(if (z/end? loc)
deps ;; return map when end is reached
(recur
(z/next loc) ;; advance through tree
(if (z/branch? loc)
;; for (non-root) branches, add top-level key with direct descendants
(if-let [parent (some-> (z/prev loc) z/node)]
(assoc deps parent (filterv keyword? (z/children loc)))
deps)
;; otherwise add top-level key with no direct descendants
(assoc deps (z/node loc) [])))))
=> {:A [:B :E], :B [:C :D], :C [], :D [], :E [:F], :F []}
This is easy to do using the tupelo.forest library. I reformatted your source data to make it fit into the Hiccup syntax:
(dotest
(let [relationhip-data-hiccup [:A
[:B
[:C]
[:D]]
[:E
[:F]]]
expected-result {:A [:B :E]
:B [:C :D]
:C []
:D []
:E [:F]
:F []} ]
(with-debug-hid
(with-forest (new-forest)
(let [root-hid (tf/add-tree-hiccup relationhip-data-hiccup)
result (apply glue (sorted-map)
(forv [hid (all-hids)]
(let [parent-tag (grab :tag (hid->node hid))
kid-tags (forv [kid-hid (hid->kids hid)]
(let [kid-tag (grab :tag (hid->node kid-hid))]
kid-tag))]
{parent-tag kid-tags})))]
(is= (format-paths (find-paths root-hid [:A]))
[[{:tag :A}
[{:tag :B} [{:tag :C}] [{:tag :D}]]
[{:tag :E} [{:tag :F}]]]])
(is= result expected-result ))))))
API docs are here. The project README (in progress) is here. A video from the 2017 Clojure Conj is here.
You can see the above live code in the project repo.
user=> (into {} '((:a :b) (:c :d)))
Throws: ClassCastException clojure.lang.Keyword cannot be cast to java.util.Map$Entry clojure.lang.ATransientMap.conj (ATransientMap.java:44).
Whereas:
user=> (into {} (list [:a :b] [:c :d]))
Is fine. It's a strange difference, since many times other functions return lists when the thing they had to begin with was a vector:
user=> (into {} (partition 2 (interleave [:a :b] [:c :d])))
Will throw, because it partition 2 ...) results in ((:a :c) (:b :d)). So it's pretty annoying. You basically have to memorize both the return types of methods and the specific behaviors of functions like into, or you have to just let stuff blow up and fix it as you find it with stuff like (into {} (map vec (partition 2 (interleave [:a :b] [:c :d])))).
Is there a specific reason why into doesn't like the pairs as lists?
The reason is as you state, only a vector pairs can be used to build maps. I don't know of a practical reason why this limitation exists. But there are also several other methods for constructing hash-maps. If you find yourself using partition, perhaps the answer is to use an alternate construction method.
If you have parallel sequences of keys and values:
(zipmap [:a :c] [:b :d])
If you have all the items in a flat sequence:
(apply hash-map [:a :b :c :d])
Building a map from a sequence:
(into {} (for [[k v] xs]
[k (transform v)]))
I never realized this wouldn't work! Don't forget:
(apply hash-map (interleave [:a :b] [:c :d]))
;=> {:b :d, :a :c}
since hash-map implicitly creates pairs from the scalar args:
(hash-map :a :c :b :d)
;=> {:b :d, :a :c}
you don't really need the (partition 2...) which is the source of the problem.
By 'at-least' I mean schema that will ignore all disallowed-key errors.
Consider the following snippet:
(require '[schema.core :as s])
(def s {:a s/Int})
(s/check s {:a 1}) ;; => nil (check passed)
(s/check s {:a 1 :b 2}) ;; => {:b disallowed-key}
(def at-least-s (at-least s))
(s/check at-least-s {:a 1}) ;; => nil
(s/check at-least-s {:a 1 :b 2}) ;; => nil
The first idea about implementation of at-least function was to conjoin [s/Any s/Any] entry to initial schema:
(defn at-least [s]
(conj s [s/Any s/Any]))
but unfortunately such implementation won't work for nested maps:
(def another-s {:a {:b s/Int}})
(s/check (at-least another-s) {:a {:b 1} :c 2}) ;; => nil
(s/check (at-least another-s) {:a {:b 1 :d 3} :c 2}) ;; => {:a {:d disallowed-key}}
Is there's a possibility to get at-least schemas that work for nested maps as well? Or maybe prismatic/schema provides something out of the box that I'm missing?
There is something you can use from metosin/schema-tools: schema-tools.walk. There is even a code that you need in the test:
(defn recursive-optional-keys [m]
(sw/postwalk (fn [s]
(if (and (map? s) (not (record? s)))
(st/optional-keys s)
s))
m))
Is there a way in Clojure to test a vector and see if it's nested, i.e. a way to test [:a :b :c :d] vs. [[:a :b] [:c :d]]?
I've tried the test
(vector? [:a :b :c :d])
true
but it remains true for nested vectors as well,
(vector? [[:a :b] [:c :d]])
true
checking if any of them are sequential seems close:
user> (every? #(not (sequential? %)) [:a :b :c :d])
true
user> (every? #(not (sequential? %)) [:a :b :c :d [:e]])
false
because all the base collections can be made into sequences, though it may be necessary to also check for Java arrays:
(every? #(not (sequential? %)) [:a :b :c :d (into-array [1 2 3])])
vector? returns true if its argument is a vector (implements IPersistentVector). [:a :b :c :d] is a vector. So is [[:a :b] [:c :d]]. Therefore, calling vector? on either of them will return true.
Now, we can say a vector is nested if any of its elements is a vector. We can test for this using some and the vector? predicate:
(defn nested-vector? [v]
(some vector? v))
This will test specifically for vectors. However, you might want to take a more general approach that applies to any Sequential data structure:
(defn nested? [coll]
(some sequential? coll))
I'm new to clojure and I've been staring at this for some time, I'm sure there's something basic I just don't see. I want to conj two sets, but they're nested, example:
(def foo {:b #{:test}})
(def bar {:a {:b #{:ab}} :c :d})
I tried:
=>(update-in bar [:a :b] conj (:b foo) )
{:a {:b #{#{:test} :ab}}, :c :d}
I guess that makes sense, but what I wanted was {:a {:b #{:test :ab}}, :c :d}
I just can't seem how to get either #{:test} out of the set to conj it, or to properly access :b as a set given the update-in syntax.
Any help is enormously appreciated.
You need to use into instead of conj:
(update-in bar [:a :b] into (:b foo))
;= {:a {:b #{:test :ab}}, :c :d}