unj2
unj2

Reputation: 53481

How do you add in this hash table in Clojure?

I have a document hash which is a reference like this:

(def *document-hash* (ref (hash-map)))  

It looks like this

 {"documentid" {:term-detail {"term1" count1 ,"term2" count2},  "doclen" 33}}}

How do I add to this hash table?Right now I have

(defn add-doc-hash [docid  term-number count]
  (dosync (alter *document-hash*
    (fn [a-hash]
      (assoc a-hash docid {:term-detail  
        (assoc ((a-hash docid)) :term-detail) term-number count), :doclen 33))))))

But this throws the null pointer exception because ther term-detail hash is not created when I try to add the term-number.

Upvotes: 3

Views: 1606

Answers (3)

Eric Normand
Eric Normand

Reputation: 3836

Here's a rewrite of your function that should work. It uses the assoc-in function

(defn add-doc-hash [docid  term-number count]
  (dosync (alter *document-hash* assoc-in [docid :term-detail term-number] count)))

Upvotes: 1

Brian Carper
Brian Carper

Reputation: 72926

user> (def x (ref {"documentid" {:term-detail {"term1" 1 ,"term2" 2},  "doclen" 33}}))
#'user/x
user> (dosync (alter x assoc-in ["documentid" :term-detail "term3"] 0))
{"documentid" {:term-detail {"term3" 0, "term1" 1, "term2" 2}, "doclen" 33}}
user> (dosync (alter x update-in ["documentid" :term-detail "term3"] inc))
{"documentid" {:term-detail {"term3" 1, "term1" 1, "term2" 2}, "doclen" 33}}

Upvotes: 1

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91534

Another way of phrasing the question if i understand it correctly would be: "how do i write a function to add another [term, count] pair to a map."

a little helper function to get the current details for a map, if that map is not yet added, then clearly it will have no details so i represent this with an empty map
this solves your problem of where to add the first term-number:

(defn get-term-detail [a-hash docid]
  (let [entry (a-hash docid)]
    (if nil? entry)
       {}
       (:term-details entry))))

then soemthing like:

(assoc a-hash docid {:term-details (assoc (get-term-detail a-hash docid) term-number count)        :doclen 33)

to actuall add it to the hash

Upvotes: 0

Related Questions