On Thu, Jul 7, 2011 at 6:00 PM, David Nolen <[email protected]> wrote:
> On Thu, Jul 7, 2011 at 5:14 PM, Ken Wesson <[email protected]> wrote:
>>
>> (defn array? [x] (and x (contains? (set (.getName (.getClass x))) \[)))
>>
>> (defn seqable? [x]
>>  (or
>>    (coll? x)
>>    (nil? x)
>>    (instance? java.util.Collection x)
>>    (instance? java.util.Map x)
>>    (instance? java.util.Set x)
>>    (string? x)
>>    (array? x)))
>>
>> It does do a bit of a hack to test for arrays and it breaks if seq is
>> extended in the future to work with anything it currently won't
>> accept.
>
> A better extensible way to do this: http://dosync.posterous.com/51626638
> David

Interesting, but there is still going to be a performance issue for
the perhaps-common case of testing a non-seqable for seqability: in
that case, it will do the reflective check for isArray and it won't
use the protocol to cache the result. Making it do so, however, would
cause problems if one had (seqable? some-foo) and later attempted to
extend the protocol to mark Foos as seqable within the same runtime
session.

I'm surprised that (.isArray (.getClass foo)) is that slow, though. It
may be "reflection", but it's not the sort I was given to understand
was slow, namely reflective method invocation. An obvious JVM
implementation would make the first word of every object a pointer to
its concrete Class instance and then the above should be no worse than
(.length (.getSomeString some-bean)): a method call and a field lookup
in an object whose pointer was the result of another method call and
another field lookup in another object. Indeed, both method calls are
nonpolymorphic in the former case, since the getClass method is final
and the entire Class class is final so isArray is also final.

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to