Puzzle with lazy sequences

2013-02-05 Thread N8Dawgrr
If the head is retained on a lazy sequence we have a potential memory leak. I set my JVM memory low, 64mb and ran the following: user (defn test1 [coll] (reduce + coll)) #'user/test1 user (test1 (take 1000 (iterate inc 0))) 499500 user Now if we do: user (defn test2 [coll]

Re: Puzzle with lazy sequences

2013-02-05 Thread Herwig Hochleitner
Clojure has a feature called locals clearing, which sets 'coll to nil before calling reduce in test1, because the compiler can prove it won't be used afterwards. In test2, coll has to be retained, because reduce is called a second time on it. 2013/2/5 N8Dawgrr nathan.r.matth...@gmail.com If

Re: Puzzle with lazy sequences

2013-02-05 Thread Jim foo.bar
Couldn't the compiler infer that the 2 expressions are identical with identical arguments and perform the reduce only once? Basically what the programmer would do in a let statement? Would that be too expensive? Jim On 05/02/13 15:21, Herwig Hochleitner wrote: Clojure has a feature called

Re: Puzzle with lazy sequences

2013-02-05 Thread N8Dawgrr
Hi Thanks for the super fast response, Still a little confused. If coll is set to nil before reduce is called, then what is reduce called with? On Tuesday, February 5, 2013 3:21:14 PM UTC, Herwig Hochleitner wrote: Clojure has a feature called locals clearing, which sets 'coll to nil before

Re: Puzzle with lazy sequences

2013-02-05 Thread Timothy Baldridge
Hi Thanks for the super fast response, Still a little confused. If coll is set to nil before reduce is called, then what is reduce called with? Remember, the JVM is a stack machine (before the JIT is run). So the code produced is something like this: push coll push nil pop-into coll call-fn

Re: Puzzle with lazy sequences

2013-02-05 Thread Herwig Hochleitner
Me: No Well, actually Ambrose of core.typed has proposed an interface to let the compiler know about type information. That seems to make it conceivable to mark functions as pure, hence, implement the optimization you proposed, Jim. Sorry if my first response sounded a tad negative, btw