Skip to content

Instantly share code, notes, and snippets.

@NicolasLambert
NicolasLambert / merging-streaming-sources-clojure.md
Last active November 14, 2026 21:36
Merging Two Streaming Sources in Clojure Without Blowing Up Memory

Merging Two Streaming Sources in Clojure Without Blowing Up Memory

Why sequence + mapcat silently turns your streaming pipeline into an eager buffer, and how IReduceInit fixes it


Introduction

In the previous article, we built an elegant transducer pipeline for streaming large files: a with-open-xf transducer managing resource lifecycle inside the chain, mapcat expanding one file into N rows, and transduce consuming everything in O(1) memory. It works perfectly for a single source.

@NicolasLambert
NicolasLambert / elegant-transducer-pipelines.md
Last active November 14, 2026 21:36
Elegant Resource-Safe Transducer Pipelines in Clojure

Elegant Resource-Safe Transducer Pipelines in Clojure

How to stream large files with O(1) memory, composable transducers, and automatic resource cleanup


The Problem

You need to process large files from cloud storage. Each file has a header line followed by tens of thousands of data rows. The rows must be parsed, transformed, and written to a database in batches.

@NicolasLambert
NicolasLambert / test.clj
Last active September 5, 2021 08:30
[Partition every] How to partition a stream on a time basis #clojure #transducer
(deftest partition-every-test
(testing "Should partition based on elapsed time"
(let [item-count 5
result (into []
(comp
(map deref)
(partition-every 100 :millis))
(repeatedly item-count #(future (Thread/sleep 50) 42)))
flatten-result (flatten result)
count-result (map count result)]
@NicolasLambert
NicolasLambert / test.clj
Last active September 5, 2021 08:31
[Sliding Sort] How to sort element between 2 streams #clojure #transducer
(deftest sliding-sort-test
(testing "Should sort all the given element incrementaly"
(let [xf (sliding-sort 10 <)
input [5 6 7 2 4 0 8 3 1 9]
output (into [] xf input)]
(is (= output [0 1 2 3 4 5 6 7 8 9]))))
(testing "Should sort the given element decrementaly in a sliding window of 5"
(let [xf (sliding-sort 5 >)
input [5 6 7 2 4 0 8 3 1 9]
output (into [] xf input)]