On Wed, Jan 14, 2009 at 12:11 AM, GS <[email protected]> wrote: > > On Jan 13, 7:17 pm, "Nick Vogel" <[email protected]> wrote: >> seq returns nil when a collection has no items. According to the >> documentation for empty?, empty? is the same as (not (seq coll)) so you >> should use seq for expressing the opposite of empty? > > According to my experiment, the code above works equally well whether > 'seq or 'seq? is used. Intuitively (i.e. without actually knowing :) > seq? is more efficient because it returns a boolean, not a newly- > allocated sequence. > > So, either: > > 1. My experiment was wrong, and seq? is not a valid stand-in > for seq in the above code.
Right on the first try! :-) user=> (seq-chunk 2 [1 2 3 4 5]) ((1 2) (3 4) (5)) user=> (seq?-chunk 2 [1 2 3 4 5]) nil This is because a vector is not itself a seq, though it is seq-able. Thus 'seq?' returns false, which 'seq' returns a sequence as long as the vector is not empty. > 2. My intuition is wrong, and 'seq? is not more efficient than 'seq. Both are usually very fast -- fast enough to not worry about them, and certainly fast enough that you should use the correct one rather than the fast one. :-) However, for the record, depending on the type of object being examined, either may be faster. 100,000 times each, fastest to slowest: (seq '(1 2 3)))) ==> "Elapsed time: 8.086614 msecs" (seq? '(1 2 3)))) ==> "Elapsed time: 11.290486 msecs" (seq? [1 2 3]))) ==> "Elapsed time: 19.127055 msecs" (seq [1 2 3]))) ==> "Elapsed time: 20.471575 msecs" --Chouser --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] 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 -~----------~----~----~----~------~----~------~--~---
