Instead of (let [predicate #(contains? (set unselect) %1)] ...)
I would write (let [predicate (set unselect)] ...) On Tuesday, September 4, 2012 11:10:04 AM UTC+2, Marcus Lindner wrote: > > I wanted to use it to select a random element in a collection (set, > vector or list) where I can define elements which should not be selected. > The function I now use is: > > (defn select-random [collection & unselect] > "(select-random collection & unselect) selects a random element from > the specified 'collection'. > It will ignore any element which is specified in 'unselect'. If none > elements are specified in 'unselect', > then any element from the specified 'collection' may be chosen. " > ( let [predicate #(contains? (set unselect) %1)] > (rand-nth (remove predicate collection)))) > > Eventually this can be optimized for better reading and/or performance. > It might be also a good idea to add some preconditions to it like > {pre: [(coll? collection)] }. > > Am 03.09.2012 13:29, schrieb Tassilo Horn: > > Goldritter <marcus.goldr...@googlemail.com <javascript:>> writes: > > > >> Ah ok. So I need to transform a vector and/or a list into a set first. > > No, not really. All clojure collections implement java.util.Collection, > > so you can always use > > > > (.contains your-coll something) > > > > to check if your-coll contains something. However, keep in mind that > > this does a linear search for lists, sequences or vectors. So if your > > algorithm relies on containment checks, you might be better off using > > sets directly. > > > > Bye, > > Tassilo > > > > -- 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