Ditto what everyone else said, plus let's get rid of the duplicated call to Math/log by splitting the iterate into an iterate + a map:
(take-while (fn [[_ second]] (< second 10)) (map (fn [x] [x (/ x (Math/log x))]) (iterate #(* % 2) 2))) Stu > > On 02.06.2009, at 17:35, Wilson MacGyver wrote: > >> for example, the first 10 produces. >> >> ([2 2.8853900817779268] [4 2.8853900817779268] [8 2.8853900817779268] >> [16 3.8471867757039027] [32 5.7707801635558535] [64 >> 9.233248261689365] >> [128 15.38874710281561] [256 26.380709319112476] [512 >> 46.16624130844683] [1024 82.07331788168325]) >> >> what if I want to filter so I only get pairs for which the 2nd value >> is < 10. I couldn't figure out how to get >> filter to work for pair values. > > Try this: > > (take 3 > (filter #(< (second %) 10) > (iterate (fn [[a b]] [(* 2 a) (/ a (Math/log a))]) > [2 (/ 2 (Math/log 2))]))) > > If you try to take 10 values, you will create an endless loop because > with the given parameters your sequence actually has less than ten > elements that satisfy the condition! > >> a 2nd question is more of general clojure idiom, in trying to covert >> the following java code from michaelg's java 1 presentation >> >> private int calcSize(){ >> int max = 2; >> while ((max/Math.log(max)) < size && >> max < Integer.MAX_VALUE && max > 0){ >> max *=2; >> } >> return max; >> } >> >> My first reaction was to do it using a sequence. Is this the clojure >> idiomatic way to convert a while loop from other languages? > > Clojure has loops as well: > > (let [size 10] ;made up > (loop [max 2] > (if (or (>= (/ max (Math/log max)) size) > (>= max Integer/MAX_VALUE) > (<= max 0)) > max > (recur (* 2 max))))) > > Konrad. > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---