completing

added
1.7

ns
clojure.core

type
function

(completing f) (completing f cf)

Takes a reducing function f of 2 args and returns a fn suitable for
transduce by adding an arity-1 signature that calls cf (default -
identity) on the result argument.

                ;; Fix apparently inconsistent behaviour of - with transduce:
(transduce (map inc) - 0 (range 10))
;; 55
(transduce (map inc) (completing -) 0 (range 10))
;; -55
            
                ;; the reducing fn arity-1 executes the last transformation in transduce.
;; completing defaults to "identity" but you can change it.
;; Use this fact for example with transients and go back to persistent once done.
(require '[clojure.string :refer [lower-case]])
(transduce
 (comp
  (remove nil?)
  (map lower-case))
 (completing #(assoc! %1 %2 (inc (get %1 %2 0))) persistent!)
 (transient {})
 ["hi" "ho" "Hello" "hoi" "Hi" "Ha" "ha" "hello"])
;; {"hi" 2, "ho" 1, "hello" 2, "hoi" 1, "ha" 2}