开发者

How do you add in this hash table in Clojure?

开发者 https://www.devze.com 2022-12-08 09:02 出处:网络
I have a document hash which is a reference like this: (def *document-hash* (ref (hash-map))) It looks like this

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))))))
  • I want to update the term-details for the documents
  • Every time a new term comes, I want to get the term-details开发者_如何学Python and update the terms and its count
  • initially the hash is empty

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


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}}


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)))


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

0

精彩评论

暂无评论...
验证码 换一张
取 消