From d16bd380faa3099b1b5f6630a4b3215e06ac6799 Mon Sep 17 00:00:00 2001 From: Christophe Grand Date: Thu, 8 Dec 2016 12:01:49 +0100 Subject: [PATCH] x/count can now be used as a transducing context (x/count xform coll). /cc @iig --- src/net/cgrand/xforms.clj | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/net/cgrand/xforms.clj b/src/net/cgrand/xforms.clj index 542884a..de99f69 100644 --- a/src/net/cgrand/xforms.clj +++ b/src/net/cgrand/xforms.clj @@ -433,12 +433,17 @@ (vswap! vwacc f x)) acc)))))))) -(defn count [rf] - (let [n (java.util.concurrent.atomic.AtomicLong.)] - (fn - ([] (rf)) - ([acc] (rf (unreduced (rf acc (.get n))))) - ([acc _] (.incrementAndGet n) acc)))) +(defn count + "Count the number of items. Either used directly as a transducer or invoked with two args + as a transducing context." + ([rf] + (let [n (java.util.concurrent.atomic.AtomicLong.)] + (fn + ([] (rf)) + ([acc] (rf (unreduced (rf acc (.get n))))) + ([acc _] (.incrementAndGet n) acc)))) + ([xform coll] + (transduce (comp xform count) rf/last coll))) (defn multiplex "Returns a transducer that runs several transducers (sepcified by xforms) in parallel.