Re: shortcut for for comprehension
On Apr 20, 2009, at 12:19 PM, Michael Hunger wrote: > > Is it possible to use :while to shortcut a for macro when a certain > number of yiels have happened? > > e.g. (for [x (range 1000) :when (= (rem x) 1) :while (number of > yields <= 10)] > > so i want only the first 10 results. Is it possible? Yes... (let [yields (ref 0)] (for [x (range 1000) :when (when (odd? x) (dosync (ref-set yields (inc @yields))) true) :while (<= @yields 10)] x)) => (1 3 5 7 9 11 13 15 17 19) Is it advisable? No. > > Or should I just use (take 10 ) on the for ? Definitely yes. Aloha, David Sletten --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
Re: shortcut for for comprehension
I am a newbie, but seems that "for comprehension" are lazy lists: user> (take 5 (for [x (range 20) :when (do (printf "*%d* " x) (= (rem x 2) 1))] x)) (*0* *1* *2* *3* 1 *4* *5* 3 *6* *7* 5 *8* *9* 7 9) So, I think that you should use take :) 2009/4/21 Michael Hunger > > Is it possible to use :while to shortcut a for macro when a certain number > of yiels have happened? > > e.g. (for [x (range 1000) :when (= (rem x) 1) :while (number of yields <= > 10)] > > so i want only the first 10 results. > > Or should I just use (take 10 ) on the for ? > > Michael > > > > --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
Re: shortcut for for comprehension
On 21.04.2009, at 00:19, Michael Hunger wrote: > > Is it possible to use :while to shortcut a for macro when a certain > number of yiels have happened? > > e.g. (for [x (range 1000) :when (= (rem x) 1) :while (number of > yields <= 10)] > > so i want only the first 10 results. you could: (def zip (partial map vector)) and then use: user> (for [[i x] (zip (iterate inc 0) (range 10 -10 -1)) :while (< i 5)] x) (10 9 8 7 6) > Or should I just use (take 10 ) on the for ? that's probably more readable and works just as well ;). hth, --Chris --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
Re: shortcut for for comprehension
As far as I know the number of yields is not available for testing, so you have to use take user=> (take 10 (for [x (range 1000) :when (= (rem x 2) 1)] x)) (1 3 5 7 9 11 13 15 17 19) On Apr 21, 8:19 am, Michael Hunger wrote: > Is it possible to use :while to shortcut a for macro when a certain number of > yiels have happened? > > e.g. (for [x (range 1000) :when (= (rem x) 1) :while (number of yields <= 10)] > > so i want only the first 10 results. > > Or should I just use (take 10 ) on the for ? > > Michael --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
shortcut for for comprehension
Is it possible to use :while to shortcut a for macro when a certain number of yiels have happened? e.g. (for [x (range 1000) :when (= (rem x) 1) :while (number of yields <= 10)] so i want only the first 10 results. Or should I just use (take 10 ) on the for ? Michael --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---