On Sep 18, 10:35 pm, Jarkko Oranen <chous...@gmail.com> wrote:
> On Sep 18, 10: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!
>
> > /Patrik
>
> First a nitpick: fn is a bad name, because it shadows a special form.
> Using just 'f' is fine and idiomatic.
Yes, the naming was less than stellar, agree.
>
> your problem is the (def integers ...). You're keeping the entire
> sequence of integers in memory :)
Of course. I feel really stupid now :-)
Thank you for your help.
/Patrik
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---