liveBook · Manning

Interleave-xform

As explained in Clojure: The Essential Reference

interleave-xform is a custom transducer that expresses interleaving as a single-input transducer transformation.

interleave-xform is a custom single-input transducer that inserts successive values from a second collection between values arriving from the input. It separates interleaving from pair creation, making algorithms such as Egyptian multiplication expressible with transduce. section 7.2.1

Egyptian multiplication

Compose (interleave-xform (iterate #(* % 2) y)) with partition-all, filtering, and mapping, then transduce over (iterate #(quot % 2) x). The resulting egypt-mult function returns 20 for (egypt-mult 4 5). section 7.2.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines the reducing-function contract and implementation state, stopping short of broader performance analysis.
Examples
Demonstrates Egyptian multiplication using interleaved doubling and halving sequences.
What it assumes
Assumes familiarity with transduce, reducing functions, and transducer composition.
What you can do after
Build a custom interleaving transducer and compose it with standard transducer operations.
How it connects
Connects transduce with interleave, partition-all, reduce, and transducers.

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.