如何在Clojure中更好地写出这个

时间:2009-10-06 15:24:53

标签: clojure

哈希的结构是

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

1 个答案:

答案 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