On Tue, Jun 23, 2009 at 11:59 PM, Daniel Lyons <fus...@storytotell.org>wrote:

>
> But if I try it with your original number (10x bigger than mine) I run out
> of Java heap space. Which didn't happen before either, because the lists
> weren't materialized.
>
> So range is cheating here? How's that?
>


Range, like most specialized seqs, aren't implemented as a (lazy) linked
list. They are simply a reference to the parent collection and an index.
There's no reference to the next element, a new next is built each time you
call 'next.

user=> (let [s (seq (range 10))] (identical? (next s) (next s)))
false
user=> (let [s (seq [1 2 3])] (identical? (next s) (next s)))
false
user=> (let [s (seq {1 2 3 4})] (identical? (next s) (next s)))
false
user=> (let [s (seq "abc")] (identical? (next s) (next s)))
false
user=> (let [s (seq '(1 2 3 4))] (identical? (next s) (next s)))
true
user=> (let [s (seq (map identity (range 10)))] (identical? (next s) (next
s)))
true
user=> (let [s (seq (take 10 (iterate inc 0)))] (identical? (next s) (next
s)))
true

Hence keeping a reference to the head doesn't retain the next seqs in
memory.
(apply + ...) must hold a reference to the head (don't know where) and thus
crashes when the seq isn't a specialized seq.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to