Re: automatically call functions based on arguments
- From: pjb@xxxxxxxxxxxxxxxxx (Pascal J. Bourguignon)
- Date: Wed, 30 Apr 2008 09:09:47 +0200
normanj <nii2awh@xxxxxxxxx> writes:
Sorry, those digits should be replaced with normal strings.
Now, the presence of these digits is strange. Couldn't they be
parameters to the functions? Do you have a lot of them?
All those parameters have nothing to do with the parameters
of functions/macros, they are only related with their names.
Yes, maybe hundreds.
(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))
This could be taken as a solution.
Given the number of functions, it might be worthwhile to use a
hash-table for the map.
(defvar *name-to-function-map*
(make-hash-table :test (function equal))) ; case sensitive strings, or else equalp
(dolist (pair '( (func-aa-1-2 . (aa one two)) ; which is the same to write (func-aa aa one two)
(func-bb-3-2 . (bb three two))
... ))
(setf (gethash (cdr pair) *name-to-function-map*) (car pair)))
and then use (gethash name *name-to-function-map*) instead of (cdr (assoc ...)),
with name bound to (aa one two), etc.
--
__Pascal Bourguignon__
.
- References:
- automatically call functions based on arguments
- From: normanj
- Re: automatically call functions based on arguments
- From: normanj
- Re: automatically call functions based on arguments
- From: normanj
- automatically call functions based on arguments
- Prev by Date: Re: Questions - Higer Order Functions
- Next by Date: Re: def app = apply, problem
- Previous by thread: Re: automatically call functions based on arguments
- Next by thread: Re: automatically call functions based on arguments
- Index(es):
Relevant Pages
|