哈希的结构是
(def *document-hash* {"documentid" {:term-detail {"term1" tf1 ,"term2" tf2}})
我想找到tf。现在我有这个。在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))))
答案 0 :(得分:4)
更新为使用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