count is now a transducer
This commit is contained in:
parent
56db5ca016
commit
05a82e2b74
2 changed files with 9 additions and 4 deletions
|
|
@ -4,9 +4,9 @@ More transducers and reducing functions for Clojure!
|
|||
|
||||
[](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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue