Suppose I have two functions: F and G. F calls G once with arguments
related to the meaning of F. I'd like not to call G each time F
called. Is it possible to have something like "Call G when F being
compiled and always use the returned result when F will actually be
called" ? I wish not exactly this above, only not to call G many
times. All G's arguments are numbers and/or strings.
--
Vladimir Zolotykh ······@eurocom.od.ua
"Vladimir Zolotykh" <······@eurocom.od.ua> wrote in message
······················@eurocom.od.ua...
> Suppose I have two functions: F and G. F calls G once with arguments
> related to the meaning of F. I'd like not to call G each time F
> called. Is it possible to have something like "Call G when F being
> compiled and always use the returned result when F will actually be
> called" ? I wish not exactly this above, only not to call G many
> times. All G's arguments are numbers and/or strings.
>
Sounds like G is a macro.
Coby
--
(remove #\space "coby . beck @ opentechgroup . com")
Vladimir Zolotykh <······@eurocom.od.ua> writes:
> Suppose I have two functions: F and G. F calls G once with arguments
> related to the meaning of F. I'd like not to call G each time F
> called. Is it possible to have something like "Call G when F being
> compiled and always use the returned result when F will actually be
> called" ? I wish not exactly this above, only not to call G many
> times. All G's arguments are numbers and/or strings.
>
There are several ways you could do this. If G is defined when F is loaded
then you could use (load-time-value (G ...)). Note that you *can't* rely
on the lexical environment of F if you do this, but if you want something
to happen at compile time you can't rely on that anyway. If you do
need to call G `properly' - when F is actually called, but only once,
you could do an ad-hoc memoizing trick:
(let ((gr (load-time-value (cons nil nil))))
(or (car gr)
(setf (car gr) (g ...))))
There are other things you could do, of course.
--tim
Vladimir Zolotykh wrote:
>
> ....I wish not exactly this above, only not to call G many
> times. All G's arguments are numbers and/or strings.
I think #.(G...) give me what I look for. At load time, though.
Sorry.
--
Vladimir Zolotykh ······@eurocom.od.ua
Vladimir Zolotykh <······@eurocom.od.ua> writes:
> Vladimir Zolotykh wrote:
> >
> > ....I wish not exactly this above, only not to call G many
> > times. All G's arguments are numbers and/or strings.
>
> I think #.(G...) give me what I look for. At load time, though.
That is the read time.
--
Janis Dzerins
Eat shit -- billions of flies can't be wrong.
Vladimir Zolotykh <······@eurocom.od.ua> writes:
> I think #.(G...) give me what I look for. At load time, though.
> Sorry.
No, this is at read time which is much earlier than load time, and is more
likely to be just before compile time & macro expansion time.
--tim
Tim Bradshaw <···@tfeb.org> writes:
> Vladimir Zolotykh <······@eurocom.od.ua> writes:
>
>
> > I think #.(G...) give me what I look for. At load time, though.
> > Sorry.
>
> No, this is at read time which is much earlier than load time, and is more
> likely to be just before compile time & macro expansion time.
(LOAD-TIME-VALUE (G ...)) works to do something at load time.
Understanding #.(...) means thinking about the various times the reader
might conceivably be called. It's in the runtime environment for uncompiled
code. It's in the compile-time environment for compiled code. It's in
someone's randomly unprepared environment for code-walking tools that are
just calling read on your file as if it contained only data. So use it
sparingly.
For constants to be folded at compile-time, DEFCONSTANT is often the way
to go, and then use a reference to the named constant inline. I don't think
there's an anonymous way to do this, unfortunately.