On Oct 7, 5:02 pm, Alan Malloy <a...@malloys.org> wrote: > Can't you write that function yourself? > > (defn realized-length [xs] > (loop [n 0 xs xs] > (if (realized? xs) > (recur (inc n) (rest xs)) > n)))
Thanks, Alan! > > drop returns a new lazy sequence, with no realized elements, I didn't "realize" that! I expected that "drop" would call "rest" recursively, like your "realized-length" does. But apparently, it's lazier than that: user=> (def c (drop 12345678 naturals)) #'user/c user=> (time (first c)) Exception in thread "main" java.lang.OutOfMemoryError: Java heap space (more java.lang.barfing....) > user=> (def naturals (rest (iterate inc 0))) > #'user/naturals You introduce another subtlety here: "realized?" won't work on the result of "iterate" (as Tassilo Horn found), but it will work on "(rest (iterate ...))". And this is because: > Note that this does not work for the "base" case of an iterated > sequence, because that is not a lazy-seq but a cons. Seems a bit weird > to me, but then realized? itself is a bit weird... "realized?" doesn't seem so weird, when it deals with promises, delays and futures. Those objects have explicit API's for their creation and realization, and "realized?" has a natural and predictable role in that API. Lazy sequences are more magic, and less explicit about creation and realization (or so it seems to me). Implementors of functions such as "iterate", "drop", "rest", "range", etc., have some freedom to decide times of creation/realization. Then, when you try to use "realized?" on lazy sequences, those decisions (which you may not have expected) come to the surface. Maybe "realized?" doesn't really belong in the lazy sequence API, but it is fun to play around with it. Thanks for showing me how! regards, George -- 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