How to write it better in Clojure

My hash structure

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

      

I want to find tf. I have it right now. 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))))

      

+2


source to share


1 answer


Updated to work with 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

      

+4


source







All Articles