Re: Is there any particular reason for using this form?

83 views
Skip to first unread message

David Nolen

unread,
May 16, 2013, 3:34:16 PM5/16/13
to clojure
It is necessary - integer literals default to primitive long and I'm not sure if >= will inline if it doesn't have type information in scope. This bit of noise could probably be removed by improving type inference in the compiler.


On Thu, May 16, 2013 at 3:09 PM, Pavel Prokopenko <pavel.a.p...@gmail.com> wrote:
Hi,

I've been reading Clojure sources in order to get a better understanding of how and why things work and found this implementation of nth function in src/clj/clojure/gvec.clj:

  (nth [this i not-found]
       (let [z (int 0)]
         (if (and (>= i z) (< i (.count this)))
           (.nth this i)
           not-found)))

The question might sound weird, but anyway..  Is there any reason for doing so instead of:

  (nth [this i not-found]
         (if (and (>= i 0) (< i (.count this)))
           (.nth this i)
           not-found))


or is it just a matter of taste? I'm just curious because the first form seems more verbose and doesn't improve readability, though it's not complex either.

Thanks.

--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Pavel Prokopenko

unread,
May 16, 2013, 4:15:10 PM5/16/13
to clo...@googlegroups.com
Thanks, David!

What about (.nth this i) vs (nth this i)? Is that also some optimization trick like direct object's method call vs reflection method call?

David Nolen

unread,
May 16, 2013, 4:30:52 PM5/16/13
to clojure
.nth is a method call, nth is a function call. Another perf thing.

In anycase if you're looking for examples of everyday Clojure it's best to look elsewhere :) fast Clojure tends to look a bit quirky and relies on details of the current state of the compiler.



--

David Nolen

unread,
May 16, 2013, 4:33:17 PM5/16/13
to clojure
On Thu, May 16, 2013 at 4:30 PM, David Nolen <dnolen...@gmail.com> wrote:
.nth is a method call, nth is a function call. Another perf thing.

In anycase if you're looking for examples of everyday Clojure it's best to look elsewhere :) fast Clojure tends to look a bit quirky and relies on details of the current state of the compiler.

And by "fast" I mean code that for some reason or another needs to reach the best performance offered by the host. These sorts of tricks are not necessary to write reasonably efficient everyday code IMO.
Reply all
Reply to author
Forward
0 new messages