completing adapts a reducing function for use with transduce by supplying or replacing its completion step. It addresses reducing functions whose single-argument behavior is missing, unwanted, or needs custom finalization. section 7.2.3 section 7.2
The returned function supports the arities required by transducer reduction:
- With zero arguments, invokes the original reducing function with zero arguments to obtain an initial accumulator. section 7.2.3
- With one argument, invokes the closing function on the accumulated result; identity is used when no closing function is supplied. section 7.2.3
- With two arguments, forwards the accumulator and item to the original reducing function. section 7.2.3
- The required f argument must support zero-arity and standard two-arity calls; the optional cf must accept at least one argument. section 7.2.3
Replacing an unwanted completion call
A binary reducing function such as - has single-argument behavior that changes the final result when transduce completes. The wrapper makes that completion behavior explicit: (transduce (map inc) - 0 (range 10)) ; 55 (transduce (map inc) (completing -) 0 (range 10)) ; -55 The first expression uses - directly; the second still delegates two-argument reduction to -, but applies identity at completion instead of invoking its single-argument behavior. section 7.2.3
Listing 7.7. → Transducers, Arity Completion, Helper Function
(completing ([f]) ([f cf]))
Use the compact signature to compare the default identity completion with an explicitly supplied closing function: (completing f) or (completing f cf). section 7.2.3
