On Jun 4, 2009, at 10:58 AM, sross wrote:
wouldn't it be best to define seqable? in terms of seq ?
Your version works and is actually faster than what I posted in most cases. It's also guaranteed always to be in sync with any (current or future) definition of "seq".
I didn't do it the way you suggest because I have adopted a philosophy from Rich of avoiding using exceptions in the normal flow of a program, reserving them instead for exceptional situations. One place I can see this being beneficial is in debugging. With code done this way, you could tell the debugger to stop on any exception and have a decent shot at only stopping for interesting conditions.
I do find it very interesting how fast exceptions are on the JVM: I simplified your version to this: (defn seqable? [x] (try (seq x) true (catch IllegalArgumentException _ false)))I was surprised when I compared the performance of the two versions on my machine:
For x = 3, your version took 2.4 microseconds, while my version took 16 microseconds.
For x = (), your version was in the 40 nanosecond range, mine near 33 nanoseconds.
Cheers, --Steve
smime.p7s
Description: S/MIME cryptographic signature