> > >> ​Well, you misunderstand it, as far as I know, clojure treat all fn as > object, so in your adder-maker example, clojure compiled two fn as object, > that's adder-maker itself and anonymous function it returns. >
You must be right. As far as I know, the compiler is only called as part of eval, and I don't think eval would be called every time a function wants to produce a resultant function at run time. I guess what I'm trying to understand then, is how the maker function can produce a compiled function object when it doesn't know all the details of the function until runtime (the m parameter in this case). > The compiled add-maker is just instantiate that anonymous with `m`, and > return it. So (adder-maker 1) will produce that instantiated anonymous > function, and since it's a function and it's in the first position of list, > clojure will invoke it with 2, and that anonymous function will do actual > computation. > So maybe what is happening underneath is... When the function maker function is compiled, it creates a java object type corresponding to the anonymous function. This object type includes both an invocation member (taking one argument, n) and storage for m. Then at run time when this function is needed for a particular m (in this case 1), an instance of the java object type is created, with m set to 1. That's what I am guessing anyway. What this does mean though I guess is that the code can't be optimized to take advantage of the specific case of m being 1 (eg low level using an inc instruction on n rather than explicitly adding 1 to it). But maybe this is where JIT optimization comes into play - maybe this optimization can happen here? Thanks for your reply. Cheers, Mark. P.S. I still wonder whether the clojure compiler itself could do some kind of mini-compile when at-runtime producing a function. I guess it would be hard for the compiler to know whether the mini-compile was warranted or not. > > functions are compiled at once when clojure reader meet them, and > invocation is just the matter of instantiate. > > Thanks, > Di Xu > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.