tags:

views:

103

answers:

1

The structure of my hash is

(def *document-hash*   {"documentid" {:term-detail {"term1" tf1 ,"term2" tf2}})

I wan to find the tf. Right now I have this. Is there a better way to do this in clojure?

;; Find tf
(defn find-tf [documentid term-number]
  (if (nil? *document-hash* documentid)
       0
       (if (nil? (((*document-hash* documentid) :term-detail) term-number))
            0
            (((*document-hash* documentid) :term-detail) term-number))))
+3  A: 

Updated to work with a Ref:

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

(dosync (alter *document-hash* conj {12603 {:term-detail {2 10}, :doclen 30}}))

(defn find-tf [documentid term-number]
  (or (get-in @*document-hash* [documentid :term-detail term-number])
      0))

(find-tf 12603 2) ; yields 10
pmf
It returns null even though I have a value. Coud you try for this: (def *document-hash* (ref (hash-map)))(dosync (alter *document-hash* conj {12603 {:term-detail {2 10}, :doclen 30}}))
kunjaan
If your document-hash is a ref, you'll have to replace *document-hash* by @*document-hash* (I'm editing my response to incorporate this).
pmf