liveBook · Manning

Completing

As explained in Clojure: The Essential Reference

completing is a Clojure function that adapts a binary reducing function with completion behavior for use as a transducer reducing function.

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

How Clojure: The Essential Reference covers it

How deep it goes
Defines zero-, one-, and two-argument behavior and demonstrates completion flow through a transducer chain.
Examples
Demonstrates completion with -, custom closing behavior, tracing, and a composed map-partition-all-cat transducer.
What it assumes
Assumes familiarity with reducing functions, arities, accumulators, transducers, and transduce.
What you can do after
Apply it via transduce to repair missing, unwanted, or specialized single-argument completion behavior.
How it connects
Connects reducing functions with transducers, transduce, completion functions, and transducer composition.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.