Software Function Definition: How to get rid of "eval" here?

I have a set of functions called "ip", "date", "url", etc.

With this, I want to generate another set of functions: "ip-is", "date-is", etc.

I finally got the following solution, which works fine, but uses "eval".

(loop for name in '(ip date url code bytes referer user-agent) do
  (let ((c-name (intern (concatenate 'string (symbol-name name) "-IS"))))
    (eval `(defun ,c-name (c)
           #'(lambda (l) (equal (,name l) c))))))

Can someone help me how to get rid of the "evil eval"? It is very important for my program that the function names are presented as a list. So call for some marcro

   (define-predicate ip)
   (define-predicate date)
   (define-predicate url)

etc..

not fit my needs. I don't have a real problem with "eval", but I read very often that eval is considered bad and should be avoided if possible.

Thank Advance

+5
source share
2

. ( ) . :

(defmacro define-predicates (&rest names)
  `(progn
     ,@(loop
          for name in names
          collect (let ((c-sym (gensym))
                        (l-sym (gensym)))
                    `(defun ,(intern (concatenate 'string (symbol-name name) "-IS")) (,c-sym)
                       #'(lambda (,l-sym) (equal (,name ,l-sym) ,c-sym)))))))


(define-predicates ip date url)

, GENSYM . , , , .

+7

( , ), (setf fdefinition):

(loop for name in '(ip date url code bytes referer user-agent) do
  (let ((c-name (intern (concatenate 'string (symbol-name name) "-IS"))))
    (setf (fdefinition c-name)
          (lambda (c) (lambda (l) (equal (funcall name l) c))))))
+5

All Articles