On Fri, Sep 18, 2009 at 3:52 PM, Patrik Fredriksson <patri...@gmail.com>wrote:

>
> Hi!
>
> Could someone please help me understand why the following causes a
> java.lang.OutOfMemoryError: Java heap space for large n:s (100000
> works fine, 1000000 does not).
>
> (def integers (iterate inc 1))
>
> (defn limited-reduce [fn coll n]
>  (loop [c coll i n result 0]
>  (if (zero? i)
>    result
>  (recur
>    (rest c)
>    (dec i)
>    (fn result (first c))))))
>
> (limited-reduce + integers 100000)
>
> Many thanks!


With (def integers (iterate inc 1)) you're holding on to the head of the
sequence. Try it with

(defn integers [] (iterate inc 1))

and

(limited-reduce + (integers) 1000000)

and the OOME should not occur.

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