On Mon, Jun 28, 2010 at 20:31, Savanni D'Gerinel <sava...@alyra.org> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Along those lines, why is the apply necessary?
>
> More clearly, and I encountered this with a different block of code
> earlier today, repeat returns a lazy sequence.  Str does nothing to
> evaluate that sequences, so (str (repeat ...)) returns a lazy sequence.
>
> Why does (apply str (repeat ...)) evaluate the sequence?  The
> documentation for apply does not mention that it will do so.

apply fully realizes the argument sequence it's applying the function
to. Even in cases like this:

(defn foo [& more]
  ...)

(apply foo (range 10000))

This fully realizes the (range 10000) before calling foo, which is
wasteful if foo is written to consume more incrementally.

I've run into this trouble with concat, when I've wanted a lazy
sequence of the concatenation of the sequences produced by a list
comprehension:

(apply concat (for [n (range 1000)]
                  (range 100)))

Concat gets passed a fully realized sequence of 1000 lazy sequences of
100 integers.

There may be some well-considered reasons why apply behaves this way,
but I do wish that functions like concat provided a variant that took
a single sequence of sequences as its argument, rather than a variable
number of sequences.

// Ben

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