How can I update a vector item in Clojure? - clojure

Given:
(def my-vec [{:id 0 :a "foo" :b "bar"} {:id 1 :a "baz" :b "spam"}
{:id 2 :a "qux" :b "fred"}])
How can I idiomatically update * the item in my-vec with :id=1 to have values :a="baz2" and :b="spam2"?
*: I recognize that I wouldn't actually be updating my-vec, but really returning a new vector that is identical to my-vec except for the replacement values.

Do you know ahead of time that the map with id == 1 is the second map in your vector? If so:
user> (-> my-vec
(assoc-in [1 :a] "baz2")
(assoc-in [1 :b] "spam2"))
[{:id 0, :a "foo", :b "bar"} {:id 1, :a "baz2", :b "spam2"} {:id 2, :a "qux", :b "fred"}]
If you need to access your data by id a lot, another idea is to replace your vector of hash-maps with a hash-map of hash-maps keyed on :id. Then you can more easily assoc-in no matter the order of things.
user> (def new-my-vec (zipmap (map :id my-vec) my-vec))
#'user/new-my-vec
user> new-my-vec
{2 {:id 2, :a "qux", :b "fred"}, 1 {:id 1, :a "baz", :b "spam"}, 0 {:id 0, :a "foo", :b "bar"}}
user> (-> new-my-vec
(assoc-in [1 :a] "baz2")
(assoc-in [1 :b] "spam2"))
{2 {:id 2, :a "qux", :b "fred"}, 1 {:id 1, :a "baz2", :b "spam2"}, 0 {:id 0, :a "foo", :b "bar"}}

map a function over the vector of maps that either creates a modified map if the key matches or uses the original if the keys don't match then turn the result back into a vector
(vec (map #(if (= (:id %) 1)
(assoc % :a "baz2" :b "spam2")
%)))
It is possible to do this more succinctly though this one really shows where the structural sharing occurs.

Might want to take a look at array-map which creates a map backed by an array and keyed by the index instead of using :id?

Related

Convert a list of maps by the values of the maps [clojure]

I have a list filled with many maps (all of them have the same key), like this:
({:a 1} {:a 1} {:a 2} {:a 2} {:a 3} {:a 2})
I would like to convert it to a map that stores the occurrence of the value of each map. For exemple, the list above should return the following map:
{:1 2, :2 3, :3 1}
Any ideas on how can i do that?
(def m '({:a 1} {:a 1} {:a 2} {:a 2} {:a 3} {:a 2}))
(frequencies (map :a m)) ;; => {1 2, 2 3, 3 1}
Note the keys of the result are not keywords, as that would be an odd thing to do.
I would solve it like this:
(ns tst.demo.core
(:use demo.core tupelo.core tupelo.test))
(defn maps->freqs
[maps]
(frequencies
(for [m maps]
(second (first m)))))
(dotest
(let [data (quote
({:a 1} {:a 1} {:a 2} {:a 2} {:a 3} {:a 2}))]
(is= (maps->freqs data)
{1 2, 2 3, 3 1})))
The above uses my favorite template project. The best technique is to build it up slowely:
(defn maps->freqs
[maps]
(for [m maps]
(first m)))
then (spyx-pretty (maps->freqs data)) produces
(maps->freqs data) =>
[[:a 1] [:a 1] [:a 2] [:a 2] [:a 3] [:a 2]]
modify it:
(defn maps->freqs
[maps]
(for [m maps]
(second (first m))))
with result
(maps->freqs data) =>
[1 1 2 2 3 2]
Then use frequencies to get the final result.
Please be sure to read the list of documentation, especially the Clojure CheatSheet!

Using specter to transform values that match a key

I'm sorry if this has been answered elsewhere, but I can't seem to find an example that matches the pattern of what I'm looking for. I also may not yet understand recursive specter paths fully.
If I have the data (explicitly with the nested vector):
{:a "1" :b "2" :c [ {:a "3" :b "4"} {:a "5" :b "6"} ]}
And I'd like to apply the keyword function to all values with the key :a to result in:
{:a :1 :b "2" :c [ {:a :3 :b "4"} {:a :5 :b "6"} ]}
Finally, I'd like it to be recursive to an arbitrary depth, and handle the vector case as well.
I've read https://github.com/nathanmarz/specter/wiki/Using-Specter-Recursively , but I must be missing something critical.
Thanks to anyone pointing me in the right direction!
(use '[com.rpl.specter])
(let [input {:a "1" :b "2" :c [{:a "3" :b "4"} {:a "5" :b "6"}]}
desired-output {:a :1 :b "2" :c [{:a :3 :b "4"} {:a :5 :b "6"}]}
FIND-KEYS (recursive-path [] p (cond-path map? (continue-then-stay [MAP-VALS p])
vector? [ALL p]
STAY))]
(clojure.test/is
(= (transform [FIND-KEYS (must :a)] keyword input)
desired-output)))
Not a Specter solution, but it is easily done via clojure.walk/postwalk:
(ns demo.core
(:require
[clojure.walk :as walk] ))
(def data {:a "1" :b "2" :c [{:a "3" :b "4"} {:a #{7 8 9} :b "6"}]})
(def desired {:a :1 :b "2" :c [{:a :3 :b "4"} {:a #{7 8 9} :b "6"}]})
(defn transform
[form]
(if (map-entry? form)
(let [[key val] form]
(if (and
(= :a key)
(string? val))
[key (keyword val)] ; can return either a 2-vector
{key val})) ; or a map here
form))
(walk/postwalk transform data) =>
{:a :1, :b "2", :c [{:a :3, :b "4"} {:a #{7 9 8}, :b "6"}]}
I even put in a non-string for one of the :a values to make it trickier.

How to merge list of maps in clojure

I am given a list maps:
({:a 1 :b ["red" "blue"]} {:a 2 :b ["green"]} {:a 1 :b ["yellow"]} {:a 2 :b ["orange"]})
and I need to combine them to ultimately look like this:
({:a 1 :b ["red" "blue" "yellow"]} {:a 2 :b ["green" "orange"]})
Where the maps are combined based off the value of the key "a".
So far, I have this
(->> (sort-by :a)
(partition-by :a)
(map (partial apply merge)))
But the merge will overwrite the vector in "b" with the last one giving me
({:a 1 :b ["yellow"]} {:a 2 :b ["orange"]})
Instead of merge, use merge-with, i.e.
(->> a (sort-by :a)
(partition-by :a)
(map (partial
apply
merge-with (fn [x y] (if (= x y) x (into x y))))))
Outputs
({:a 1, :b ["red" "blue" "yellow"]} {:a 2, :b ["green" "orange"]})
You don't really want to sort or partition anything: that is just CPU time spent on nothing, and at the end you have an awkward data structure (a list of maps) instead of something that would be more convenient, a map keyed by the "important" :a value.
Rather, I would write this as a reduce, where each step uses merge-with on the appropriate subsection of the eventual map:
(defn combine-by [k ms]
(reduce (fn [acc m]
(update acc (get m k)
(partial merge-with into)
(dissoc m k)))
{}, ms))
after which we have
user=> (combine-by :a '({:a 1 :b ["red" "blue"]}
{:a 2 :b ["green"]}
{:a 1 :b ["yellow"]}
{:a 2 :b ["orange"]}))
{1 {:b ["red" "blue" "yellow"]}, 2 {:b ["green" "orange"]}}
which is usefully keyed for easily looking up a map by a specific :a, or if you prefer to get the results back out as a list, you can easily unroll it.

How to check if a map is a subset of another in clojure?

I would like to write a function that checks if a map is a subset of another.
An example of usage should be:
(map-subset? {:a 1 :b 2} {:a 1 :b 2 :c 3})
=> true
Is there a native way to do that?
By converting the maps to sets, you can use clojure.set/subset?
(clojure.set/subset? (set {:a 1 :b 2}) (set {:a 1 :b 2 :c 3}))
=> true
This would make each pair of the map an element in the set
(set {:a 1 :b 2 :c 3})
=> #{[:b 2] [:c 3] [:a 1]}
And as such, {:a 1 :b 3} would not be a subset
(clojure.set/subset? (set {:a 1 :b 3}) (set {:a 1 :b 2 :c 3}))
=> false
(defn submap?
"Checks whether m contains all entries in sub."
[^java.util.Map m ^java.util.Map sub]
(.containsAll (.entrySet m) (.entrySet sub)))
REPL demo:
(submap? {:foo 1 :bar 2 :quux 3} {:foo 1 :bar 2})
;= true
(submap? {:foo 1 :bar 2 :quux 3} {:foo 1 :bar 3})
;= false
(submap? {:foo 1 :bar 2} {:foo 1 :bar 2 :quux 3})
;= false
Another option could be:
(defn submap? [a b]
(= a (select-keys b (keys a))))
This will only check equity of the keys in the first map.
Assuming that you intend the keys and the values to match ...
Is there a native way to do that?
There is no standard function. I suggest ...
(defn map-subset? [a-map b-map]
(every? (fn [[k _ :as entry]] (= entry (find b-map k))) a-map))
A few examples:
(map-subset? {:a 1 :b 2} {:a 1 :b 2 :c 3})
=> true
(map-subset? {:d 4} {:a 1 :b 2 :c 3})
=> false
(map-subset? {:a 3} {:a 1 :b 2 :c 3})
=> false
(map-subset? {:a nil} {})
=> false
It traverses its first argument just once.
It handles nil values properly.
If neither of these are important, go with a more elegant version such as kasterma's.
Making an assumption on what you mean by subset (direct translation of that definition):
(and (every? (set (keys m1)) (keys m2)) ;; subset on keys
(every? #(= (m1 %)(m2 %)) (keys m2))) ;; on that subset all the same values
There are many possible ways to solve your question. One quick possible solution would be:
(defn contains-submap? [map-structure keys]
(every? (partial contains? map-structure) keys))
(contains-submap? {:a 1 :b 2 :c 3} (keys {:a 1 :b 2}))
true
It can be done with Sets for example. But as #kasterma pointed, it depends on your original intend.

How to add fields to a map in Clojure?

I have a map like this:
{:a 1 :b 20}
: and I want to make sure that certain fields are not missing from the map:
(:a :b :c :d )
: Is there a function to merge the two, something like :
(merge-missing-keys {:a 1 :b 20} (:a :b :c :d ))
: which can produce :
{:a 1 :b 20 :c nil :d nil}
?
Update:
With some pointers from the answers I found that this can be done like this:
(defn merge-missing-keys [
a-set
some-keys
]
(merge-with
#(or %1 %2)
a-set
(into {} (map (fn[x] {x nil}) some-keys))))
(merge-missing-keys {:a 1 :b 20} '(:a :b :c :d :e ))
You should use merge-with:
Returns a map that consists of the rest of the maps conj-ed onto
the first. If a key occurs in more than one map, the mapping(s)
from the latter (left-to-right) will be combined with the mapping in
the result by calling (f val-in-result val-in-latter).
So the following will merge all maps with one actual value selected from the maps or nil.
(merge-with #(or %1 %2)
{:a 1 :b 2}
{:a nil :b nil :c nil :d nil})
; -> {:d nil :c nil :b 2 :a 1}
This will probably be enough for you to build your implementation.
You can always just merge into your default array as follows:
(merge
{:a nil :b nil :c nil :d nil} ; defaults
{:a 1 :b 20}) ; current values
=> {:a 1, :b 20, :c nil, :d nil}
A riff on #mikera's answer, to make it work when you don't have the keys available as literals:
(let [keys [:a :b :c :d]]
(merge (zipmap keys (repeat nil))
{:a 1 :b 20}))