unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: defining a setter function with gv.el
       [not found]     ` <jwvharo6r9e.fsf-monnier+emacs@gnu.org>
@ 2012-08-30 16:05       ` Ivan Kanis
  2012-08-31  0:59         ` Stefan Monnier
  0 siblings, 1 reply; 2+ messages in thread
From: Ivan Kanis @ 2012-08-30 16:05 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs devel

Hi Stefan,

As I was trying to make sense of it I tried the following in my
*scratch* buffer:

(setq lexical-binding t)

(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))))))))))))


(setq foo '((bar . foo) (baz . qux)))

(alist-get 'bar foo) => "Get the value associated to KEY in ALIST."

It's as if everything in declare was not evaluated. Where did I err?

Stefan Monnier <monnier@IRO.UMontreal.CA> wrote:

> 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)))))

Yes this is what needs to be generated. I don't understand why there
are two lambdas in alist-get. There is no map type function so I don't
understand what they do.

Is there any chance you would uncomment this function so that I can use
it in my code?

Take care,
-- 
Ivan Kanis
http://ivan.kanis.fr

History is a gallery of pictures in which there are few original and
many copies.
    -- Alexis de Tocqueville



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: defining a setter function with gv.el
  2012-08-30 16:05       ` defining a setter function with gv.el Ivan Kanis
@ 2012-08-31  0:59         ` Stefan Monnier
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier @ 2012-08-31  0:59 UTC (permalink / raw)
  To: Ivan Kanis; +Cc: emacs devel

> (setq lexical-binding t)

BTW, don't do that: instead, set it as a file-local variable with
"-*- lexical-binding: t -*-".

> (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))))))))))))

The (declare ...) form is used to give auxiliary info about the function
you're defining (such as info about how to indent it, how Edebug needs
to instrument it, or in our case how `setf' or `push' can "set" it).
The function itself ignores it.  So the definition above is like

   (defun alist-get (key alist)
     "Get the value associated to KEY in ALIST.")

I.e. you removed the body of the function (which was the last line:

   (cdr (assoc key alist))

).
     
> Yes this is what needs to be generated.  I don't understand why there
> are two lambdas in alist-get.

See C-h f gv-define-expander RET and `gv-get'.

> Is there any chance you would uncomment this function so that I can use
> it in my code?

There's a chance, but if you could explain why (push (cons KEY VAL) ALIST)
doesn't work for you, that would help,


        Stefan



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-08-31  0:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <87ehmsyj2b.fsf@googlemail.com>
     [not found] ` <jwvlih0ld2j.fsf-monnier+emacs@gnu.org>
     [not found]   ` <87vcg4cij6.fsf@googlemail.com>
     [not found]     ` <jwvharo6r9e.fsf-monnier+emacs@gnu.org>
2012-08-30 16:05       ` defining a setter function with gv.el Ivan Kanis
2012-08-31  0:59         ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).