gpt4 book ai didi

clojure - 如何在 Clojure 中添加这个哈希表?

转载 作者:行者123 更新时间:2023-12-02 11:25:00 28 4
gpt4 key购买 nike

我有一个文档哈希,它是这样的引用:

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

看起来像这样

 {"documentid" {:term-detail {"term1" count1 ,"term2" count2},  "doclen" 33}}}

如何添加到这个哈希表?现在我已经

(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))))))
  • 我想更新文档的术语详细信息
  • 每次出现新术语时,我都想获取术语详细信息并更新术语及其计数
  • 最初哈希为空

但这会引发空指针异常,因为当我尝试添加术语编号时,不会创建术语详细信息哈希。

最佳答案

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

关于clojure - 如何在 Clojure 中添加这个哈希表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1527986/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com