Monday, February 23, 2009

rest vs drop

Now that fully lazy sequences are in the trunk, (rest a-seq) and (drop 1 a-seq) aren't equivalent anymore:
user=> (def s (map #(do (println %) %) (range 10)))
#'user/s
user=> (def d (drop 1 s))
#'user/d
user=> (def r (rest s))
0
#'user/r

As one can see rest needs to realize the first element of s while drop doesn't. The corollary is that (drop n s) holds on the whole seq (including the nth first elements) while rest computes the first element and then discards it.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.