On 2010 Mar 24, at 2:39 AM, Meikel Brandmeyer wrote:
On Mar 24, 2:22 am, Douglas Philips <d...@mac.com> wrote:
would (let [s1 (first seq1)
s1tail (rest seq1)] ...)
be any better? rest says it calls seq on its argument, and that would
force as well?
Hmmm... So only things like map are able to realize first without
realizing (first (rest seq1)) too?
No. Understand the difference between next and rest.
Since I don't want to depend on implementation details, I'll go by the
docs:
http://richhickey.github.com/clojure/clojure.core-api.html
rest function
Usage: (rest coll)
Returns a possibly empty seq of the items after the first. Calls
seq on its argument.
next function
Usage: (next coll)
Returns a seq of the items after the first. Calls seq on its
argument. If there are no more items, returns nil.
seq function
Usage: (seq coll)
Returns a seq on the collection. If the collection is empty,
returns nil. (seq nil) returns nil. seq also works on Strings,
native Java arrays (of reference types) and any objects that
implement Iterable.
Ok, so rest might return an empty seq, next would return nil, both
call seq and seq says it can return nil if the collection is empty, so
by calling seq, both rest and next force the first element.
Since next can return nil, it must be realizing the second element of
the sequence, where as rest isn't.
Or have I missed something?
Back to the original issue:
(let [[first-s & rest-s] some-seq] ...)
According to the fine print :) (http://clojure.org/special_forms):
The basic sequential form is a vector of binding-forms, which will
be bound to successive elements from the init-expr, looked up via
nth. In addition, and optionally, & followed by a binding-forms
will cause that binding-form to be bound to the remainder of the
sequence, i.e. that part not yet bound, looked up via nthnext .
So by the definition of next quoted above, the let destructuring
should be "one ahead" because it uses next instead of rest...
Thanks!
-Doug
--
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
To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or
reply to this email with the words "REMOVE ME" as the subject.