Appending into nested associative structures - clojure

I have a structure that I created at the REPL,
{1 {10 {:id 101, :name "Paul"},
20 {}},
2 {30 {}, 40 {}},
3 {50 {}, 60 {}}}
and I want to add a new k v to the key 1, such that the resulting structure looks like this,
{1 {10 {:id 101, :name "1x2"}, 20 {}, 11 {:id 102, :name "Ringo"}},
2 {30 {}, 40 {}}, 3 {50 {}, 60 {}}}.
I just discovered get-in update-in and assoc-in for working with nested structures like these, but cannot figure out how to add new elements within elements. In my app this is all wrapped in a ref and updated with dosync/alter, but for now, I just want to be able to do this at the REPL.
Maybe I've just been looking at this too long, but any attempt to use assoc or assoc-in just changes what is already there, and does not append new elements.

Given your input
(def input
{1 {10 {:id 101 :name "Paul"}
20 {}}
2 {30 {} 40 {}}
3 {50 {} 60 {}}})
You can use assoc-in to add an element to the nested map with key 1 like this:
(assoc-in input [1 11] {:id 102 :name "Ringo"})
which yields
{1 {10 {:id 101 :name "Paul"}
11 {:id 102 :name "Ringo"}
20 {}}
2 {30 {} 40 {}}
3 {50 {} 60 {}}}
Assoc-in doesn't need to point all the way to the deepest level of a structure.
If you use two calls to assoc-in you can use the second one to change the name "Paul" to "1x2" as per your example:
(assoc-in
(assoc-in input [1 11] {:id 102 :name "Ringo"})
[1 10 :name] "1x2"))
Which returns
{1 {10 {:id 101 :name "1x2"}
11 {:id 102 :name "Ringo"}
20 {}}
2 {30 {} 40 {}}
3 {50 {} 60 {}}}

For what it's worth you could still do this if you had to point to an existing node:
(update-in input [1] assoc 11
{:id 102 :name "Ringo"})

Related

How to filter a map comparing it with another collection

I have a map with collection of these {:id 2489 ,values :.......} {:id 5647 ,values : .....} and so on till 10000 and I want to filter its value dependent on another collection which has ids of first one like (2489 ,......)
I am new to clojure and I have tried :
(into {} (filter #(some (fn [u] (= u (:id %))) [2489 3456 4567 5689]) record-sets))
But it gives me only the last that is 5689 id as output {:id 5689 ,:values ....}, while I want all of them, can you suggest what I can do.
One problem is that you start out with a sequence of N maps, then you try to stuff them into a single map. This will cause the last one to overwrite the first one.
Instead, you need to have the output be a sequence of M maps (M <= N).
Something like this is what you want:
(def data
[{:id 1 :stuff :fred}
{:id 2 :stuff :barney}
{:id 3 :stuff :wilma}
{:id 4 :stuff :betty}])
(let [ids-wanted #{1 3}
data-wanted (filterv
(fn [item]
(contains? ids-wanted (:id item)))
data)]
(println data-wanted))
with result:
[{:id 1, :stuff :fred}
{:id 3, :stuff :wilma}]
Be sure to use the Clojure CheatSheet: http://jafingerhut.github.io/cheatsheet/clojuredocs/cheatsheet-tiptip-cdocs-summary.html
I like filterv over plain filter since it always gives a non-lazy result (i.e. a Clojure vector).
You are squashing all your maps into one. First thing, for sake of performance, is to change your list of IDs into a set, then simply filter.
(let [ids (into #{} [2489 3456 4567 5689])]
(filter (comp ids :id) record-sets))
This will give you the sequence of correct maps. If you want to covert this sequence of maps into a map keyed by ID, you can do this:
(let [ids (into #{} [2489 3456 4567 5689])]
(->> record-sets
(filter (comp ids :id))
(into {} (map (juxt :id identity)))))
Another way to do this could be with the use of select-keys functions in Clojure
select-keys returns a map of only the keys given to the function
so given that your data is a list of maps we can convert it into a hash-map of ids using group-by and then call select-keys on it
(def data
[{:id 1 :stuff :fred}
{:id 2 :stuff :barney}
{:id 3 :stuff :wilma}
{:id 4 :stuff :betty}])
(select-keys (group-by :id data) [1 4])
; => {1 [{:id 1, :stuff :fred}], 4 [{:id 4, :stuff :betty}]}
However now the values is a map of ids. So in order to get the orignal structure back we need get all the values in the map and then flatten the vectors
; get all the values in the map
(vals (select-keys (group-by :id data) [1 4]))
; => ([{:id 1, :stuff :fred}] [{:id 4, :stuff :betty}])
; flatten the nested vectors
(flatten (vals (select-keys (group-by :id data) [1 4])))
; => ({:id 1, :stuff :fred} {:id 4, :stuff :betty})
Extracting the values and flattening might seem a bit inefficient but i think its less complex then the nested loop that needs to be done in the filter based methods.
You can using the threading macro to compose all the steps together
(-> (group-by :id data)
(select-keys [1 4])
vals
flatten)
Another thing that you can do is to store the data as a map of ids from the beginning this way using select keys wont require group-by and the result wont require flattening.
Update all keys in a map
(update-values (group-by :id data) first)
; => {1 {:id 1, :stuff :fred}, 2 {:id 2, :stuff :barney}, 3 {:id 3, :stuff :wilma}, 4 {:id 4, :stuff :betty}}
This would probably be the most efficient for this problem but this structure might not work for every case.

Find previous item by key value in vector of maps

I have a vector of maps like this:
[{:id 2 :val "v1"} {:id 5 :val "v2"} {:id 10 :val "v3"}]
and now I want to find an element previous to chosen id.
For example: when provided with id = 10 i want to receive:
{:id 5 :val "v2"}
and when selected id = 2 then return nil.
I'm new in clojurescript programming and cannot think of simple solution for this problem... Help please :)
You can use partition to pair adjacent maps and then search for a match on the second by id:
(def ms [{:id 2 :val "v1"} {:id 5 :val "v2"} {:id 10 :val "v3"}])
(ffirst (filter #(= 10 (:id (second %))) (partition 2 1 ms)))
(partition 2 1 data) in the accepted answer is an option, but here are two alternatives based on a "lagged" sequence.
This one first constructs the look-up table (mapping next ids to each item), which should be more performant if many lookups needs to be done. You could even easily map over it. But this approach requires ids to be unique.
(let [data [{:id 2 :val "v1"} {:id 5 :val "v2"} {:id 10 :val "v3"}]
ids (zipmap (map :id (rest data)) data)]
[(ids 10)
ids])
; [{:id 5, :val "v2"}
; {5 {:id 2, :val "v1"}, 10 {:id 5, :val "v2"}}]
This second one generates a sequence of matching documents, which is necessary if there might be more than one:
(let [data [{:id 2 :val "v1"} {:id 5 :val "v2"} {:id 10 :val "v3"}]
next-ids (->> data rest (map :id))]
(->>
(map (fn [item next-id] (if (= 10 next-id) item))
data next-ids)
(filter some?)
first))
; {:id 5, :val "v2"}
You'd get similar code by using partition but instead of #(...) you'd use destructuring: (fn [first-item second-item] (= 10 (:id second-item))). Indeed ffirst comes very handy in this approach.

Add items from collection 1 to collection 2, if collection 2 doesn't contain item from collection 1

I've got two maps:
(def people {:1 "John" :2 "Paul" :3 "Ringo" :4 "George"})
(def band
{:data
{:members
{:1 {:id 1 :name "John"}
:2 {:id 2 :name "Paul"}}}})
I want to loop over people and add any members that don't exist in [:data :members] to band, resulting in:
(def band
{:data
{:members
{:1 {:id 1 :name "John"}
:2 {:id 2 :name "Paul"}
:3 {:id 3 :name "Ringo"}
:4 {:id 4 :name "George"}}}})
Here's what I've tried:
(for [[id name] people]
(when-not
(contains? (get-in band [:data :members]) id)
(assoc-in band [:data :members id] {:id id :name name})))
Which yields:
({:data
{:members
{:4 {:id :4, :name "George"},
:1 {:name "John", :id 1},
:2 {:name "Paul", :id 2}}}}
nil
nil
{:data
{:members
{:1 {:name "John", :id 1},
:2 {:name "Paul", :id 2},
:3 {:id :3, :name "Ringo"}}}})
I'm not sure why I'm getting back what looks to be a list of each mutation of band. What am I doing wrong here? How can I add the missing members of people to band [:data :members]?
To be pedantic you aren't getting back any mutation of band. In fact, one of the most important features of Clojure is that the standard types are immutible, and the primary collection operations return a modified copy without changing the original.
Also, for in Clojure is not a loop, it is a list comprehension. This is why it always returns a sequence of each step. So instead of altering an input one step at a time, you made a new variation on the input for each step, each derived from the immutable original.
The standard construct for making a series of updated copies of an input based on a sequence of values is reduce, which passes a new version of the accumulator and each element of the list to your function.
Finally, you are misunderstanding the role of :keyword syntax - prefixing an item with a : is not needed in order to construct map keys - just about any clojure value is a valid key for a map, and keywords are just a convenient idiom.
user=> (def band
{:data
{:members
{1 {:id 1 :name "John"}
2 {:id 2 :name "Paul"}}}})
#'user/band
user=> (def people {1 "John" 2 "Paul" 3 "Ringo" 4 "George"})
#'user/people
user=> (pprint
(reduce (fn [band [id name :as person]]
(if-not (contains? (get-in band [:data :members]) id)
(assoc-in band [:data :members id] {:id id :name name})
band))
band
people))
{:data
{:members
{3 {:id 3, :name "Ringo"},
4 {:id 4, :name "George"},
1 {:name "John", :id 1},
2 {:name "Paul", :id 2}}}}
nil
You may notice the body of the fn passed to reduce is essentially the same as the body of your for comprehension. The difference is that instead of when-not which returns nil on the alternate case, I use if-not, which allows us to propagate the accumulator (here called band, same as the input) regardless of whether any new version of it is made.

How can I create an empty hash-map in clojure

It gives me a ArrayMap as I code
(class (hash-map))
But it comes out a HashMap when I code:
(class (hash-map "" ""))
Question is "How can I create an empty hash-map"?
Another possibility is to use pre-defined EMPTY field:
user=> (clojure.lang.PersistentHashMap/EMPTY)
{}
In my opinion it is better shows your intent.
You can create empty hash-map like this:
(. clojure.lang.PersistentHashMap create {})
(clojure.lang.PersistentHashMap/create {})
(clojure.lang.PersistentHashMap/EMPTY)
You can check the source code of hash-map:
user=> (source hash-map)
(defn hash-map
"keyval => key val
Returns a new hash map with supplied mappings. If any keys are
equal, they are handled as if by repeated uses of assoc."
{:added "1.0"
:static true}
([] {})
([& keyvals]
(. clojure.lang.PersistentHashMap (create keyvals))))
As you can see in the code, if you don't provide arguments, hash-map function returns {}, which is the instance of PersistentArrayMap.
If you really need the instance of empty PersistentHashMap, you can create it with the following code:
(. clojure.lang.PersistentHashMap create {})
You can check the class of created instance:
user=> (class (. clojure.lang.PersistentHashMap create {}))
clojure.lang.PersistentHashMap
user=> (class (clojure.lang.PersistentHashMap/create {}))
clojure.lang.PersistentHashMap
user=> (class (clojure.lang.PersistentHashMap/EMPTY)) ;; om-nom-nom's : much simpler
clojure.lang.PersistentHashMap
But, I'm not sure that doing this is good or necessary. Perhaps you code shouldn't depend on specific implementation class.
You shouldn't really need to worry about this. The runtime makes a judgement on the best implementation to use. PersistentArrayMap is preferred (ie. it's more efficient in time and space) for a small number of key/value pairs, but promotion to PersistentHashMap happens once the kv limit of 8 is crossed, see the relevant code for details
*clojure-version*
{:major 1, :minor 5, :incremental 1, :qualifier nil}
; map declared with {} with 8 kv pairs is ArrayMap
(type {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8})
=> clojure.lang.PersistentArrayMap
; map declared with {} with 9 kv pairs is HashMap
(type {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9})
=> clojure.lang.PersistentHashMap
; assoc'ing 1 kv pairs into an ArrayMap is an ArrayMap (oddly)
(type (-> {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8}
(assoc :i 9)))
clojure.lang.PersistentArrayMap
; assoc'ing 2 kv pairs into an ArrayMap is an HashMap
(type (-> {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8}
(assoc :i 9)
(assoc :j 10)))
clojure.lang.PersistentHashMap

update-in for sets in Clojure?

i have a series of items in a set like this:
(def my-set
#{
{:id "ab" :a 1 :b 2}
{:id "abc" :a 1 :b 2}
{:id "abcd" :a 1 :b 2}
}
)
: and I wish to update one of the items something like this :
(update-in-set my-set :id "abc" {:id "abc" :a 6 :b 20})
. that would return :
#{
{:id "ab" :a 1 :b 2}
{:id "abc" :a 6 :b 20}
{:id "abcd" :a 1 :b 2}
}
: Is there any Clojure built in function or other easy way to do this?
Update
In the end I did this:
(defn update-in-set [my-set key value new-record]
(merge (clojure.set/select #(not= (get % key) value) my-set ) new-record)
)
I wonder if you shouldn't be using a map rather than a set here, with id as the key. Then what you want to do could be easily performed with assoc.
You are having problems as sets don't really have the idea of updating values - each item is unique and either present or not - so what you need to do is remove the old value and add a new one. This could be done a little easier with conj and disj I think:
(conj (disj #{'a 'b 'c} 'a) 'e)
Which would remove 'a and add 'e. This assumes you have some way of getting the complete item from the "key".