Distributing and merging hash-map elements in Clojure - clojure

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}]

Related

In Clojure, how do I make a nested map return a map with an inner map all set to 0?

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}

Fill in missing values in a list of list of maps over a certain range in Clojure

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}.

Default value for a nested map in clojure

I have been looking into clojure for a while now, but I couldnt find a function which would supply a default value for a nested map. for example if I have a map as below,
(def paths {:A {:B 5, :D 5, :E 7},
:B {:C 4},
:C {:D 8, :E 2},
:D {:C 8, :E 6},
:E {:B 3}
})
I want a function that creates a map as below, from the given hash-map,
{:A {:B 5, :C default_val :D 5, :E 7},
:B {:A default_val, :C 4, :D default_val, :E default_val},
:C {:A default_val, :B default_val, :C default_val, :D 8, :E 2},
:D {:A default_val, :B default_val, :C 8, :D default_val, :E 6},
:E {:A default_val, :B 3, :C 8, :D default_val, :E 3}
}
from reading your example it's hard to tell exactly when you want the default value included, though there is a very common pattern of providing a set of defaults and merging the input into it so that wherever a value is given it's used and if it's not there for a particular key the default value is used:
user> (def default-values {:A :default :B :default :C :default :D :default})
#'user/default-values
user> (def paths {:A {:B 5, :D 5, :E 7},
:B {:C 4},
:C {:D 8, :E 2},
:D {:C 8, :E 6},
:E {:B 3}})
#'user/paths
user> (keys paths)
(:A :D :B :C :E)
user> (vals paths)
({:D 5, :B 5, :E 7} {:C 8, :E 6} {:C 4} {:D 8, :E 2} {:B 3})
user> (map #(merge default-values %) (vals paths))
({:A :default, :D 5, :B 5, :C :default, :E 7}
{:A :default, :D :default, :B :default, :C 8, :E 6}
{:A :default, :D :default, :B :default, :C 4}
{:A :default, :D 8, :B :default, :C :default, :E 2}
{:A :default, :D :default, :B 3, :C :default})
user> (clojure.pprint/pprint
(zipmap (keys paths)
(map #(merge default-values %)
(vals paths))))
{:E {:A :default, :D :default, :B 3, :C :default},
:C {:A :default, :D 8, :B :default, :C :default, :E 2},
:B {:A :default, :D :default, :B :default, :C 4},
:D {:A :default, :D :default, :B :default, :C 8, :E 6},
:A {:A :default, :D 5, :B 5, :C :default, :E 7}}

Group(Partition) lazy sequence by key

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"}])

How can I merge hash-maps that are inside a collection of vectors in clojure?

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)