以更实用的方式更新后现代行 [英] Updating a postmodern row in a more functional way

查看:110
本文介绍了以更实用的方式更新后现代行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个类(即 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阻碍了你的工作?

解决方案

你正在修改状态,所以这就是你写的。



我只看到了两个问题:


  1. 您的 defmethod 的lambda列表混淆了:它应该是(to-lend book),而不是其他方式。这应该会给出一些警告或错误。
  2. 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:

  1. 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.
  2. The participle of "lend" is "lent", so the slots should be named book-lent and book-lent-to.

这篇关于以更实用的方式更新后现代行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆