我正在尝试用Clojure中的defrecord
创建我自己的不可变数据类型/方法。我们的目标是有一个数据类型,我可以创建它的实例,然后调用它的方法来返回带有变异变量的自身的新副本。假设a和b是向量。我想在两者中更新一个值,并返回整个结构的一个新副本,其中包含更新后的向量。这显然不能编译,我只是想让大家理解我的想法。
(defrecord MyType [a b]
(constructor [N]
; I'd like to build an initial instance, creating a and b as vectors of length N
)
(mutate-and-return []
; I'd like to mutate (assoc the vectors) and return the new structure, a and b modified
)
)
我想调用构造函数,然后调用赋值函数(还有其他函数不会发生变化,但我不想让这个问题变得更复杂)。
或者,如果这不是惯用的Clojure,那么您应该如何做这样的事情?
发布于 2020-12-10 21:43:29
Clojure允许您创建记录,这些记录是自定义的maplike数据类型。它们类似于maplike,因为它们将键与值相关联,您可以像使用map一样查找它们的值,并且它们像map一样是不可变的。
(defrecord Person [last first address])
;=> core.Person
(defrecord Ad [street city zip])
;=> core.Ad
(def p1 (Person. "Jhon" "Mick"
(Ad. "US187956" "NY" 3369)))
;=> #'core/p1
(update-in p1 [:address :zip] inc)
;=> #core.Person{:last "Jhon", :first "Mick", :address #playsync.core.Ad{:street "US187956", :city "NY", :zip 3370}}
(assoc p1 :last "Adam")
;=> #core.Person{:last "Adam", :first "Mick", :address #playsync.core.Ad{:street "US187956", :city "NY", :zip 3370}}
https://stackoverflow.com/questions/11958027
复制相似问题