On Wed, Feb 27, 2013 at 11:20 AM, Marko Topolnik <marko.topol...@gmail.com>wrote:
> On Wednesday, February 27, 2013 10:59:33 AM UTC+1, Christophe Grand wrote: > >> >> Now that reduce can be short-circuited, redifining every?, some and al on >> top of it would yield some interesting gains: >> >> (defn revery? [pred coll] >> (reduce (fn [t x] >> (if (pred x) >> t >> (reduced false))) true coll)) >> >> (defn rblank? [s] (revery? #(Character/isWhitespace ^char %) s)) >> (defn blank? [s] (every? #(Character/isWhitespace ^char %) s)) >> >> >> Christophe >> > > These are very interesting results because they show that the current, > supposedly "optimized" implementation involving *recur* is apparently 5x > slower than plain *reduce* (the short-circuiting aspect doesn't play a > role in this example). > Indeed not in this example (all chars are blank and the string is short): but you need short-circuiting to replace seq-based every? by reduce-based every?. reduce is special-cased for Strings (well for StringSeq to be precise). I wouldn't call optimized the seq-idiomatic version which allocates one Cons at each iteration step. Using reduce allows (potentially) to avoid the seq allocations (and reducers allow that on a whole transformation pipeline, without having to (manually) merge everything in the reducing fn). Having a short-circuiting reduce allows for reduce-idiomatic versions of functions that would have been too eager otherwise. -- On Clojure http://clj-me.cgrand.net/ Clojure Programming http://clojurebook.com Training, Consulting & Contracting http://lambdanext.eu/ -- -- 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.