I spent a long time debugging some Clojure code yesterday.  The
essence of it looked similar to this:

(defn items []
  (mapcat expensive-function (range 0 4000 100)))

. . . (take 5 (items)) . . .

expensive-function is a function that issues an HTTP GET to retrieve a
vector of data.  Since range's docstring says it returns a lazy
sequence, and since mapcat is defined in terms of map and concat,
which are both supposed to return lazy sequences, I expected (take 5
(items)) to cause only one HTTP GET.  In reality, it causes 32 GETs.
That's kind of costly in time and space, considering I merely wanted
the first 5 of the 100 items returned in the response to the first
GET.

This behavior was baffling to me at first, but after some research I
found section 12.3 of _The Joy of Clojure_, which mentions that ever
since Clojure 1.1 some functions (such as range) which are advertised
as lazy are actually moderately eager, realizing chunks of up to 32
elements at a time.

I retrieved the release notes for Clojure 1.1.  In the section about
chunking, it says, "Please share any use cases where chunked
processing has resulted in behavioral differences that matter to you
on the Clojure Google group."  That's why I posted this.

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