On Wed, Feb 19, 2014 at 10:02 AM, Niels van Klaveren < niels.vanklave...@gmail.com> wrote:
> 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)) > > > The definitions of alt-or and alt-and are swapped: (alt-or false true) and (alt-or true false) both return false, (but (and) is true, and both (alt-and) and (alt-or) are false). (alt-and true false) and (alt-and false true) both return true. > > 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) ?* > The result wouldn't return faster if the input is an infinite list. -- Ben Wolfson "Human kind has used its intelligence to vary the flavour of drinks, which may be sweet, aromatic, fermented or spirit-based. ... Family and social life also offer numerous other occasions to consume drinks for pleasure." [Larousse, "Drink" entry] -- 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.