[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: coercion to functions
- To: spector@mimsy.umd.edu (Lee Spector)
- Subject: Re: coercion to functions
- From: moon (David A. Moon)
- Date: Thu, 12 Sep 91 19:05:49 EDT
- Cc: info-mcl
> From: spector@mimsy.umd.edu (Lee Spector)
> Date: 12 Sep 91 16:28:07 GMT
>
> I'm working on something that does a lot of dynamic creation of functions,
> and this has lead me to a couple of observations and questions....
>
> (time (let ((f (eval `#'(lambda () 23))))
> (dotimes (n 500) (funcall f))))
>
> ;; while the COERCE version runs slowly
> (time (let ((f (coerce '(lambda () 23) 'function)))
> (dotimes (n 500) (funcall f))))
Another thing you can do is take advantage of other ways to construct
functions besides building S-expressions and compiling or interpreting
them. For example, (constantly 23) will make a function that always
returns 23. For more complex cases, you can use a closure. For example,
instead of
(eval `#'(lambda (x) (+ x ,y)))
you could use
(let ((y y)) #'(lambda (x) (+ x y)))
This will share the same compiled code among all the functions you generate,
but each will have its own value of y.
I don't know whether these techniques are applicable in your application,
but you should think about them if you have not done so already.