以更实用的方式更新后现代行 [英] Updating a postmodern row in a more functional way
问题描述
我有几个类(即 book
和 user
)。我需要更新 book
,方法是将 lended
槽位设置为 t
及其借贷至
借方的 id
。
我使用Postmodern作为PostgreSQL数据库的后端
这就是我想到的(我希望名称足够自描述)
(defmethod lend-book((to-lend book)借方);借方是用户实例
(if (借出借出)
无
(让(出借)(get-dao'book(book-id to-lend)))
(setf(book-lent- )
(setf(book-lent to-lend)t)
(update-dao to-lend))))
但是这对我来说太过于迫切了。
这样做的功能方式还是PostModern阻碍了你的工作?
你正在修改状态,所以这就是你写的。
我只看到了两个问题:
- 您的
defmethod
的lambda列表混淆了:它应该是(to-lend book)
,而不是其他方式。这应该会给出一些警告或错误。
- lend的分词是借出的,所以这些插槽应该被命名为
book-lent
和book-lent-to
。
I have a couple of classes (namely book
and user
). I need to update a book
by setting its lended
slot to t
and its lended-to
to the borrower's id
.
I'm using Postmodern as a back-end to a PostgreSQL database
This is what I came up with (I hope the names are self-describing enough)
(defmethod lend-book ((to-lend book) borrower) ;borrower is a user instance
(if (book-lent to-lend)
nil
(let (to-lend (get-dao 'book (book-id to-lend)))
(setf (book-lent-to to-lend) (user-id borrower))
(setf (book-lent to-lend) t)
(update-dao to-lend))))
But it seems too much imperative to me.
Is there a more functional way to do this or does Postmodern get in the way?
You are modifying state, so that's what you write. I see this as idiomatic.
I just see two problems with your code:
- Your
defmethod
has its lambda list mixed up: it should be(to-lend book)
, not the other way around. This should give some warnings or errors. - The participle of "lend" is "lent", so the slots should be named
book-lent
andbook-lent-to
.
这篇关于以更实用的方式更新后现代行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!