On Wed, Mar 28, 2012 at 2:13 PM, Timothy Baldridge <tbaldri...@gmail.com> wrote:
>>>is there a use case behind apply being lazy when Clojure is otherwise a 
>>>strictly evaluating language
>
> In clojure-py we have to pass vararg arguments as tuples. So it ends
> up a lot like
>
> (to-tuple (concat args seqarg))
>
> I always saw the seq argument in IFn as a crutch to get around the "we
> have too many vars to pass to this function" problem. Basically, since
> the JVM doesn't support lazy varargs, Clojure doesn't either.

Apply isn't really non-strict. If your source contains

(apply foo x y z w)

then apply, foo, x, y, z, and w will all be evaluated, in
left-to-right order, before foo is called with x, y, z, and the
contents of w as arguments. If w is a literal such as [w1 w2 w3 w4]
then w1, w2, w3, and w4 will get evaluated, after z and in
left-to-right order, before foo is called.

In other words, any side effects in a line like

(apply foo x y z [w1 w2 w3 w4])

happen in the order one would expect. And if w is really a lazy
sequence, and a lambda involved in generating it has side effects, the
fact that the side effects are in a lambda (or not even in the
(apply...) line) tells you that the side effects may be delayed.

Further, it's possible to get a kind of non-strict-analogous behavior
(side effects may be delayed, reordered, or not happen at all) in
Clojure if foo and its caller are written specially. For example,

(defn weird-and [x y]
  (if @x @y))

(weird-and (delay (long-running-test-1)) (delay (long-running-test-2)))

will only compute the second expensive test if the first didn't return
logical false. (The built-in and macro also short-circuits, but does
so by being a macro. Unlike and, weird-and could be passed to a HOF.
But the HOF would have to know to pass it delays rather than normal
booleans. You could do (reduce weird-and [(delay this) (delay that)
...]).)

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