On Wed, Mar 28, 2012 at 8:32 PM, Nathan Sorenson <n...@sfu.ca> wrote:
>
>> I don't think it's possible to ever have apply not evaluate all its
>> arguments? I think this is what Nathan was saying.
>
> Cedric is right that apply, itself, is strict and evaluates its arguments as
> one might expect. But I'm not referring to manual thunking/delaying your
> arguments to mimic laziness in a strict language. What I'm trying to
> understand is that one is able to write a polyvariadic function that
> *doesn't* completely realize the "& rest" parameter (but only in the case
> that the fn is invoked via apply with a lazy seq argument). It's this
> combination of requirements that means apply should *not* construct a tuple
> (as in clojure-py) or cons arguments together into a list (as I'm currently
> doing in clojure-scheme) as these operations will cause the realization of
> the entire applied sequence.

I suggest that "apply" should, if the function has no rest argument,
realize enough of its input(s) to either be ready to call the function
or know there's at least one more argument than the function's highest
arity and throw an exception; if the function has a rest argument, it
should cons up a list of arguments until either it runs out or there's
enough for all the non-rest arguments of the highest arity overload,
then call the function (or throw) if it's exhausted its input or else
call the function with the rest argument bound to the unrealized tail
of the input.

So, if the input is (range), then:

for (defn foo [a b]) it would realize 0, 1, and 2, then throw arity.

for (defn foo [a b & r]) it would realize 0 and 1, then call foo with
(nth (range) 0) (nth (range) 1) (next (next (range))) as a, b, and r.

> My question is what would require this sort of function? Is there a use-case
> for a function that expects to be called via 'apply' to avoid evaluating
> some of its arguments?

I can't think of any offhand.

> Put another way: why does apply need to promise not to realize its seq
> argument?

(apply + some-lazy-seq-too-big-to-fit-in-main-memory)

Not "avoid evaluating some of its arguments" but "avoid holding onto
the head" in that case.

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