Alex Rivera | Logout

counting only truthy values in a collection

Asked 2012-09-20T18:06:29.660
8

Possible Duplicate:
how to return only truthy values as the result of a map operation

I have a collection which has falsy and truthy values. I would like to only count the truthy values, is there a way to do that ?

(count (1 2 3 nil nil)) => 5

Edit
Report

1 Answer

2
(defn truthy-count [coll] 
   (reduce + 0 
     (map #(if % 1 0) coll)))

Although I admit I like dAni's solution better.

answered 2012-09-20T19:28:47.950

Your Answer