我有以下结构:
(defrecord Member [id name salary role])
(defrecord Project [id name duration])
(defrecord ProjectMember [project member])
(def project-member-records (ref ()))
(defn find-project-member-record [parm-proj-id parm-member-id]
(filter #(let [project (.project %)
member (.member %)
proj-id (:id project)
member-id (:id member)]
(and (= proj-id parm-proj-id)
(= member-id parm-member-id))) @project-member-records))
;;Sample func, does not work
(defn remove-project-member-record [proj-id member-id]
(dosync (ref-set project-member-records (remove #(= (:id (.project %)) proj-id) @project-member-records))))
现在,我要从project-member-records
中删除项目。例如,我想逐项删除项目ID和成员ID,就像我在find-project-member-record
函数中找到记录一样。或者(和)我想删除项目,而不是project-member-records
找到的记录,例如(remove (find-project-member-record 1 1) project-records) ;pseudo code
但是我不知道我该怎么做。
答案 0 :(得分:1)
使用remove
是正确的,但find-project-member-record
会返回一个序列。尝试
(defn remove-project-member-record [proj-id member-id]
(let [it (first (find-project-member-record proj-id member-id))]
(dosync
(ref-set project-member-records
(remove #(= % it) @project-member-records)))))
如果find-project-member-record
始终返回空序列或带有一个项目的序列,则可能有意义的是返回项目本身或nil
。 (例如,在first
而非filter
及其他地方find-project-member-record
的结果上致电remove-project-member-record
。
如果您希望将所有内容包含在dosync
事务中,可以使用以下方法构建它:
(defn remove-project-member-record [proj-id member-id]
(dosync
(let [it (first (find-project-member-record proj-id member-id))]
(ref-set project-member-records
(remove #(= % it) @project-member-records)))))