So basically how do I make a function given the input {:A 1 :B 2 :C {:X 5 :Y 5 :Z 5} :D 1} and the key :C
return {:A 1 :B 2 :C {:X 0 :Y 0 :Z 0} :D 1}? It's the same mapping but with the nested map all set to 0. Given that we know that the key :C has the nested values.
I'm very new to clojure and I'm struggling with loops and iterations so any help would be appreciated.
Thanks.
(defn with-zero-vals-at-key
[m k]
(update m k (fn [m2] (zipmap (keys m2) (repeat 0)))))
(with-zero-vals-at-key {:A 1 :B 2 :C {:X 5 :Y 5 :Z 5} :D 1} :C)
;; => {:A 1, :B 2, :C {:X 0, :Y 0, :Z 0}, :D 1}
;; OR
(defn with-zero-vals
[m]
(zipmap (keys m) (repeat 0)))
(update {:A 1 :B 2 :C {:X 5 :Y 5 :Z 5} :D 1}
:C
with-zero-vals)
;; => {:A 1, :B 2, :C {:X 0, :Y 0, :Z 0}, :D 1}
If our input were to look something like this:
(({:a 1 :b 100} {:a 2 :b 300} {:a 4 :b 0}) ({:a 0 :b 10} {:a 4 :b 50}))
Our range that we would like to police over would be (0 1 2 3 4)
We would like the output to be:
(({:a 0 :b 0} {:a 1 :b 100} {:a 2 :b 300} {:a 3 :b 0} {:a 4 :b 0})
({:a 0 :b 10} {:a 1 :b 0} {:a 2 :b 0} {:a 3 :b 0} {:a 4 :b 50}))
Basically what it should do is look at the first list of maps then the second and so on and figure out what the range is for :a. We can easily do that with a min/max function. Now it creates a range and applies it to both lists. If an :a is missing on one list it adds in that :a with a :b of 0. (ie the addition of {:a 0 :b 0} or {:a 3 :b 0} in the first list. We have a function that can somewhat do it, but aren't quite there yet. Here it is:
(map
#(doseq [i (vec myRange)]
(if (some (fn [list] (= i list)) (map :a %))
nil
(println (conj % {:a i :b 0}))))
myList)
Obviously because of Clojures immutable data structures this function fails. If our input is something like:
(({:a 1, :b 1} {:a 2, :b 3} {:a 4, :b 5})
({:a 0, :b 3} {:a 4, :b 1}))
our output is:
(nil nil)
but if we println:
({:a 0, :b 0} {:a 1, :b 1} {:a 2, :b 3} {:a 4, :b 5})
({:a 3, :b 0} {:a 1, :b 1} {:a 2, :b 3} {:a 4, :b 5})
({:a 1, :b 0} {:a 0, :b 3} {:a 4, :b 1})
({:a 2, :b 0} {:a 0, :b 3} {:a 4, :b 1})
({:a 3, :b 0} {:a 0, :b 3} {:a 4, :b 1})
(nil nil)
We want the output to look like:
(({:a 0, :b 0} {:a 1, :b 1} {:a 2, :b 3} {:a 3, :b 0} {:a 4, :b 5})
({:a 0, :b 3} {:a 1, :b 0} {:a 2, :b 0} {:a 3, :b 0} {:a 4, :b 1}))
without the use of a println. Any suggestions?
the idea of working with immutable data in loop, is to pass the result of the latest iteration to the next one. You could do it with loop/recur, but in your case it is common to use reduce function (which is literally one of the cornerstones of functional programming):
(defn update-coll [range items]
(reduce (fn [items i] (if (some #(= (:a %) i) items)
items
(conj items {:a i :b 0})))
items range))
the first parameter to reduce "updates" items for every value of range (i), passing the updated value to the next iteration.
now you just have to map your input data with it:
(def input '(({:a 1 :b 100} {:a 2 :b 300} {:a 4 :b 0})
({:a 0 :b 10} {:a 4 :b 50})))
(map (comp (partial sort-by :a)
(partial update-coll [0 1 2 3 4]))
input)
output:
(({:a 0, :b 0} {:a 1, :b 100} {:a 2, :b 300}
{:a 3, :b 0} {:a 4, :b 0})
({:a 0, :b 10} {:a 1, :b 0} {:a 2, :b 0}
{:a 3, :b 0} {:a 4, :b 50}))
also you can do it without accumulation using clojure's sets:
(defn process-input [input r]
(let [r (map #(hash-map :a % :b 0) r)]
(map (fn [items] (into (apply sorted-set-by
#(compare (:a %1) (:a %2))
items)
r))
input)))
(process-input input [0 1 2 3 4])
output:
(#{{:b 0, :a 0} {:a 1, :b 100} {:a 2, :b 300}
{:b 0, :a 3} {:a 4, :b 0}}
#{{:a 0, :b 10} {:b 0, :a 1} {:b 0, :a 2}
{:b 0, :a 3} {:a 4, :b 50}})
My attempt:
(defn fill-in-missing [lists]
(let [[min max] (apply (juxt min max) (map :a (flatten lists)))]
(for [cur-list lists]
(for [i (range min (inc max))]
(merge {:a i :b 0}
(some #(when (= i (:a %)) %) cur-list))))))
To get the minimum and maximum values of :a I just collect every :a with map and flatten, then I use juxt so I can apply both the min and max functions to them at the same time.
Since we want two levels of nesting lists, I went for two for list comprehensions and tried to make an expression that would attempt to find the map in the input, or else return the default {:a i :b 0}.
I have a lazy sequence of maps :
{:a 1 :b "a"} {:a 1 :b "b"} {:a 2 :b "a"} {:a 3 :b "a"} {:a 3 :b "b"} ...
and I want to group it by key :a and return another lazy seq:
[{:a 1 :b "a"} {:a 1 :b "b"}] [{:a 2 :b "a"}] [{:a 3 :b "a"} {:a 3 :b "b"}] ...
What would be the best way to achieve this?
user=> (def a [{:a 1 :b "a"} {:a 1 :b "b"} {:a 2 :b "a"} {:a 3 :b "a"} {:a 3 :b "b"}])
#'user/a
user=> (group-by :a a)
{1 [{:a 1, :b "a"} {:a 1, :b "b"}], 2 [{:a 2, :b "a"}], 3 [{:a 3, :b "a"} {:a 3, :b "b"}]}
user=> (map second (group-by :a a))
([{:a 1, :b "a"} {:a 1, :b "b"}] [{:a 2, :b "a"}] [{:a 3, :b "a"} {:a 3, :b "b"}])
What would be quickest way to transform this collection :
[[{:a 1} {:a 2} {:a 3}] [{:b 4} {:b 5} {:b 6}] [{:c 7} {:c 8} {:c 9}]]
into this collection? :
[{:a 1, :b 4, :c 7} {:a 2, :b 5, :c 8} {:a 3, :b 6, :c 9}]
I've come up with this but I still feel it could be shorter :
(map (partial apply merge)
(apply map vector collection))
Note that the numbers are randomly picked, just to show that the content of each val is unique...
(def data [[{:a 1} {:a 2} {:a 3}] [{:b 4} {:b 5} {:b 6}] [{:c 7} {:c 8} {:c 9}]])
(apply mapv merge data)
;=> [{:a 1, :c 7, :b 4} {:a 2, :c 8, :b 5} {:a 3, :c 9, :b 6}]
Given collection of vectors of separated hash-maps
How can I go from :
[[{:a 1} {:b 2} {:c 3}] [{:a 4} {:b 5} {:c 6}] [{:a 7} {:b 8} {:c 9}]]
To:
[[{:a 1 :b 2 :c 3}] [{:a 4 :b 5 :c 6}] [{:a 7 :b 8 :c 9}]]
Thanks you for your answers!
(def coll [[{:a 1} {:b 2} {:c 3}] [{:a 4} {:b 5} {:c 6}] [{:a 7} {:b 8} {:c 9}]])
(mapv (fn [v] [(apply merge v)]) coll)
;; => [[{:a 1 :c 3 :b 2}] [{:a 4 :c 6 :b 5}] [{:a 7 :c 9 :b 8}]]
(def data [[{:a 1} {:b 2} {:c 3}] [{:a 4} {:b 5} {:c 6}] [{:a 7} {:b 8} {:c 9}]])
(mapv #(-> [(into {} %)]) data)