Ah, I didn't know about the reduce function. I'll give that a try and
thanks a lot.

On Feb 10, 10:42 pm, Brenton <bashw...@gmail.com> wrote:
> chaosprophet,
>
> Clojure wants you to think in terms of sequences instead to loops.
> Instead to looping through cat-all and keeping track of the sum, you
> want to use map and reduce.
>
> (reduce + (map #(probability-of-category-given-document % tokens) cat-
> all))
>
> Brenton
>
> On Feb 10, 7:14 am, chaosprophet <bg.x...@gmail.com> wrote:
>
> > Hi guys,
> > I'm new to both clojure and functional programming and as an exercise
> > in learning Clojure, I decided to write a naive bayes categorizer. I
> > have a piece of code wherein I have a doseq inside which i am calling
> > a function which returns a value. What I would like to do is have the
> > value returned by the function to be added to a particular binding
> > (variable), so that at the end of the doseq, I'll have a total sum of
> > all values returned. Right now im doing this by using def to rebind
> > the value. The exact code I'm using is:
> > (def prob-sum 0)
> > (doseq [cat cat-all]
> >                 (def prob-sum (+ prob-sum 
> > (probability-of-category-given-document
> > cat tokens))))
>
> > I know that using def to rebind is not good practice and I should
> > probably be using ref. However, this executes only in a single thread,
> > so I'm not sure if it would be appropriate to use ref.
> > Also, I realize that I'm abusing the concept of data immutability, but
> > in this case I really need this to be mutable. However, I have a gut
> > feeling that I'm not really approaching this in the right way (as in
> > correct way to do it in a functional language), and I would really
> > appreciate it if someone could set me upon the right track.
>
> > Regards,
> > chaosprophet

-- 
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

Reply via email to