Define conj-in, a conjing variant of assoc-in.

This commit is contained in:
Richard Newman 2016-07-12 20:19:37 -07:00
parent 5a8dbace4a
commit 69348eb0b4

View file

@ -17,3 +17,15 @@
(str/starts-with? (name x) "?")) (str/starts-with? (name x) "?"))
(keyword (subs (name x) 1)) (keyword (subs (name x) 1))
(raise (str x " is not a Datalog var.")))) (raise (str x " is not a Datalog var."))))
(defn conj-in
"Associates a value into a sequence in a nested associative structure, where
ks is a sequence of keys and v is the new value, and returns a new nested
structure.
If any levels do not exist, hash-maps will be created. If the destination
sequence does not exist, a new one is created."
{:static true}
[m [k & ks] v]
(if ks
(assoc m k (conj-in (get m k) ks v))
(assoc m k (conj (get m k) v))))