On Nov 16, 12:18 pm, "Craig Andera" <[EMAIL PROTECTED]> wrote:
> One way I could think of to do this would be to build a map with each
> unique item as a key and the count as its value. Then it would be
> trivial to pull out the keys with the count as a specified value. I
> started writing this, but my SLIME is royally screwed up right now
> after the upgrade to Clojure HEAD. Sorry. Anyway, it shouldn't require
> any refs - probably just a loop/recur.
>
> Or maybe someone has a far more clever idea. :)
>
> > On Sun, Nov 16, 2008 at 12:02 PM, npt11tpn <[EMAIL PROTECTED]> wrote:
>
> > Hi guys,
> > This is my first post to the group, but have been following the
> > discussions for almost a year. Many thanks to Rich Hickey for creating
> > a fantastic future-proof language which is a pleasure to use and to
> > the great community!
> > The following function (as part of a chemistry-related application)
> > filters the elements of a sequence that a repeated a specified number
> > of times. Wondering if the function could be simplified and written
> > without the reference (newseq).
> > Thanks a lot!
> > Nik
>
> > (defn filter-repeats [seq n]
> > "Returns a vector of the elements of a sequence that are repeated n
> > times"
> > (let [newseq (ref [])]
> > (doseq [u (set seq)]
> > (when (= (count (filter #(= % u) seq)) n)
> > (dosync (commute newseq conj u))))
> > @newseq))
>
> > Usage e.g.
> > (filter-repeats '(2 3 1 4 2 2 4 3 5 ) 2)
> > [3 4]
(defn filter-repeats [n coll]
(let [counts (reduce (fn [m k] (assoc m k (inc (m k 0))))
{} coll)]
(for [[k v] counts :when (= v n)] k)))
Rich
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---