clojure 2026-11-12

I would like to learn more about cursors vs transducers. If I understand correctly, cursors are simply iterators, like C#’s LINQ, also composable like transducers. Of course transducers come with the superpower of being completely source and destination agnostic, but other than that I don’t think I am able to enumerate (pun intended) any other drawbacks of cursors. Cursors too avoid the problem of “intermediate trollies” compared to e.g. the vanilla JS filter and map. Any good resources on this?

Wouldn’t a cursor just be a source, in that context? Like an iterable etc? In the JDBC world, cursors generally provide a single pass iteration over a result set, as a sort of lazy seq where you don’t hold onto the head. So, they seem orthogonal to me: you could treat a cursor like a lazy sequence, and therefore as a source for a transducing process. Or am I not understanding your question?

I might be wrong, but I think the iterable (or enumerable in C# terms) is the source, while the iterator (or enumerator) is the cursor that “walks” the source.

In Clojure we compose transducers, in C# you compose enumerators.

(def xf (comp (filter odd?) (map inc) (take 2)))
I have never anyone define an enumerator ahead of time like this, instead of doing it inline, but I think it would look something like this:
Func<IEnumerable<int>, IEnumerable<int>> cursor =
    src => src.Where(x => x % 2 == 1)
              .Select(x => x + 1)
              .Take(2);
They “look the same”, but are completely different things under the hood. My impression was that Rich didn’t like cursors, since he explicitly mentioned that transducers are not cursors.

Well, yes, a cursor is generally a mutable thing that provides “the current item” and can be moved to the next item (or previous item, in some situations). The ResultSet in JDBC is basically a cursor, and next.jdbc makes those reducible so you can use a transducer on them.

Clojure has enumeration-seq and iterator-seq to wrap those sorts of things.

I’m sure the statefulness has something to do with the criticism. I just wish to be able to understand how this statefulness results in “bad things”. Are there any concrete comparisons out there? I imagine a situation where I’m being an transducer evangelist to someone who likes LINQ, and they ask me what the benefits are. What do I tell them?

You can’t safely share mutable data across threads: both threads could change the data. In the cursor case, the “current item” in one thread could be changed by the other thread.

I think transducers are kind of irrelevant here, TBH.

It’s more about cursors vs (immutable) lazy sequences IMO, and more generally the sequence abstraction itself (which most other languages don’t really have in any meaningful way).

to emphasize something Sean mentions, there’s a radical change to how you can read, understand, and extend code when you know values are safe to examine, share, store, and reuse by default

Transducers are more thoroughly decomplected 🙂 The transducing context can fuel a lazy sequence (`sequence`), eagerly populate a collection (`into`), transmit asynchronously over a channel (`onto`), reduce (`transduce`), defer (`eduction`), etc., or presumably you could invent another. How would you do partition as a .Net iterator?