On Saturday, June 21, 2014, Di Xu <xudi...@gmail.com> wrote:

> Suppose at my REPL I do...
>>
>> (defn direct-report-oneplustwo [] (println (str "Direct one plus two is "
>> ((fn [n] (+ 1 n)) 2) ".")))
>>
>> ...then I presume that the compiler has compiled my
>> direct-report-oneplustwo function, and that this has included compilation
>> of my anonymous function (fn [n] (+ 1 n)).  So that now if I run...
>>
>> (direct-report-oneplustwo)
>>
>> ...at my REPL it just needs to run this precompiled code to tell me my
>> answer 3 - no additional compilation required.
>>
>> So far so good.
>>
>> But what if instead I do this at my REPL...
>>
>> (defn adder-make [m] (fn [n] (+ m n)))
>>
>> (defn indirect-report-oneplustwo [] (println (str "Indirect one plus two
>> is " ((adder-make 1) 2) ".")))
>>
>> ...Now at this point, both adder-make and indirect-report-oneplustwo
>> should have been compiled.  But unlike our first approach, the anonymous
>> function (fn [n] (+ 1 n)) has not yet been created by (adder-make 1) and so
>> presumably (??) has not yet been compiled??
>>
>> I am presuming that it is only when we actually run the report function...
>>
>> (indirect-report-oneplustwo)
>>
>> ...that (adder-make 1) is actually run, thereby producing the anonymous
>> function (fn [n] (+ 1 n)), which then gets compiled right then-and-there,
>> immediately followed by its execution??
>>
>
> ​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.
>
> 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.
>
> functions are compiled at once when clojure reader meet them, and
> invocation is just the matter of instantiate.
>
> Thanks,
> Di Xu
>

What's the reason for asking? If you aim for making efficient code (that is
running very many times in tight loops), I think most of this will be
inlined by the JIT, as long as it is not confused by side effects and other
things.

As long as the JVM can optimize the procedure, it's of lesser importance
how Clojure solves it in "one-shot" scenarios.

Maybe I somewhat dodge the question, but dynamic just in time compilation
is so mindblowing cool that an hypothesis about the quickest possible way
to solve the problem combined with profiling is what matters most in
practice.

/Linus

-- 
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.

Reply via email to