Thanks, this explains the stack overflow

On Thursday, June 14, 2012 4:12:28 PM UTC+3, Dave Sann wrote:
>
> also
>
>  (first (drop 100000 (iterate #(doall (map inc %)) (range 10))))
>
> so the better answer is probably - because map is lazy
>
>
> On Thursday, 14 June 2012 23:06:27 UTC+10, Dave Sann wrote:
>>
>> I suspect that the answer is the use of partial and the implementation of 
>> iterate
>>
>> with (comp vec... you force realisation of the vector
>>
>> without this I think you get (partial map (partial map (partial ....) as 
>> f is repeatedly applied
>>
>> if you do this,
>>
>> (first (drop 100000 (iterate #(apply list (map inc %)) (range 10))))
>>
>> which forces the list to be realised, it wont overflow
>>
>> D
>>
>>
>> On Thursday, 14 June 2012 23:00:15 UTC+10, Dave Sann wrote:
>>>
>>> ah...but does for 10000
>>>
>>>
>>> On Thursday, 14 June 2012 22:58:58 UTC+10, Dave Sann wrote:
>>>>
>>>> It doesn't overflow for me.
>>>>
>>>>
>>>> user=> (first (drop 1000 (iterate (partial map inc) (range 10))))
>>>> (1000 1001 1002 1003 1004 1005 1006 1007 1008 1009)
>>>>
>>>>
>>>> On Thursday, 14 June 2012 22:52:33 UTC+10, vmargioulas wrote:
>>>>>
>>>>> Can someone explain why 
>>>>> ... iterating over a sequence cause a stack overflow 
>>>>> (first (drop 1000 (iterate (partial map inc) (range 10)))) -> 
>>>>> java.lang.StackOverflowError 
>>>>>
>>>>> ...but iterating over a vector works ok? 
>>>>> (first (drop 1000 (iterate (comp vec (partial map inc)) (range 10)))) 
>>>>> - 
>>>>> > [1000 1001 1002 1003 1004 1005 1006 1007 1008 1009] 
>>>>>
>>>>

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