Re: automatically call functions based on arguments



normanj <nii2awh@xxxxxxxxx> writes:

One solution that came to my mind is that save all available rules in
a file, like:
(AA 1 2 func-aa-1-1)
(AA 2 1 func-aa-2-1)
(BB 1 3 func-bb-1-3)

In "call-function", first read the file, then retrieve the function
name by searching. If no matching functions, report an error.

As the rules may change, I don't want these rules to be hard-coded in
the program.
My solution can meet my requirement, but is there a better solution?

Yes, this is what you should do.
The rules can be saved in the sources, there's not necessarily a need
for an external file.

Now, the presence of these digits is strange. Couldn't they be
parameters to the functions? Do you have a lot of them?


(defvar *name-to-function-map*
'((aa func-aa)
(bb func-bb)
(cc func-cc)))

(defun call-function (name arguments)
(let ((fun (cdr (assoc name *name-to-function-map*))))
(if fun
(apply fun arguments)
(error "No user function named ~S" name))))

(call-function 'aa 1 2)
(call-function (read) (read) (read))



--
__Pascal Bourguignon__
.