0

我有以下结构:

(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但我不知道我该怎么做。

4

1 回答 1

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. (例如,调用in而不是 in和 elsefirst的结果)。filterfind-project-member-recordremove-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)))))
于 2013-09-23T13:15:23.987 回答