count is now a transducer

This commit is contained in:
Christophe Grand 2016-09-19 14:36:26 +02:00
parent 56db5ca016
commit 05a82e2b74
2 changed files with 9 additions and 4 deletions

View file

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

View file

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