diff --git a/README.md b/README.md index 2de06c5..8f2db6d 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ More transducers and reducing functions for Clojure! [![Build Status](https://travis-ci.org/cgrand/xforms.png?branch=master)](https://travis-ci.org/cgrand/xforms) -Transducers: `reduce`, `into`, `by-key`, `partition`, `pad`, `for`, `window` and `window-by-time`. +Transducers: `reduce`, `into`, `count`, `by-key`, `partition`, `pad`, `for`, `window` and `window-by-time`. -Reducing functions: `str`, `str!`, `avg`, `count`, `juxt`, `juxt-map` and `first`. +Reducing functions: `str`, `str!`, `avg`, `juxt`, `juxt-map` and `first`. Transducing context: `transjuxt` (for performing several transductions in a single pass). @@ -141,7 +141,7 @@ That's why most of the time the last stage of the sub-transducer will be a `x/re My faithful `(reduce-by kf f init coll)` is now `(into {} (x/by-key kf (x/reduce f init)))`. -`(frequencies coll)` is `(into {} (x/by-key identity (x/reduce x/count)) coll)`. +`(frequencies coll)` is `(into {} (x/by-key identity x/count) coll)`. ## On key-value pairs diff --git a/src/net/cgrand/xforms.clj b/src/net/cgrand/xforms.clj index 6c1b60f..f86a982 100644 --- a/src/net/cgrand/xforms.clj +++ b/src/net/cgrand/xforms.clj @@ -365,7 +365,12 @@ (vswap! vwacc f x)) acc)))))))) -(defn count ([] 0) ([n] n) ([n _] (inc n))) +(defn count [rf] + (let [n (java.util.concurrent.atomic.AtomicLong.)] + (fn + ([] (rf)) + ([acc] (rf (unreduced (rf acc (.get n))))) + ([acc _] (.incrementAndGet n) acc)))) (defn juxt "Returns a reducing fn which compute all rfns at once and whose final return