On Tue, Nov 3, 2009 at 11:51 PM, Mark Engelberg
<mark.engelb...@gmail.com> wrote:
>
> Clojure's built-in "range" function (last time I looked) essentially
> produces an uncached sequence.  And that makes a lot of sense.

'range' has since changed and now produces a chunked lazy seq
(master branch post-1.0).

> Producing the next value in a range on-demand is way more efficient
> and practical than caching those values.  I think that Clojure
> programmers should have an easy way to make similarly uncached
> sequences if that's what they really want/need.

This can be done by implementing the ISeq interface, today with
proxy, in the future with newnew/reify/deftype/etc.

  (defn incs [i]
    (proxy [clojure.lang.ISeq] []
      (seq [] this)
      (first [] i)
      (next [] (incs (inc i)))))

  user=> (let [r (range 1e9)] [(first r) (last r)])
  java.lang.OutOfMemoryError: GC overhead limit exceeded (NO_SOURCE_FILE:0)

  user=> (let [r (incs 10)] [(first r) (nth r 1e9)])
  [10 1000000010]

Both those examples retain the head, but since 'incs' isn't
a lazy-seq, the intermediate values can be garbage-collected.
Note the difference between the seq abstraction and the lazy-seq
implementation.

--Chouser

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