Clojure transducers (Clojure / core.async, with Java Stream Gatherers)
Reducing-function transformers: a map, filter or dedupe written once, as a function from one step function to another, and applied by any process that feeds inputs one at a time — a collection, a lazy sequence, an iterator, a core.async channel. Java's Stream Gatherers (JEP 485) are the closest mainstream descendant.
| Field | Value |
|---|---|
| Language | Clojure (JVM; ClojureScript ships the same core.async surface); Java 24+ for Gatherers |
| License | EPL-1.0 (epl-v10.html, core.async LICENSE); OpenJDK GPL-2.0 with Classpath Exception (LICENSE) |
| Repository | clojure/clojure · clojure/core.async · openjdk/jdk |
| Documentation | Transducers reference · announcement (2014-08-06) · Strange Loop 2014 talk · core.async reference · JEP 485 |
| Category | Source- and sink-independent element transformation; push-driven by whichever process applies it |
| Versions surveyed | Clojure 1.13.0-master-SNAPSHOT (pom.xml; newest entry in changes.md is 1.12.7) at 92ce2a8, 2026-11-23 · core.async 1.9.865 (latest release in README.md) at 8e6e382, 2026-06-12 · JDK at c35f741, 2026-11-23 (Gatherer is @since 24) |
| Core abstraction | A transducer (rf) -> rf', where rf has three arities: init (), step (result, input), completion (result) |
NOTE
Line numbers are to 92ce2a8524286f765be29779fdc6f96891453dd9 (clojure), 8e6e382d9eefaa3054bfa3834d4e6dbc413cbe2b (core.async) and c35f74179474524a5fa013f6f425691fea3e0cf6 (jdk). Transducers shipped in Clojure 1.7; halt-when came in 1.9. The core.async.flow sources are not in the core.async tree at this commit; only its guides (doc/flow.md, doc/flow-guide.md) are, so flow is cited from those.
Overview
What it solves
Before 1.7, Clojure had one map per context: the lazy-sequence map, the reducers library's r/map, and core.async's channel-to-channel map</map>/filter</unique/partition. Each re-implemented the same element logic against a different carrier. Hickey's announcement names the itch (blog):
"In working recently on providing algorithmic combinators for core.async, I became more and more convinced of the superiority of reducing function transformers over channel→channel functions for algorithmic transformation. In fact, I think they are a better way to do many things for which we normally create bespoke replicas of map, filter etc."
A transducer factors out what is common: what happens to one input. The carrier — where inputs come from, what accumulates the outputs, whether the loop is eager, lazy or a channel's put path — belongs to the transducible process. Each core sequence function gained an arity one shorter than before that returns the transducer, and the core.async channel functions were deprecated with the message "Use transducer instead" (async.clj L1127–1131).
Design philosophy
The reference defines the abstraction by its independence (reference):
"Transducers are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transformation in terms of an individual element. Because transducers are decoupled from input or output sources, they can be used in many different processes - collections, streams, channels, observables, etc."
The announcement states it as a type (blog): a reducing function is whatever, input -> whatever, a transducer is (whatever, input -> whatever) -> (whatever, input -> whatever), and "they don't care (or know about): the 'job' being done (what the reducing function does) the context of use (what 'whatever' is) the source of inputs (where input comes from)." Composition is ordinary function composition (comp), and stateful transducers keep their state in the closure created when the process applies them, not when they are defined. Hickey's Strange Loop 2014 talk derives the same design from reduce; this survey cites the written sources, which state each rule, and was not checked against a talk transcript.
How it works
The 1-arity of map is the whole pattern (core.clj L2772):
(defn map
([f]
(fn [rf] ; the transducer: takes the downstream rf
(fn
([] (rf)) ; init: delegate
([result] (rf result)) ; completion: delegate
([result input] ; step: call rf 0..n times
(rf result (f input)))))))filter calls rf zero or one time (L2838). A stateful transducer allocates its state inside (fn [rf] …), so each application gets fresh state. dedupe (L8103) holds (volatile! ::none); take (L2906) holds a countdown and returns (ensure-reduced result) after the last item; partition-by (L7564) and partition-all (L7644) hold a java.util.ArrayList and flush it in the completion arity:
([result]
(let [result (if (.isEmpty a)
result
(let [v (vec (.toArray a))]
(.clear a) ;;clear first!
(unreduced (rf result v))))]
(rf result)))The parts that make up the protocol:
| Piece | Definition | Role |
|---|---|---|
reduced / reduced? / unreduced | core.clj L2881–2904; clojure.lang.Reduced is a one-field box (Reduced.java) | Early termination: a step returns a boxed result, the process stops feeding inputs |
volatile! / vswap! / vreset! | L2570–2591; Volatile.java is a volatile Object field | Cheap mutable box for per-application state; "should only be used with thread isolation" |
completing | L7205 | Adds a completion arity (default identity) to a 2-arity reducing function |
transduce | L7217 | Eager: (let [f (xform f) ret (reduce f init coll)] (f ret)) — completion always runs |
into, sequence, eduction | L7234, L2692, L8130–8150 | Build a collection; a lazy seq via TransformerIterator; a re-runnable reducible/iterable recipe |
TransformerIterator | TransformerIterator.java | The pull adaptor: drives the push transducer one source element at a time into a small buffer |
chan with xform | async.clj L116, impl/channels.clj L289 | The transducer wraps the buffer's add!; it runs on every put, under the channel mutex |
pipeline, pipeline-blocking, -async | async.clj L573–660 | N workers between two channels; the transducer is applied to each element independently |
The reference states the process contract as three rules (reference): "If a step function returns a reduced value, the transducible process must not supply any more inputs to the step function. The reduced value must be unwrapped with deref before completion."; "A completing process must call the completion operation on the final accumulated value exactly once."; "A transducing process must encapsulate references to the function returned by invoking a transducer - these may be stateful and unsafe for use across threads." And for stateful transducers: "In the completion step, a transducer with reduction state should flush state prior to calling the nested transformer's completion function, unless it has previously seen a reduced value from the nested step in which case pending state should be discarded."
Evaluation model
- Push, always. A transducer never asks for input; the process calls its step with one input, and the step pushes zero or more outputs into
rf. Every process is the loop that owns the source:reduceoverIReduceInit, a channel's put path, the iterator'sstep(). - Pull is an adaptor around the push.
sequenceandeduction'siterator()useTransformerIterator(source): it applies the transducer to a terminal step that appends to aBuffer(Empty→Single→Many), andhasNext()pulls one source element per loop iteration until the buffer is non-empty. It calls the completion step (xf.invoke(null)) both when the source runs dry and when a step returnedreduced. One pull can therefore produce several buffered outputs (cat, apartition-allflush), which is why the adaptor needs a queue, not a slot. - Lazy or eager is the process's choice.
transduceandintoare eager loops;sequenceis incremental ("These sequences will consume input incrementally as needed and fully realize intermediate operations", reference);eductionre-runs the whole transformation "each time reduce/iterator is called" — a cold recipe. - Cold and hot do not apply to the transducer — it is a value with no subscription. A channel is hot (puts happen whether or not anyone takes); a collection is cold.
- No graph, so no glitches and no glitch-freedom. A transducer has one input and one output. Multi-input
sequence(L2692) zips its collections in lockstep throughMultiIterator; it never combines independent event sources.
Composition
(comp (map inc) (filter even?) (dedupe)) composes right to left as functions, but because each wraps the downstream rf, data flows left to right — the order of a ->> thread. The reference spends a sentence on it: "As a mnemonic, remember that the ordering of transducer functions in comp is the same order as sequence transformations in ->>" (reference).
The surface is the 1.7 list in changes.md — map, mapcat, filter, remove, take, take-while, drop, drop-while, take-nth, replace, partition-by, partition-all, keep, keep-indexed, map-indexed, distinct, interpose — plus the new cat, dedupe, random-sample, and later halt-when (1.9). That is about twenty-one operators, all unary, none timed. There is no scan transducer: reductions (L7618) has only collection arities, an absence the Gatherers API fills (Gatherers.scan).
What can and cannot be a transducer
A transducer is a function invoked by the process, synchronously, once per input. That rules out three operator classes:
| Class | Examples | Why not a transducer |
|---|---|---|
| Per-element, stateless | map, filter, keep, cat | Fits |
| Per-element, stateful | dedupe, take, partition-all, scan | Fits; state is created per application, flushed at completion |
| Time-based | debounce, throttle, sample, timeout | Must emit when no input arrives (the timer fires). A step runs only when the process delivers an input, so the transducer cannot act between inputs. It would need the clock as a second input — a second source — or a process that owns a timer |
| Concurrency / latest-wins | switchMap, flatMapLatest, mapConcurrent | Needs work that outlives one step (the inner computation) and the right to cancel it when the next input arrives. A step must return before the next step is called, and there is no cancel arity. core.async does this in go/thread processes and in pipeline*, not in transducers |
| Multi-input | merge, combineLatest, zip, race | A reducing function has one input slot. merge must interleave two independent producers, so it owns two waits — a process. Lockstep zip exists only as sequence's multi-collection arity |
core.async draws exactly this line. Its element logic is all transducers (chan with an xform), while the operations that are processes stay functions over channels: pipe, split, merge (L1083), mult/tap, pub/sub, mix, the multi-channel map (L1045), take (L1109), and pipeline*. Each of these is a go-loop that owns its channels. Debounce is not in the library at all (no debounce, throttle or sample anywhere in async.clj); it is written by hand with alts! over a timeout channel (worked pipeline).
API structure and naming
- Three roles, three vocabularies. Transformations are transducers (
(map f), composed withcomp). Sinks are reducing functions (conj,+,(completing f cf)). Processes bind a transformation to a source and a sink:transduce,into,sequence,eduction,run!,stream-transduce!(L7078, 1.12, forjava.util.stream),chan,promise-chan,pipeline*, and core.async's owntransduce(L699). The shape is(process xform sink source)throughout:(transduce xf + 0 coll),(into [] xf coll),(chan 1 xf). - Same name, shorter arity. The transducer is not a separate
mapT:(map f)returns it and(map f coll)returns a lazy seq. The reference warns that "this reduced arity is not currying or partial application" (reference) —(map f)does not wait for acoll, it returns a different kind of value. Arity overloading is how a dynamically typed library gets one name for both; it costs nothing at a call site and confuses newcomers ((map f)in a->>thread silently returns a function). - A user-defined operator is a three-arity function of
rf. No interface, no registration: write(fn [rf] (fn ([] (rf)) ([r] (rf r)) ([r x] …))), keep state in avolatile!inside the outerfn, honourreducedfromrf.cat(L8067) shows the one subtle rule: a step that runs a nestedreducemust wraprfwithpreserving-reducedso that areducedfrom downstream escapes the inner loop. - Suffixes carry effects.
!marks mutation or a channel effect (volatile!,vswap!,put!,close!),!!marks the blocking thread variant (>!!,<!!,onto-chan!!), and</>on the deprecated channel ops marked the read or write side. core.async renamedonto-chan/to-chantoonto-chan!/onto-chan!!in 1.2 (async.cljL752–763: "Deprecated - use onto-chan! or onto-chan!!") to make the parking-vs-blocking split visible. - Regrets on record. (1) The whole channel-op family (
map<,map>,filter<,filter>,remove<,mapcat<,unique,partition,partition-by) sits under;;;; deprecated - do not use ;;;;(L1127). (2) The announcement called the recipe functioniteration("(iteration xform data)", blog); it shipped aseduction, anditerationwas reused in 1.11 for an unrelated paging generator (changes.md§3.4, CLJ-2555). (3)pipeline's argument order isn to xf from,pipe's isfrom to; a comment abovepipelinestill says ";;todo - switch pipe arg order to match these (to/from)" (L623). (4)intoonce skipped completion in a way that brokehalt-when(CLJ-2556,changes.md) — the completion-exactly-once rule is easy to get wrong in a process.
Time and scheduling
Transducers have no clock, scheduler or thread. They run on whatever thread drives the process; for a channel, that is "either the writer or reader thread depending on the operation and the state of the buffer" (doc/reference.md), under the channel's mutex. Hence the rule: "Channel transducers must not block, whether by issuing i/o operations or by externally synchronizing, else risk impeding or deadlocking go blocks."
core.async's time is (timeout ms) (L145), a channel that closes after ms. The implementation (impl/timers.clj) keeps one global java.util.concurrent.DelayQueue drained by one daemon thread, and coalesces timeouts at TIMEOUT_RESOLUTION_MS 10: a timeout whose deadline falls less than 10 ms before an existing entry's reuses that entry's channel (.ceilingEntry on a ConcurrentSkipListMap), so it may fire up to 10 ms late. A timeout cannot be cancelled; abandoning it is the cancel. A debounce is a go-loop that keeps the current timeout channel and replaces it on each input. Since the deadline is recomputed from a loop variable on every input, the duration can change at runtime for free — the loop reads it from a channel or an atom.
Threads come from go (a fixed pool; running go on virtual threads, ASYNC-262, was reverted in 1.9.847-alpha3 per the README), thread (:mixed workload), io-thread (virtual threads when available, ASYNC-272, :io), and pipeline workers. core.async.flow adds a :workload per process (:io, :compute, :mixed), with :compute transforms submitted to a separate executor (doc/flow-guide.md).
Concurrency and cancellation
Transducers have no cancellation. reduced is the only way to stop, and it is a downstream decision (the consumer has enough), not an outside cancel. The process honours it and still runs completion: transduce calls (f ret) unconditionally (L7217); TransformerIterator calls xf.invoke(null) after a reduced; a channel whose transducer returns reduced runs abort — committing pending puts as true and closing itself (impl/channels.clj L58–71) — and close! then runs the completion arity (add! buf) once the puts are drained (L255).
core.async has no cancellation either. There is no cancel or interrupt anywhere in async.clj. A go block is reclaimed only when unreachable: "The resources associated with a go block may be reclaimed, and the block never resumed, when the channels with which it interacts are no longer referenced" (go docstring, L485). A thread or io-thread "must terminate ordinarily, and will keep referenced resources alive until they do" (L538). Latest-wins is therefore written by hand — a flag the worker polls, a generation number on the result — and is cancel-and-forget: nothing waits for the superseded worker.
pipeline* is where concurrency meets transducers, and it shows the price (L573–621). Each element gets its own (chan 1 xf ex-handler): the element is put, the channel closed, the results drained. So the transducer is instantiated per element, and the docstring says so: "Because it is parallel, the transducer will be applied independently to each element, not across elements, and may produce zero or more outputs per input". dedupe or take in a pipeline sees one element and forgets it — "stateful transducer functions will likely not be useful" (doc/reference.md). Order is kept by a results channel of per-job promise channels. pipeline-async takes no transducer: it takes af, a function of (v, result-chan) that must close the channel. That is a callback, not a reducing-function transformer — asynchrony does not fit the step shape.
CPU-bound work goes to thread/pipeline (:compute); a transducer that does heavy work on a channel's put path stalls whoever is putting.
Errors and completion
There is no error arity. A transducer throws; the process decides. transduce propagates the exception and skips completion. A channel catches it in add! and calls ex-handler; "any non-nil return value will be placed in the channel" (chan docstring); without a handler the exception surfaces on whichever thread ran the put or take (doc/reference.md). pipeline* passes its ex-handler to each per-element channel, so a failing element yields a substitute value or nothing, and the pipeline continues.
Completion is a value-level event: the completion arity flushes state (partition-all) and finalises the accumulator (completing's cf, e.g. persistent!). A channel's completion runs on close!, so closing a channel flushes it: a (chan 10 (partition-all 3)) closed after four puts delivers [1 2 3] and then [4]. Closing is the only stop signal core.async has, so "input ended" and "producer gave up" are the same event and both flush. halt-when (L8079) shows how much the protocol strains when it must carry a result as well as a stop: it smuggles {::halt v} through the accumulator and unpacks it in completion.
State and sharing
A transducer owns only per-application state, so the multicast and sharing questions belong to the process. core.async's answers are channel shapes: mult + tap (L797) to broadcast (the slowest tap paces all), pub/sub for topic routing, sliding-buffer 1 (L103) as conflation, and promise-chan as a write-once cell. There is no observable value cell; idiomatic Clojure keeps state in an atom with add-watch, outside the transducer world.
One placement detail matters for us: a channel's transducer wraps the buffer's add!, so it runs at put time, before the sliding buffer drops anything. dedupe on a sliding-buffer channel therefore sees every put, while a taker sees a conflated subset. A stage placed after a conflating node sees only the survivors, which makes scan (or a counting take) observe a different sequence depending on take timing.
Lifetime and ownership
Nothing is owned. A transducer is an immutable function; its state lives in the closure the process created and dies with that process (an eduction re-creates it on every reduce). core.async has no scopes: lifetime is channel reachability (for go) or the body returning (for threads), and teardown is close! cascading through pipe/pipeline's close? flags (default true). A forgotten thread leaks until it returns. core.async.flow (alpha, 2025–2026) is the library's own answer: a flow graph owns its processes and channels and gives them a lifecycle (::flow/start, stop, pause, resume) through a transition arity on the step function (doc/flow-guide.md).
Testing
Transducers are the easiest part of the stack to test: (into [] xf [1 1 2 3 3]) is a pure, synchronous call with no clock or thread, and the same xf then runs unchanged on a channel. That is the decoupling's practical payoff. core.async has no virtual time: timeout is wall-clock on a global daemon thread with 10 ms resolution, so a debounce test either sleeps or injects the timeout channel factory by hand. Flow step functions are pure (state, in, msg) -> [state' outs] and are, per the guide, "tested in isolation" (doc/flow.md).
The worked pipeline
The element logic is transducers; debounce, latest-wins and the toggle are one go-loop process over alts!, which is what core.async idiom leaves to the user:
(require '[clojure.core.async :as a] '[clojure.string :as str])
(def query-ch (a/chan (a/sliding-buffer 1) (comp (map str/trim) (dedupe)))) ; xform on puts
(def delay-ch (a/chan (a/sliding-buffer 1)))
(def enabled-ch (a/chan (a/sliding-buffer 1) (dedupe)))
(def result-ch (a/chan (a/sliding-buffer 1))) ; the UI polls this
(defn start-hash! [q gen] ; latest-wins by hand: a flag the worker polls
(let [stop (volatile! false)]
{:stop stop
:ch (a/thread (when-some [h (hash-fileset q #(deref stop))] ; nil = observed stop
[gen q h]))}))
(a/go-loop [{:keys [enabled delay q last-in tmo job gen] :as s}
{:enabled true :delay 300 :gen 0}]
(let [ports (cond-> [enabled-ch delay-ch query-ch]
tmo (conj tmo)
(:ch job) (conj (:ch job)))
[v port] (a/alts! ports :priority true)] ; toggle first on ties
(condp = port
enabled-ch (if v
(recur (assoc s :enabled true))
(do (some-> job :stop (vreset! true)) ; flag the hash, do not wait
(recur (assoc s :enabled false :tmo nil :job nil))))
delay-ch (recur (cond-> (assoc s :delay v) ; re-aim from the same anchor
tmo (assoc :tmo (a/timeout (max 0 (- (+ last-in v) (now-ms)))))))
query-ch (if enabled
(recur (assoc s :q v :last-in (now-ms) :tmo (a/timeout delay)))
(recur s))
tmo (let [g (inc gen)] ; debounce fired
(some-> job :stop (vreset! true)) ; cancel-and-forget
(recur (assoc s :tmo nil :gen g :job (start-hash! q g))))
(:ch job) (do (when (and v (= (first v) gen)) ; drop stale generations
(a/>! result-ch (rest v)))
(recur (assoc s :job nil))))))The UI thread reads (a/poll! result-ch) each frame — non-blocking, returns nil when nothing is new — and a take! callback on the same channel can call wakeIn(0) so an idle UI repaints.
Event trace:
- Keystroke during the debounce.
put!onquery-chruns(map str/trim)then(dedupe)on the putting thread under the channel lock; a duplicate is dropped there and never wakes the loop. Otherwise the sliding buffer holds it,alts!returns it, the loop stampslast-inand replacestmowith a fresh(a/timeout delay). The old timeout channel is abandoned; it closes later into nothing. - Keystroke during the hash. Same as (1): the hash keeps running, because latest-wins is tied to the debounced query. When the new timeout fires, the old job's
stopis set, a newthreadstarts withgen + 1, and the old thread's eventual result arrives on a channel no longer inports— or, if it raced in first, is dropped by the generation check. Nothing joins the old thread; ifhash-filesetnever polls the flag, it runs to the end. - Interval change.
delay-chdeliversv; if a debounce is armed, a new timeout is computed from the originallast-inanchor, so the change lengthens or shortens the pending wait rather than restarting it. The 10 ms timer coalescing is the precision floor. - Toggle off.
:priority trueputsenabled-chfirst, so a toggle beats a simultaneous timeout. The loop dropstmo(the pending debounce is forgotten), flags the job, and clears:jobso its result port leavesports. The hash thread finishes when it next polls; nothing waits for it.
Fit with a per-frame UI: good at the edges — transducers on the input channels are synchronous, allocation-light filters, and poll! is exactly a frame-polled read. Poor in the middle — the process is a hand-written state machine, cancellation is a convention, and there is no join, so "toggle off" cannot promise the CPU is idle.
The D angle: one stage, three processes
The question for the D library is whether map/filter/scan/dedupe can be written once and run on a D range, a fiber pull stream (Step next(ref FiberContext, ref T)) and Channel/Cell events. The transducer answer is yes, provided the stage is a template over its downstream, so the composed pipeline is one concrete struct with static calls: @nogc, nothrow and inlining are inferred through it, where Clojure pays an IFn call and a boxed value per stage. The Gatherer shape passes the downstream per call rather than storing it (state and Downstream are separate arguments to integrate), and that suits D's scope/dip1000 rules better: a stage never holds a pointer into the process that drives it. An uncompiled sketch:
enum Step { value, done, cancelled }
// A stage: state is the struct's fields, created fresh when a process copies it in.
// `put` returns false when downstream wants no more (Clojure's `reduced`).
struct Map(alias f)
{
enum maxOut = 1; // outputs per step: lets a pull adaptor use one slot
bool step(D, T)(ref D down, T x) => down.put(f(x));
void finish(D)(ref D down) {}
}
struct Dedupe(T)
{
enum maxOut = 1;
private bool seen;
private T prev;
bool step(D)(ref D down, T x)
{
if (seen && x == prev) return true;
seen = true;
prev = x;
return down.put(x);
}
void finish(D)(ref D down) {}
}
struct Chunk(T, size_t n) // partition-all: the one that needs finish
{
enum maxOut = 1;
private T[n] buf;
private size_t len;
bool step(D)(ref D down, T x)
{
buf[len++] = x;
if (len < n) return true;
len = 0;
return down.put(buf[]);
}
void finish(D)(ref D down) { if (len) down.put(buf[0 .. len]); } // flush on normal end only
}
// Process 1: a D range, pushed through a sink (an output range with `bool put`).
void transduce(R, X, D)(R r, X xf, ref D sink) // xf by value: fresh state per run
{
for (; !r.empty; r.popFront)
if (!xf.step(sink, r.front)) return; // downstream stopped: no flush
xf.finish(sink); // upstream ended: flush
}
// Process 2: a fiber pull stream. The TransformerIterator problem, with a one-slot buffer.
struct Via(S, X, T, U) // T = upstream element, U = output
{
S src;
X xf;
private struct Slot { U v; bool full; bool put(U x) { v = x; full = true; return true; } }
private Slot slot;
private bool ended;
Step next(ref FiberContext ctx, ref U out)
{
static assert(X.maxOut == 1, "a stage with maxOut > 1 needs a queue here");
while (!slot.full)
{
if (ended) return Step.done;
T x;
final switch (src.next(ctx, x)) // may park the fiber
{
case Step.value: if (!xf.step(slot, x)) ended = true; break;
case Step.done: xf.finish(slot); ended = true; break;
case Step.cancelled: return Step.cancelled; // no flush, no emission
}
}
slot.full = false;
out = slot.v;
return Step.value;
}
}
// Process 3: push events (a Channel drained on a fiber, a Cell change callback, a frame):
// call xf.step(downstream, event) per event and xf.finish(downstream) on close.Five conclusions follow from the comparison:
- Source independence is real for per-element stages, and it is the only part that is.
map,filter,dedupe,scan,take,takeWhileand chunking fit;debounce,switchLatest/mapLatest,merge,combineLatestandracedo not, for the reasons in the table above. They are processes, and in our design they are stream operators that own a fiber or aScope, asgo/pipeline*own threads in core.async. - Push vs pull is reconciled by the adaptor, not the stage.
std.algorithmis pull:mapandfilterwrap the source. A transducer wraps the sink. The pull adaptor (Viaabove,TransformerIteratorin Clojure) costs one buffer whose capacity is the stage's maximum outputs per input, somaxOutis a design-by-introspection hook: one slot formap/filter/dedupe/scan, a queue only forflatMap-like stages. On a stackful fiber the adaptor can park insidesrc.next, so the stage never knows it is asynchronous. - Three endings, not two. Clojure has input ended (flush, then complete) and downstream stopped (
reduced: discard pending state, still complete). OurStepaddscancelled. The mapping: upstreamdone→finish(flush) →done; downstream stop → no flush, release upstream,done;cancelled→ nofinishand no emission. Resource release on cancel is the stage's destructor and theScope's teardown, not a protocol arity. Clojure needs completion afterreducedbecause completion also finalises the accumulator (persistent!); our sinks finalise themselves as theEffectresult, so the stage-levelfinishonly flushes, and a flush after a cancel would emit into a pipeline that no longer exists. Gatherers agree: the finisher runs after a short-circuit (guarded byisRejecting()) and not after an exception. - State is created at application, so a pipeline value is a recipe. Copying the stage struct into the process is Clojure's
(xform rf)call. Building a pipeline twice yields independent state, aseductionre-runs.pipeline*is the counter-example to avoid: re-instantiating per element silently disablesdedupe/scan. - Placement relative to conflation changes meaning. A stage before a
Cellsees every change; after it, a conflated subset.map/filter/dedupeare indifferent;scan, countingtakeand chunking are not, so on aCellthey belong upstream of the write, or must be documented as sampling.
Related designs: Java Stream Gatherers
JEP 485 (Viktor Klang; previewed as JEP 461 in JDK 22 and JEP 473 in JDK 23; "We here propose to finalize the API in JDK 24, without change") adds Stream::gather(Gatherer): "Stream::gather(Gatherer) is to intermediate operations what Stream::collect(Collector) is to terminal operations" (JEP 485). A Gatherer is four functions, matching the transducer arities one for one with one addition:
| Transducer | Gatherer | Note |
|---|---|---|
state allocated in (fn [rf] …) | initializer() → A | Called when the op begins (GathererOp's sink begin, L150) |
step (result, input) | integrator().integrate(A state, T element, Downstream) | Returns boolean: false is reduced |
(rf result x) | downstream.push(r) → boolean | isRejecting() asks without pushing |
completion (result) | finisher() (A, Downstream) -> void | Run in end() (L179); "Once a state object is passed to the combiner or finisher function, it is never passed to the integrator function again" |
— (reducers' combinef) | combiner() | Enables parallel evaluation; defaultCombiner() means sequential only |
The differences are instructive. State and downstream are arguments, not closure captures, and the contract forbids keeping them: "Implementations of Gatherer must not capture, retain, or expose to other threads, the references to the state instance, or the downstream Downstream for longer than the invocation duration of the method which they are passed to" (Gatherer.java). Default values (defaultInitializer, defaultFinisher, Integrator.ofGreedy) are sentinels the runtime compares against to skip calls — introspection by identity, the JVM's version of static if. Short-circuit is "interpreted just as if there were no more elements", so the finisher still runs; windowFixed (Gatherers.java L87) guards its flush with !downstream.isRejecting(), the same discard-pending-state rule as Clojure's. Underneath, Java 8 streams were already push: Sink (begin/accept/end/cancellationRequested) is a transducer chain with the source pulled once through a Spliterator.
Gatherers.mapConcurrent(maxConcurrency, mapper) (L350) is the one built-in that brings concurrency into a stage, and it shows where the stage contract ends. It starts one virtual thread per element, keeps a deque of in-flight FutureTasks, preserves input order, and on short-circuit or failure does cancel-and-join: "First signal cancellation for all tasks in progress" (task.cancel(true)) and "Then wait for all in progress task Threads to exit" (thread.join()). It is bounded, ordered concurrency (core.async's pipeline-blocking), not latest-wins: nothing in the integrate-per-element contract can cancel the previous element's task because a new one arrived without breaking order. Time-based gatherers are absent from Gatherers (fold, mapConcurrent, scan, windowFixed, windowSliding), and the JEP's goals stop at "allow custom intermediate operations to manipulate streams of infinite size".
Naming: Gatherer/Gatherers mirrors Collector/Collectors (interface plus a factory class), families are noun-first (windowFixed, windowSliding), and factories split of (parallel-capable) from ofSequential. The JEP declines to grow Stream itself: "We will not add a new intermediate operation to the Stream class for each of the built-in gatherers defined in the Gatherers class, even though for the sake of uniformity it is tempting to do so. In order to preserve the learnability of the Stream class we will consider adding new intermediate operations to it only after experience suggests that they are broadly useful." The JEP's "Alternatives" section points to a separate design document this survey did not locate. Whether the JEP cites Clojure transducers as an influence is not stated in the JEP text; the resemblance is structural.
Strengths
- One definition of each element operator for every carrier; core.async deleted its own family in favour of them.
- A tiny, explicit protocol (three arities,
reduced, completion exactly once) that a new process can implement from the reference alone. - Per-application state makes a transformation a reusable value;
eductionis a cold recipe for free. - Trivially testable:
(into [] xf input)is pure and synchronous. - Gatherers show the shape survives static typing and adds parallelism (
combiner) and a built-in bounded-concurrency stage with cancel-and-join.
Weaknesses
- Time, concurrency and multiple inputs fall outside the model; the interesting operators of a UI pipeline are all processes, written by hand in core.async.
- No cancellation, in transducers or core.async:
reducedis consumer-side only,close!flushes rather than aborts, threads cannot be stopped. pipeline*re-instantiates the transducer per element, silently disabling stateful stages.- Channel transducers run under the channel lock on an arbitrary thread; a slow stage stalls producers.
- Arity overloading (
(map f)vs(map f coll)) and right-to-leftcompwith left-to-right data flow are recurring confusions;halt-whenshows the accumulator protocol straining to carry a result.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
| Transform the reducing function, not the source | One operator for collections, seqs, iterators, channels | Only per-element logic fits; time/concurrency/multi-input stay as processes |
| Three arities: init, step, completion | Completion lets stateful stages flush and accumulators finalise | Completion "exactly once" is a process obligation that processes get wrong (CLJ-2556) |
Early stop via a reduced box | Lets take stop an infinite source without exceptions | Downstream-only; no outside cancel; every nested loop must convey it (preserving-reduced) |
| State created when the process applies the transducer | The transducer stays an immutable, shareable value | pipeline* applies per element, so stateful stages degrade silently |
Same name, shorter arity ((map f)) | No second vocabulary; seq code converts by deleting the collection | Not currying, but looks like it; a dynamically typed mistake returns a function |
Channel xform wraps the buffer's add! | Transformation happens once per put, before buffering/conflation | Runs under the channel mutex on the putter's or taker's thread; must not block |
Gatherers pass state and Downstream as arguments | No captured references; sentinel defaults let the runtime skip calls | More verbose than closures; the JEP concedes it is less succinct than built-in operations |
mapConcurrent cancels and joins on short-circuit | No orphaned virtual threads after the stream stops | Bounded, ordered concurrency only; latest-wins is not expressible as a gatherer |
Sources
- Clojure source at
92ce2a8:src/clj/clojure/core.clj(map,filter,take,partition-by,partition-all,dedupe,cat,halt-when,completing,transduce,into,sequence,eduction,reductions,volatile!,reduced),TransformerIterator.java,Reduced.java,Volatile.java,changes.md. - core.async source at
8e6e382:async.clj,impl/channels.clj,impl/protocols.clj,impl/timers.clj,doc/reference.md,doc/rationale.md,doc/flow.md,doc/flow-guide.md. - JDK source at
c35f741:Gatherer.java,Gatherers.java,GathererOp.java,Sink.java,Stream.java. - Clojure reference: Transducers; Rich Hickey, Transducers are Coming, 2014-08-06; Rich Hickey, Transducers, Strange Loop 2014 (video; not transcribed for this survey).
- JEP 485: Stream Gatherers (Closed / Delivered, JDK 24).
- Siblings: RxJS (operators as source-wrapping functions), Kotlin Flow (
transformas the universal stage), Rust streams (Streamadaptors,select!), fs2 (Pipeas a stream function), C++ senders/receivers (receiver-wrapping algorithms, the closest structural cousin), concepts, comparison.