unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Calling different kinds of functions, which finish the same job
@ 2006-04-11  8:12 Herbert Euler
  2006-04-11 15:49 ` Kevin Rodgers
  0 siblings, 1 reply; 8+ messages in thread
From: Herbert Euler @ 2006-04-11  8:12 UTC (permalink / raw)


Hello,

Assume there are some functions, for instance, f1, f2, f3,
all of them are doing the same job, but with different arguments.
If I want to call one of them, but don't know which one is
actually called, how can I do that?  I've written two macros
like

(defmacro xgp-casi2-safe-call-iter (func largs)
  `(if ,largs
       (condition-case nil
           (apply ,func (car ,largs))
         (error (xgp-casi2-safe-call-iter ,func (cdr ,largs))))))

(defmacro xgp-casi2-safe-call (func largs)
  "Call function FUNC, try each element of LARGS, which is a list of list, 
as argument to FUNC.
First call without arguments."
  `(condition-case nil
       (apply ,func)
     (error (xgp-casi2-safe-call-iter ,func ,largs))))

And I will call it with

    (xgp-casi2-safe-call function '((args of f1) (args of f2) (args of f3)))

This is my solution.  Any suggestions?  Thanks in advance.

Regards,
Guanpeng Xu

_________________________________________________________________
Don’t just search. Find. Check out the new MSN Search! 
http://search.msn.click-url.com/go/onm00200636ave/direct/01/

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

* Re: Calling different kinds of functions, which finish the same job
  2006-04-11  8:12 Calling different kinds of functions, which finish the same job Herbert Euler
@ 2006-04-11 15:49 ` Kevin Rodgers
  2006-04-11 17:31   ` Herbert Euler
  0 siblings, 1 reply; 8+ messages in thread
From: Kevin Rodgers @ 2006-04-11 15:49 UTC (permalink / raw)


[This doesn't belong on emacs-devel]

Herbert Euler wrote:
 > Assume there are some functions, for instance, f1, f2, f3,
 > all of them are doing the same job, but with different arguments.
 > If I want to call one of them, but don't know which one is
 > actually called, how can I do that?  I've written two macros
 > like
 >
 > (defmacro xgp-casi2-safe-call-iter (func largs)
 >  `(if ,largs
 >       (condition-case nil
 >           (apply ,func (car ,largs))
 >         (error (xgp-casi2-safe-call-iter ,func (cdr ,largs))))))
 >
 > (defmacro xgp-casi2-safe-call (func largs)
 >  "Call function FUNC, try each element of LARGS, which is a list of
 > list, as argument to FUNC.
 > First call without arguments."
 >  `(condition-case nil
 >       (apply ,func)
 >     (error (xgp-casi2-safe-call-iter ,func ,largs))))
 >
 > And I will call it with
 >
 >    (xgp-casi2-safe-call function '((args of f1) (args of f2) (args of 
f3)))
 >
 > This is my solution.  Any suggestions?  Thanks in advance.

What are "args of f1" etc?  They are apparently not the arguments to a
single call to f1, since you have these variations:

(apply ,func) ; this is tried first
(apply ,func (car ,largs)) ; then this is tried, while cdr'ing down largs

If all of the functions are defined, why would any of the function call
signal an error?  Why do you care which function is actually called?
You certainly don't return that information, you only return the result
of the first non-error-signalling call.

-- 
Kevin Rodgers

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

* Re: Calling different kinds of functions, which finish the same job
  2006-04-11 15:49 ` Kevin Rodgers
@ 2006-04-11 17:31   ` Herbert Euler
  2006-04-11 17:36     ` Herbert Euler
  2006-04-11 18:36     ` Kevin Rodgers
  0 siblings, 2 replies; 8+ messages in thread
From: Herbert Euler @ 2006-04-11 17:31 UTC (permalink / raw)
  Cc: help-gnu-emacs

>From: Kevin Rodgers <ihs_4664@yahoo.com>
>To: help-gnu-emacs@gnu.org
>Subject: Re: Calling different kinds of functions, which finish the same 
>job
>Date: Tue, 11 Apr 2006 09:49:29 -0600
>
>[This doesn't belong on emacs-devel]

OK.  Sorry for having sent to emacs-devel.

>What are "args of f1" etc?  They are apparently not the arguments to a 
>single call to f1, since you have these variations:
>
>(apply ,func) ; this is tried first
>(apply ,func (car ,largs)) ; then this is tried, while cdr'ing down largs
>
>If all of the functions are defined, why would any of the function call 
>signal an error?  Why do you care which function is actually called?
>You certainly don't return that information, you only return the result of 
>the first non-error-signalling call.

Suppose functions f1, f2, and f3 are all for inserting one space, but
without side effect or with different side effect;  f1 requires an
integeral argument as count, f2 requires an symbolic argument specifies
how the side effect is caused, and f3 requires two arguments, one
of them is an integer, the other one is a symbol.  Now, if I want to
insert one space (either with or without side effect), I can issue one
of the following commands:

    (f1 1)
    (f2 'indent)
    (f3 1 'indent)

And I'm granteed that, if one space is inserted, one of f1, f2, f3 is
called, but I don't which one is called.  So I want I can write (with
function in my last post),

    (xgp-casi2-safe-call f '((1) (indent) (1 indent)))

Where f is one of f1, f2, or f3 (got from environment).  If f can be
self-insert-command as well, the first trial will be successful  (so
perhaps I should make invoking without arguments the last trial).

Any better solutions?  Thanks.

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

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

* Re: Calling different kinds of functions, which finish the same job
  2006-04-11 17:31   ` Herbert Euler
@ 2006-04-11 17:36     ` Herbert Euler
  2006-04-11 18:36     ` Kevin Rodgers
  1 sibling, 0 replies; 8+ messages in thread
From: Herbert Euler @ 2006-04-11 17:36 UTC (permalink / raw)


>Suppose functions f1, f2, and f3 are all for inserting one space

Sorry.  Should be "for inserting spaces".

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

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

* Re: Calling different kinds of functions, which finish the same job
  2006-04-11 17:31   ` Herbert Euler
  2006-04-11 17:36     ` Herbert Euler
@ 2006-04-11 18:36     ` Kevin Rodgers
  2006-04-12  0:50       ` Herbert Euler
  1 sibling, 1 reply; 8+ messages in thread
From: Kevin Rodgers @ 2006-04-11 18:36 UTC (permalink / raw)


Herbert Euler wrote:
>> From: Kevin Rodgers <ihs_4664@yahoo.com>
>> To: help-gnu-emacs@gnu.org
>> Subject: Re: Calling different kinds of functions, which finish the 
>> same job
>> Date: Tue, 11 Apr 2006 09:49:29 -0600
>>
>> [This doesn't belong on emacs-devel]
> 
> 
> OK.  Sorry for having sent to emacs-devel.
> 
>> What are "args of f1" etc?  They are apparently not the arguments to a 
>> single call to f1, since you have these variations:
>>
>> (apply ,func) ; this is tried first
>> (apply ,func (car ,largs)) ; then this is tried, while cdr'ing down largs
>>
>> If all of the functions are defined, why would any of the function 
>> call signal an error?  Why do you care which function is actually called?
>> You certainly don't return that information, you only return the 
>> result of the first non-error-signalling call.
> 
> 
> Suppose functions f1, f2, and f3 are all for inserting one space, but
> without side effect or with different side effect;  f1 requires an
> integeral argument as count, f2 requires an symbolic argument specifies
> how the side effect is caused, and f3 requires two arguments, one
> of them is an integer, the other one is a symbol.  Now, if I want to
> insert one space (either with or without side effect), I can issue one
> of the following commands:
> 
>    (f1 1)
>    (f2 'indent)
>    (f3 1 'indent)
> 
> And I'm granteed that, if one space is inserted, one of f1, f2, f3 is
> called, but I don't which one is called.  So I want I can write (with
> function in my last post),
> 
>    (xgp-casi2-safe-call f '((1) (indent) (1 indent)))
 >
> Where f is one of f1, f2, or f3 (got from environment).

No you can't, because your macros call

(f1)
(f1 1)
(f2)
(f2 indent)
(f3)
(f3 1)
(f3 indent)

> If f can be
> self-insert-command as well, the first trial will be successful  (so
> perhaps I should make invoking without arguments the last trial).
> 
> Any better solutions?  Thanks.

(condition-case nil
     (f1 1)
   (error (condition-case nil
              (f2 'indent)
            (error (f3 1 'indent)))))

(defmacro try (&rest forms)
   "Eval FORMS until one of them returns without signalling an error."
   `(condition-case nil
        ,(car forms)
      (error (try ,@(cdr forms)))))

(try (f1 1) (f2 'indent) (f3 1 'indent))

-- 
Kevin Rodgers

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

* Re: Calling different kinds of functions, which finish the same job
  2006-04-11 18:36     ` Kevin Rodgers
@ 2006-04-12  0:50       ` Herbert Euler
  0 siblings, 0 replies; 8+ messages in thread
From: Herbert Euler @ 2006-04-12  0:50 UTC (permalink / raw)
  Cc: help-gnu-emacs

>From: Kevin Rodgers <ihs_4664@yahoo.com>
>To: help-gnu-emacs@gnu.org
>Subject: Re: Calling different kinds of functions, which finish the same 
>job
>Date: Tue, 11 Apr 2006 12:36:55 -0600
>
>No you can't, because your macros call
>
>(f1)
>(f1 1)
>(f2)
>(f2 indent)
>(f3)
>(f3 1)
>(f3 indent)

Sorry for the unclear description.  I may set f by

    (setq f (key-binding " "))

So, f can be only one of f1, f2, f3.  Suppose f is f3, then
my macro will call

    (f3)
    (f3 1)
    (f3 'indent)
    (f3 1 'indent)

The last one will be the one actually executed.

This macro only (with help from Lisp interpreter) checks
the correctness of arguments of functions, which is often
insufficient, since a call may fail even if arguments are right.
So I wonder if there are better sulotions.

Regards,
Guanpeng Xu

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! 
http://search.msn.com/

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

* Re: Calling different kinds of functions, which finish the same job
       [not found] <mailman.318.1144803053.9609.help-gnu-emacs@gnu.org>
@ 2006-04-12  3:55 ` Stefan Monnier
  2006-04-12 15:28   ` Kevin Rodgers
  0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2006-04-12  3:55 UTC (permalink / raw)


> Sorry for the unclear description.  I may set f by

>    (setq f (key-binding " "))

> So, f can be only one of f1, f2, f3.  Suppose f is f3, then

Maybe the answer is quite different:

   (execute-command f)


-- Stefan

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

* Re: Calling different kinds of functions, which finish the same job
  2006-04-12  3:55 ` Stefan Monnier
@ 2006-04-12 15:28   ` Kevin Rodgers
  0 siblings, 0 replies; 8+ messages in thread
From: Kevin Rodgers @ 2006-04-12 15:28 UTC (permalink / raw)


Stefan Monnier wrote:
>>Sorry for the unclear description.  I may set f by
> 
> 
>>   (setq f (key-binding " "))
> 
> 
>>So, f can be only one of f1, f2, f3.  Suppose f is f3, then
> 
> 
> Maybe the answer is quite different:
> 
>    (execute-command f)

I think you mean: (command-execute f)

,----[ C-h f command-execute RET ]
| command-execute is a built-in function.
| (command-execute CMD &optional RECORD-FLAG KEYS SPECIAL)
|
| Execute CMD as an editor command.
| CMD must be a symbol that satisfies the `commandp' predicate.
| Optional second arg RECORD-FLAG non-nil
| means unconditionally put this command in `command-history'.
| Otherwise, that is done only if an arg is read using the minibuffer.
| The argument KEYS specifies the value to use instead of 
(this-command-keys)
| when reading the arguments; if it is nil, (this-command-keys) is used.
| The argument SPECIAL, if non-nil, means that this command is executing
| a special event, so ignore the prefix argument and don't clear it.
`----

-- 
Kevin Rodgers

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

end of thread, other threads:[~2006-04-12 15:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-11  8:12 Calling different kinds of functions, which finish the same job Herbert Euler
2006-04-11 15:49 ` Kevin Rodgers
2006-04-11 17:31   ` Herbert Euler
2006-04-11 17:36     ` Herbert Euler
2006-04-11 18:36     ` Kevin Rodgers
2006-04-12  0:50       ` Herbert Euler
     [not found] <mailman.318.1144803053.9609.help-gnu-emacs@gnu.org>
2006-04-12  3:55 ` Stefan Monnier
2006-04-12 15:28   ` Kevin Rodgers

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