(do
(def a {:foo :bar})
(def b {:another :value :foo :BAR})
(merge-with (fn [x y]
(conj (if-not (vector? x)
[x]
x)
y))
a b))
If override the duplicate values merge could be used. Also one approach you
can take is try to isolate your code that is impure (talks to database) ,
updates atom as much as possible. This way your experience with repl
becomes more productive because you reduce the complexity of things that
can go wrong.
Regards,
Geraldo
On Wednesday, October 11, 2017 at 12:44:40 PM UTC-3, [email protected]
wrote:
>
> I seem unable to figure out where I made a mistake, though this should be
> simple.
>
> I have two SQL calls that bring back 5 fields:
>
> SELECT company_profile_id , reference_id, reference_source FROM
> company_reference_id limit 1 ;
>
> +--------------------+--------------+------------------+
> | company_profile_id | reference_id | reference_source |
> +--------------------+--------------+------------------+
> | 2 | 331089191 | EIN |
> +--------------------+--------------+------------------+
>
> SELECT company_profile_id, url FROM company_website limit
> 1 ;
>
> +--------------------+------------------+
> | company_profile_id | url |
> +--------------------+------------------+
> | 2 | mikeshawauto.com |
> +--------------------+------------------+
>
> This brings back a total of 5 values. I need to have a document that has 5
> values, though if values have the same field-name, then I want to
> consolidate them into one vector. There are 4 unique field names, so I
> expect to end up with 4 vectors, holding 5 values. Instead, I get this:
>
> ({:company_profile_id ["2"], :topic :company,
> :how-many-rows-and-fields-from-database 13, :url ["mikeshawauto.com"],
> :reference_id ["331089191"], :reference_source ["ein"]})
>
> I expect:
>
> {:company_profile_id ["2" "2"]
>
> but I get:
>
> {:company_profile_id ["2"]
>
> The documents are combined in a map in an atom, with this function:
>
>
> (def documents (atom {}))
>
>
> (defn update-documents
> [denormalized-id field-name field-value
> how-many-rows-and-fields-from-database topic]
> {:pre [
> (not (nil? denormalized-id))
> (not (nil? field-name))
> (vector? field-value)
> ]}
> (swap! documents
> (fn [old-documents]
> (slingshot/try+
> (let [
> document (get old-documents denormalized-id {})
> old-field-value (get old-documents field-name [])
> new-field-value (into old-field-value field-value)
> document (assoc document field-name new-field-value)
>
> previous-how-many-rows-and-fields-from-database (get
> document :how-many-rows-and-fields-from-database 0)
> new-how-many-rows-and-fields-from-database (+
> previous-how-many-rows-and-fields-from-database
> how-many-rows-and-fields-from-database)
> document (assoc document :topic topic)
> document (assoc document
> :how-many-rows-and-fields-from-database
> new-how-many-rows-and-fields-from-database)
> new-documents (assoc old-documents denormalized-id
> document)
> ]
> new-documents)
> (catch Object o
> (errors/log o)
> (slingshot/throw+ {
> :type ::update-documents
> :error o
> }
> ))))))
>
> Can I assume that this always gives me 2 values in a vector?
>
> old-field-value (get old-documents field-name [])
> new-field-value (into old-field-value field-value)
> document (assoc document field-name new-field-value)
>
> I'm going to guess that the bug is somewhere else in my code. But if
> anyone sees a flaw in this function, I'd be grateful if you could point it
> out to me.
>
>
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.