tags:

views:

474

answers:

3

Hello,

I have a big list of global variables that each have thier own setup function. My goal is to go through this list, call it's setup function, and generate some stats on the data loaded in.

The globals and thier setup functions are case-sensitive (this came from xml and is necessary for uniqueness).

The data looks something like this
'(ABCD ABC\d AB\c\d ...) and the setup fns look like:
(defun setup_ABCD...
(defun setup_ABC\d...

I've tried concatenating them together and turning the resulting string into a function but this interferes with the namespace of the previously loaded setup function.

(make-symbol(concatenate 'string "setup_" (symbol-name(first '(abc\d)))))

But funcalling this doesn't work

Any ideas? Thanks in advance

+6  A: 

It's because MAKE-SYMBOL returns an uninterned symbol. You should use INTERN instead.

Nowhere man
BINGO! I was hoping it would be that simple, Thanks a bunch!
Chet
Well, Chet, next step: accept the answer.
Svante
+2  A: 

I agree with Nowhere man on the intern -- you do need to specify in what package the symbol lives you want to invoke.

But I also ask myself: where do these setup functions come from? Wouldn't it possibly be easier to just insert some anonymous functions in a hash table referenced by these names?

HD
A: 

I'd either use INTERN or (possibly, you'd have to profile to be 100% sure it's helpful) a helper function that does the string concatenation and the initial find, then caches the result in a hashtable (keyed off the original symbol). That may be faster than a pure INTERN/CONCATENATE solution, would potentially generate less transient garbage, but would probalby end up using more long-term storage.

Something along the lines of:

(defvar *symbol-function-map* (make-hash-table))

(defun lookup-symbol (symbol)
  (or (gethash symbol *symbol-function-map*)
      (let ((function-name (intern (concatenate 'string "setup_" (symbol-name symbol)))))
        (setf (gethash symbol *symbol-function-map*) (symbol-function function-name)))))

If you require the name of the function, rather than the function itself, leave out the call to SYMBOL-FUNCTION.

Vatine