There is a standard library function for this: separate. For example (separate even? coll) returns two results in a vector: (filter even? coll) and (filter odd? coll).
On Feb 10, 9:05 pm, Manuel Paccagnella <manuel.paccagne...@gmail.com> wrote: > On 02/09/2012 11:40 PM, Steve Miner wrote: > > > filter is lazy so it won't actually do the work unless the values are > > needed. To get a reasonable time, you need to use the result for some > > computation. Try something like this: > > > (defn sum-all [m] (reduce + (apply map + (vals m)))) > > > (time (sum-all (separate-nums (range 10000)))) > > It was pretty simple. I forgot laziness :/ > > I ran several times in a row both functions on the same sequence, and I > obtained these timings (functional first, iterative second): > > "Elapsed time: 3019.56405 msecs" > "Elapsed time: 621.744839 msecs" > > "Elapsed time: 867.197906 msecs" > "Elapsed time: 551.287444 msecs" > > "Elapsed time: 314.490382 msecs" > "Elapsed time: 647.862119 msecs" > > "Elapsed time: 328.403288 msecs" > "Elapsed time: 621.69671 msecs" > > "Elapsed time: 334.29854 msecs" > "Elapsed time: 839.599691 msecs" > > "Elapsed time: 272.061383 msecs" > "Elapsed time: 499.008063 msecs" > > The patterns seems to be this: initially the functional one is slower, > but quickly begins to run about twice as fast as the iterative one. > > Using instead a sequence of random numbers, I got a more stable result: > in average the functional approach is slower than the iterative one. > > I think the lesson here is this: use a functional approach, that way the > code is easier to write, read, compose and reason about. If and when you > need to optimize, one option is to rewrite some core functions in an > iterative style. A plus here is that functional code is easier to profile. -- 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