I'm fairly new to Clojure and this was a fun little exercise to try. (def color-map (reduce #(assoc % (count %) %2) {} [:red :yellow :blue :green :purple]))
color-map => {4 :purple, 3 :green, 2 :blue, 1 :yellow, 0 :red} (defn random-repeating-map ([coll-map] (random-repeating-map coll-map coll-map)) ([coll-map new-coll-map] (if (seq new-coll-map) (let [ key-vec (into [] (keys new-coll-map)) idx-key (key-vec (rand-int (count key-vec)))] (cons (new-coll-map idx-key) (lazy-seq (random-repeating-map coll-map (dissoc new-coll-map idx-key))))) (random-repeating-map coll-map)))) (take 20 (random-repeating-map color-map)) => (:blue :purple :yellow :red :green :purple :blue :red :yellow :green :red :purple :yellow :green :blue :green :blue :yellow :red :purple) Step 4: Profit On Wednesday, March 26, 2014 1:10:12 PM UTC-7, Gary Trakhman wrote: > > true, he did say 'or something that would be as useful', but lazy-seqs > are more useful :-). > > > On Wed, Mar 26, 2014 at 4:08 PM, Ben Wolfson <wol...@gmail.com<javascript:> > > wrote: > >> "cycle" isn't really the same thing, though, at least if the OP really >> does require (for some reason) something that can be called repeatedly and >> get a different answer each time. >> >> >> On Wed, Mar 26, 2014 at 1:05 PM, Gary Trakhman >> <gary.t...@gmail.com<javascript:> >> > wrote: >> >>> cycle - http://clojuredocs.org/clojure_core/clojure.core/cycle >>> >>> And generally, this class of functionality is called lazy-seqs. >>> >>> >>> On Wed, Mar 26, 2014 at 3:59 PM, Christopher Howard >>> <cmho...@alaska.edu<javascript:> >>> > wrote: >>> >>>> Hi. I'm still fairly new to Clojure. I was wondering: What's the >>>> easiest way to make a generator (or something that would be as useful). >>>> In one application, I need something that will return a different color >>>> each time I call it - following a predefined list of colors, and >>>> starting over again at the beginning when colors are exhausted. >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Clojure" group. >>>> To post to this group, send email to clo...@googlegroups.com<javascript:> >>>> Note that posts from new members are moderated - please be patient with >>>> your first post. >>>> To unsubscribe from this group, send email to >>>> clojure+u...@googlegroups.com <javascript:> >>>> 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+u...@googlegroups.com <javascript:>. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To post to this group, send email to clo...@googlegroups.com<javascript:> >>> Note that posts from new members are moderated - please be patient with >>> your first post. >>> To unsubscribe from this group, send email to >>> clojure+u...@googlegroups.com <javascript:> >>> 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+u...@googlegroups.com <javascript:>. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >> >> -- >> Ben Wolfson >> "Human kind has used its intelligence to vary the flavour of drinks, >> which may be sweet, aromatic, fermented or spirit-based. ... Family and >> social life also offer numerous other occasions to consume drinks for >> pleasure." [Larousse, "Drink" entry] >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clo...@googlegroups.com<javascript:> >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+u...@googlegroups.com <javascript:> >> 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+u...@googlegroups.com <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > > -- 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/d/optout.