Currently, *and* and *or *are implemented as macro's, and cannot be used in 
*apply 
*or as function parameters. Related functions like *every?* and *some* are 
implemented either on the basis of *or* or recursion over lazy sequences. 
These choices were made because they all need to be able to short-circuit, 
which could be hard in previous clojure versions.

>From 1.5 we have the *reduced* function though, which enables 
short-circuiting *reduce *functions. Since I couldn't find much examples of 
*reduced*, I started experimenting a bit to find out it's usages. One of 
the things I found was that it was pretty easy to create reduced based 
functions for the above, so they could be used with *apply *and as function 
parameters. I came up with the following:

(defn alt-or
  ([] nil)
  ([& args] (reduce (fn [_ s] (if s s (reduced s))) nil args)))

(defn alt-and
  ([] nil)
  ([& args] (reduce (fn [_ s] (if s (reduced s) s)) nil args)))

(defn alt-every?
  [pred coll]
  (reduce (fn [_ s] (let [t (boolean (pred s))] (if t t (reduced t)))) true 
coll))

(defn alt-some
  [pred coll]
  (reduce (fn [_ s] (let [t (pred s)] (if t (reduced t) t))) nil coll))


Some quick further testing also showed they were pretty quick, with the 
alternate *every?* and *some *being twice as fast as current versions. Of 
course usually these are held back by the complexity of their predicates, 
but every bit counts. Testing the *or *and *and *functions on large 
collections proved a bit harder, since you can't *apply* the default ones, 
and evalling large lists prepended with *or *and *and* blew the permgen 
pretty quickly. *Anyone have an idea how to compare such scenario's?* With 
repeating a lot of small evaluations both were on par.

Another idea was that this way I could also make core.reducers based 
versions. Of course this would mean that sometimes you would evaluate too 
much tests, but since this would happen in parallel, in general the result 
would return faster. 
*Am I right in presuming that, or am I overlooking some very basic argument 
against them (I'm pretty good at 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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to