Yup, five bugs by my count. Not bad for a one-liner. More coffee
necessary.
Same algorithm, but tested.
(defn frequencies [coll]
(reduce (fn [map val] (assoc map val (if (contains? map val) (+ 1
(get map val)) 1))) {} coll)
)
On Dec 11, 9:52 am, Randall R Schulz <[EMAIL PROTECTED]> wrote:
> On Thursday 11 December 2008 06:33, Dave Griffith wrote:
>
> > On Dec 11, 9:21 am, bOR_ <[EMAIL PROTECTED]> wrote:
> > > Hi all,
>
> > > I thought I remembered there was a method in the api somewhere that
> > > would count the frequency of each unique item in a collection, but
> > > I can't find it anymore. What would be a brief way to write that in
> > > clojure?
>
> > > (In ruby: array.inject(Hash.new(0)) {|hash,key| hash[key] += 1 ;
> > > hash})
>
> > (defn frequencies [coll]
> > (reduce (fn [map val] (assoc map val (if (contains map val) (get map val)
> > 1)) #{})
> > )
>
> Shooting from the hip, eh? There is no "contains", it's "contains?".
> There's a missing close parenthesis. I don't see where this is doing any
> counting. How does it compute occurrence frequencies?
>
> Randall Schulz
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---