从记录列表中删除项目

时间:2013-09-23 09:34:44

标签: clojure

我有以下结构:

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

1 个答案:

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