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

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

Re: Making alist that executes multiple commands


From: Stephen Berman
Subject: Re: Making alist that executes multiple commands
Date: Mon, 25 Nov 2024 21:31:13 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

On Mon, 25 Nov 2024 16:36:46 +0000 Heime <heimeborgia@protonmail.com> wrote:

> Sent with Proton Mail secure email.
>
> On Tuesday, November 26th, 2024 at 2:58 AM, Stephen Berman
> <stephen.berman@gmx.net> wrote:
>
>> On Mon, 25 Nov 2024 13:10:57 +0000 Heime heimeborgia@protonmail.com wrote:
>>
>> > Sent with Proton Mail secure email.
>> >
>> > On Monday, November 25th, 2024 at 9:40 PM, Stephen Berman
>> > stephen.berman@gmx.net wrote:
>> >
>> > > On Mon, 25 Nov 2024 01:05:10 +0000 Heime heimeborgia@protonmail.com 
>> > > wrote:
>> > >
>> > > > On Monday, November 25th, 2024 at 11:39 AM, Heime via Users list for 
>> > > > the GNU
>> > > > Emacs text editor help-gnu-emacs@gnu.org wrote:
>> > > >
>> > > > > Sent with Proton Mail secure email.
>> > > > >
>> > > > > On Monday, November 25th, 2024 at 11:28 AM, Stephen Berman
>> > > > > stephen.berman@gmx.net wrote:
>> > > > >
>> > > > > > On Sun, 24 Nov 2024 23:13:51 +0000 Heime
>> > > > > > heimeborgia@protonmail.com wrote:
>> > > > > >
>> > > > > > > On Monday, November 25th, 2024 at 10:56 AM, Stephen Berman
>> > > > > > > stephen.berman@gmx.net wrote:
>> > > > > > >
>> > > > > > > > On Sun, 24 Nov 2024 21:51:38 +0000 Heime via Users list for
>> > > > > > > > the GNU Emacs
>> > > > > > > > text editor help-gnu-emacs@gnu.org wrote:
>> > > > > > > >
>> > > > > > > > > What changes can I make to the following to allow me to
>> > > > > > > > > execute more
>> > > > > > > > > commands than just one (as in alkotr-ar and alkotr-go).
>> > > > > > > > >
>> > > > > > > > > For ar I want to call functions alkotr-ar and alkotr-af
>> > > > > > > > >
>> > > > > > > > > (let ( (lookup-alist
>> > > > > > > > > '((ar . alkotr-ar)
>> > > > > > > > > (go . alkotr-go))))
>> > > > > > > > >
>> > > > > > > > > (dolist (actm symbol-list)
>> > > > > > > > > (let ((func (cdr (assoc actm lookup-alist))))
>> > > > > > > > > (if func
>> > > > > > > > > (funcall func)
>> > > > > > > > > (message "ACTM Unrecognised: %s%s" "'" actm)))))
>> > > > > > > >
>> > > > > > > > Something like this:
>> > > > > > > >
>> > > > > > > > (let ((symbol-list '(ar go))
>> > > > > > > > (lookup-alist '((ar alkotr-ar alkotr-af)
>> > > > > > > > (go alkotr-go alkotr-gc))))
>> > > > > > > > (dolist (actm symbol-list)
>> > > > > > > > (let ((fnlist (cdr (assoc actm lookup-alist))))
>> > > > > > > > (while fnlist
>> > > > > > > > (let ((func (pop fnlist)))
>> > > > > > > > (if (functionp func)
>> > > > > > > > (funcall func)
>> > > > > > > > (message "ACTM Unrecognised: %s%s" "'" actm)))))))
>> > > > > > > >
>> > > > > > > > Steve Berman
>> > > > > > >
>> > > > > > > Have thought about this. Any criticisms about it?
>> > > > > > >
>> > > > > > > '((ar . (lambda ()
>> > > > > > > (alkotr-ar)
>> > > > > > > (alkotr-af)))
>> > > > > > >
>> > > > > > > (go . (lambda ()
>> > > > > > > (alkotr-go)
>> > > > > > > (alkotr-gc))))
>> > > > > >
>> > > > > > That seems fine if the functions take no arguments, though 
>> > > > > > probably not
>> > > > > > as flexible as looping over a list.
>> > > > >
>> > > > > Could you explain? Can't I do
>> > > > >
>> > > > > (go . (lambda ()
>> > > > > (alkotr-go go)
>> > > > > (alkotr-gc gc))
>> > >
>> > > Yes (but as Stefan Monnier pointed out and I overlooked, you have to
>> > > evaluate the lambda expressions). I was just referring to the specific
>> > > function calls you used.
>> > >
>> > > > > > > What would you suggest for function commands requiring arguments,
>> > > > > > > e.g. (alkotr-ar ar) and (alkotr-af af)?
>> > > > > >
>> > > > > > (funcall 'alkotr-ar ar)
>> > > > > > (funcall 'alkotr-ar af)
>> > > > >
>> > > > > How would the above solution fit with argument incorporation within
>> > > > > lookup-alist?
>> > >
>> > > I'm not sure what you're asking here.
>> > >
>> > > > Does one use
>> > > >
>> > > > (lookup-alist '( (ar (alkotr-ar arg-ar) (alkotr-af arg-af))
>> > > > (go (alkotr-go arg-go) (alkotr-gc arg-gc))) ))
>> > >
>> > > Use it for what?
>> > >
>> > > Steve Berman
>> >
>> > (defun fpln-test (symbol-list)
>>
>> ^^^^^^^^^^^
>>
>> > (let ((symbol-list '(ar go))
>>
>> ^^^^^^^^^^^^^^^^^^^^^^
>>
>> It doesn't make sense to pass the value of a variable as a function
>> argument and also unconditionally bind the same variable in the body of
>> the function before it's used (unless the use is outside of the scope of
>> the binder, which it isn't here).
>
> Right, that line should not be there.
>
>> > (lookup-alist '((ar alkotr-ar alkotr-af)
>> > (go alkotr-go alkotr-gc))))
>> >
>> > (dolist (actm symbol-list)
>> > (let ((fnlist (cdr (assoc actm lookup-alist))))
>> > (while fnlist
>> > (let ((func (pop fnlist)))
>> > (if (functionp func)
>> > (funcall func)
>> > (message "ACTM Unrecognised: %s%s" "'" actm))))))))
>> >
>> > You suggested the use use of
>> >
>> > (funcall 'alkotr-ar ar)
>> > (funcall 'alkotr-ar af)
>> >
>> > Where are they to be introduced?
>> >
>> > I was planning to introduce the arguments in the lookup-alist.
>> > So that for ar one can include arguments to alkotr-ar and
>> > alkotr-af.
>>
>>
>> If each argument is passed to only one function, then using a list of
>> function calls as the value of each alist element seems reasonable. If
>> the functions and arguments can be (more) freely combined, looping over
>> lists of these seems programmatically cleaner. But either way should
>> work.
>>
>> Steve Berman
>
> Would be good to allow arguments to be freely combined.
> But how does one loop over lists of these arguments from the
> code you posted?

Something like this, for example:

(dolist (f '(+ - list))
  (dolist (a '(1 2 3))
    (funcall f a)))

> I could have the following, but got some difficulties about
> how to execute each command with its arguments, in the way you
> suggest.
>
> (defun fpln-test (symbol-list)
>   (let ( (lookup-alist '((ar ((alkotr-ar arg-this)
>                               (alkotr-af arg-that arg-other)))
>                          (go ((alkotr-go arg-dim)
>                               (alkotr-gc arg-dum arg-sum))) )) )
>
>     (dolist (actm symbol-list)
>       (let ((commands (cdr (assoc actm lookup-alist))))
>         (while commands
>             (dolist (cmd commands)
>               (apply (car cmd) (cdr cmd)))
>           (message "Key '%s' not found in lookup-alist" key))))))
>
>

When I wrote "using a list of function calls as the value of each alist
element seems reasonable" I was assuming you wanted them in lambda
expressions, where they are evaluated, as in the example you gave
previously.  You can't use a function call as the first argument of
`apply' or `funcall' because a function call (which is just a list whose
first element is a function) is not a function.

On Mon, 25 Nov 2024 17:59:22 +0000 Heime <heimeborgia@protonmail.com> wrote:

> I get error when calling (marnap '(armg go))
>
> That is I get message "ACTM Unrecognised: 'armg"
>
>
>
> (defun marnap (&optional actm-service)
>
>   (let ( (lookup-alist '((armg ((add-to-list 'load-path 
> (marnap-sec-fpln-waypt "NAPLN"))
>                                 (require 'napyon)))
>
>                          (go   ((napyon 'go))) )) )
>
>     (dolist (actm actm-service)
>       (let ((fnlist (cdr (assoc actm lookup-alist))))
>         (while fnlist
>           (dolist (cmd fnlist)
>             (if (functionp (car cmd))
>                 (apply (car cmd) (cdr cmd))
>               (message "ACTM Unrecognised: '%s\n" actm)))))) ))

This is for the same reason as above: the first invocation of `(car
cmd)' returns `(add-to-list 'load-path (marnap-sec-fpln-waypt
"NAPLN"))', which is a function call, not a function.

Steve Berman



reply via email to

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