all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@IRO.UMontreal.CA>
To: Ivan Kanis <ivan.kanis@googlemail.com>
Cc: emacs help <help-gnu-emacs@gnu.org>
Subject: Re: defining a setter function with gv.el
Date: Mon, 27 Aug 2012 16:28:32 -0400	[thread overview]
Message-ID: <jwvharo6r9e.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87vcg4cij6.fsf@googlemail.com> (Ivan Kanis's message of "Mon, 27 Aug 2012 20:10:53 +0200")

>> ;; (defun alist-get (key alist)
>> ;;   "Get the value associated to KEY in ALIST."
>> ;;   (declare
>> ;;    (gv-expander
>> ;;     (lambda (do)
>> ;;       (macroexp-let2 macroexp-copyable-p k key
>> ;;         (gv-letplace (getter setter) alist
>> ;;           (macroexp-let2 nil p `(assoc ,k ,getter)
>> ;;             (funcall do `(cdr ,p)
>> ;;                      (lambda (v)
>> ;;                        `(if ,p (setcdr ,p ,v)
>> ;;                           ,(funcall setter
>> ;;                                     `(cons (cons ,k ,v) ,getter)))))))))))
>> ;;   (cdr (assoc key alist)))
>> 

> Good grief, does it have to be so complicated for such a simple need?

I think so, yes.  CL's define-expander-macro would be a bit worse, but
otherwise comparable.  The expander macro itself above is:

        (macroexp-let2 macroexp-copyable-p k key
          (gv-letplace (getter setter) alist
            (macroexp-let2 nil p `(assoc ,k ,getter)
              (funcall do `(cdr ,p)
                       (lambda (v)
                         `(if ,p (setcdr ,p ,v)
                            ,(funcall setter
                                      `(cons (cons ,k ,v) ,getter)))))))))))

and the code we want to generate for a `setf' is along the lines of:

        (let* ((k KEY)
               (a ALIST)
               (p (assoc k (alist-getter a)))
               (v VAL))
          (if p (setcdr p v)
            (alist-setter a (cons (cons k v) (alist-getter a)))))

so it's not too far from the optimum (all those let-binding of KEY to
`k' etc... are needed if KEY is an expression that could have
side-effects so we need to make sure it's evaluated exactly once).

Note that the above expander can be used for things like (push VAL
(alist-get KEY ALIST)) as well, where we want to generate code like

        (let* ((k KEY)
               (a ALIST)
               (p (assoc k (alist-getter a)))
               (v VAL))
          (if p (setcdr p (cons v (cdr p)))
            (alist-setter a (cons (cons k (cons v (cdr p)))
                                  (alist-getter a)))))

Note that it doesn't work quite right for `letf', where

  (letf (((alist-get KEY ALIST) VAL)) BODY)

gets expanded to:

   ELISP> (macroexpand '(letf (((alist-get KEY ALIST) VAL)) BODY))
   (let* ((p (assoc KEY ALIST))
          (vnew VAL) (old (cdr p)))
     (unwind-protect
         (progn
           (if p (setcdr p vnew) (setq ALIST (cons (cons KEY vnew) ALIST)))
           BODY)
       (if p (setcdr p old) (setq ALIST (cons (cons KEY old) ALIST)))))

So to be correct for `letf' we could use something like:

   (defun alist-get (key alist)
     "Get the value associated to KEY in ALIST."
     (declare
      (gv-expander
       (lambda (do)
         (macroexp-let2 macroexp-copyable-p k key
           (gv-letplace (getter setter) alist
             (macroexp-let2 nil p `(assoc ,k ,getter)
               (funcall do `(cdr ,p)
                        (lambda (v)
                          `(if ,p (setcdr ,p ,v)
                             ,(funcall setter
                                       `(cons (setq ,p (cons ,k ,v))
                                              ,getter)))))))))))
     (cdr (assoc key alist)))

But for most uses this extra `setq' would be at best useless and might
make the code less efficient (a var that's never `setq'd can be
captured into a closure much more cheaply because we can just keep
a copy of its value, rather than keeping a reference to the variable
itself and looking up its value everytime).


       -- Stefan



  reply	other threads:[~2012-08-27 20:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-27  5:56 defining a setter function with gv.el Ivan Kanis
2012-08-27 12:54 ` Stefan Monnier
2012-08-27 18:10   ` Ivan Kanis
2012-08-27 20:28     ` Stefan Monnier [this message]
2012-08-30 16:05       ` Ivan Kanis
2012-08-31  0:59         ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvharo6r9e.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=help-gnu-emacs@gnu.org \
    --cc=ivan.kanis@googlemail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.