Updating a postmodern row in a more functional way

跟風遠走 提交于 2020-01-07 00:34:29

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/6723173/updating-a-postmodern-row-in-a-more-functional-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!