我在下面创建了参考“ people”,并希望函数delete-person从事务内的数据结构中删除项目。
(defrecord Person [name favorite-color])
(def people (ref ()))
(defn add-person [new-person]
(dosync
(alter people conj new-person)))
(add-person (Person. "Joe" "Red"))
(add-person (Person. "Sam" "Blue"))
;; how do I make this function work?
;; would @people (destructured) have to be the second argument to filter?
(defn delete-person [name-to-delete]
"Delete named person from ref"
(dosync
(alter people filter #(not= (:name %) name-to-delete))))
(delete-person "Joe")
IllegalArgumentException Don't know how to create ISeq from:
user$delete_person$fn__1407$fn__1408 clojure.lang.RT.seqFrom (RT.java:542)
下面的函数起作用是因为我过滤了非结构化的ref,但是如何在事务中更改数据呢?
(filter #(not= (:name %) "Sam") @people)
=> (#user.Person{:name "Joe", :favorite-color "Red"})