help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to quote a list of functions?


From: Pascal J. Bourguignon
Subject: Re: How to quote a list of functions?
Date: Sun, 09 Aug 2015 13:42:51 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Marcin Borkowski <mbork@mbork.pl> writes:

> On 2015-08-09, at 12:26, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
>
>> Marcin Borkowski <mbork@mbork.pl> writes:
>>
>>> On 2015-08-09, at 02:33, Pascal J. Bourguignon <pjb@informatimago.com> 
>>> wrote:
>>>
>>>> Marcin Borkowski <mbork@mbork.pl> writes:
>>>>
>>>>> OK, but this seems a bit awkward, especially for my second example,
>>>>> which would then become
>>>>>
>>>>> (list (list #'function argument) ...)
>>>>
>>>>   `((,(function f) ,(gen 42)))
>>>
>>> What do you mean by `gen'?  My Emacs (25.0.50.1) doesn't have such
>>> a function.
>>
>> Why do I mean by f?
>
> OK, but (AFAIUC) in the above form `function' gets evaluated (so that
> the symbol `f' -- the function name -- ends up in the list), and then
> Emacs tries to /evaluate/ the form `(gen 42)'.  This probably fooled me
> (again).  Do I get it right that you wanted to show how to generate the
> argument dynamically and not put a constant in the whole expression?

Right! 

I thought it would be obvious, because I misread what you wanted to do
when you wrote:

> (setq custom-format '((format-field-one 4) ...))?

I thought you wanted to generate a field formater function created by
the expression: (format-field-one 4).

But now I'm reading:

> (defun format-field-one (record width)
>    (format (format "%%%ds" width) (cdr (assoc 'field-one record))))
>
> I want to be able to include in my custom "format" things like
>
> (format-field-one 4)

and I realize that (format-field-one 4) is meaningless.



I'm sorry, I'm often misled when people say there's a problem when there
is no problem:

    (defun format-field-one (record width)
      (format (format "%%%ds" width) (cdr (assoc 'field-one record))))

    (setq custom-format '((format-field-one 4)))

    (let ((record-list '(((field-one . 33))
                         ((field-one . 42)))))
        (mapcar (lambda (record)
                  (mapcar (lambda (field-specifier)
                            (apply (car field-specifier)
                                   record
                                   (cdr field-specifier)))
                          custom-format))
                record-list))
    --> (("  33") ("  42"))


And as seen in the rest of the discussion, writting it as:

    (setq custom-format `((,#'format-field-one 4)))
    --> ((format-field-one 4))

instead of:

    (setq custom-format '((format-field-one 4)))
    --> ((format-field-one 4))

wouldn't change anything in emacs lisp.

On the other hand, you could write a function to generate a closure to
format fields:

    (setf lexical-binding t) ; why isn't it the default already?

    (defun generate-field-formatter (width)
      (let ((format-control (format "%%%ds" width)))
        (lambda (record)
          (format format-control (cdr (assoc 'field-one record))))))


    (setq custom-format `(,(generate-field-formatter 4)))
    ;; --> ((closure ((format-control . "%4s") (width . 4) t) (record) (format 
format-control (cdr (assoc (quote field-one) record)))))

    (let ((record-list '(((field-one . 33))
                         ((field-one . 42)))))
        (mapcar (lambda (record)
                  (mapcar (lambda (field-formatter)
                            (funcall field-formatter record))
                          custom-format))
                record-list))
    ;; --> (("  33") ("  42"))

and here, I discover an horrible bug in emacs-version "24.3.1":

    (byte-compile 'generate-field-formatter)
    ;; --> #[(width) "\301\302\"\210\303\207" [width format "%%%ds" #[(record) 
"\302\303\304  \"A\"\207" [format-control record format assoc field-one] 5]] 3]

    (setq custom-format `(,(generate-field-formatter 4)))
    ;; --> (#[(record) "\302\303\304   \"A\"\207" [format-control record 
format assoc field-one] 5])

    (let ((record-list '(((field-one . 33))
                         ((field-one . 42)))))
        (mapcar (lambda (record)
                  (mapcar (lambda (field-formatter)
                            (funcall field-formatter record))
                          custom-format))
                record-list))
    Debugger entered--Lisp error: (void-variable format-control)


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]