Of course my code below will inevitably throw ArrayIndexOfBoundExceptions if no document is malformed ! I let you figure out how to place the correct loop stopper.
2010/5/27 Laurent PETIT <laurent.pe...@gmail.com> > 2010/5/27 CuppoJava <patrickli_2...@hotmail.com> > > The purpose is quite straightforward. >> I just have to call process() on every word in the w and d array. >> >> But I don't want to load docs if they have already been loaded. >> And I want to stop when it hits the first malformed document. >> >> Thanks for the help. Is there a nice efficient way of doing it with >> loop and recur? The proposed solutions are quite expensive as written >> as compared to the Java equivalent. >> -Patrick >> >> >> > If you really have to keep this mutable for some reason (may I guess > performance concerns. Premature, or already measured ?) : > > Here is my attempt, with this additional trick: since you want to stay in > the mutable world, embrace it even more, and make the load() method take > care of optimizing the load. If you can't touch the load method, then just > add a slight wrapper around it, say load2. Also, let us make some use of the > load2 return value. Say it will logical true if not malformed, or logical > false if malformed. > > the code then becomes: > > (loop [i 0] > (let [doc (aget d i)] > (when (load2 doc) > (process doc (aget w i)) > (recur (inc i)))) > > and load2 could be written like this: > > (with-local-vars [previous-doc -1] > (defn load2 [doc] > (if (= (var-get previous-doc) doc) > true > (do > (var-set previous-doc doc) > (load doc) > (not (malformed doc))))) > > I left as an exercise to the reader the correct addition of primitive type > hints :) > > Not that I would encourage this style of programming, of course :) > > -- 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