๐จ Recursive pipe programming
on September 29, 2025
In programming, we often start by writing imperative loops. Simple, effective... but quickly limited. Let's take a concrete example: a for loop that displays the values of i from 1 to 3.
for ($i = 1; $i <= 3; $i++) { echo $i . PHP_EOL; }
๐ The 2D graph representation
We can represent this for in the form of a graph:
- Initialize
i = 1* Test for conditioni <= 3 ?* Execute the loop body (print i) * Incrementi = i + 1* Return to the condition while it is true * Exit when the condition becomes false
Visually, this results in a cycle with multiple nodes and arrows. This type of graph works, but it has a major problem: the larger the program gets, the more illegible the graph becomes. We end up with dozens of nodes, arrows in all directions... and reading becomes a real headache.
๐ The linear version: a functional pipe
Instead of thinking in terms of a 2D graph, we can reduce the loop to a linear transformation. We go from an initial state (i = 1), we apply a function that handles the recursion, and we arrive at the output (Exit). Everything is aligned vertically like a data pipeline.
Schematically:
i = 1 โ Y (recursion) โ Exit
Here, Y Combinator plays the key role: it allows recursion to be expressed in a functional language without needing to explicitly name the function. Recursion becomes a single node in the pipeline, instead of a tangle of arrows in a graph.
๐งฉ Example in PHP with Y Combinator
// Y Combinator Definition $Y = fn($F) => (function ($x) use ($F) { return $F(function (...$args) use ($x, $F) { return $x($x)(...$args); }); })(fn($x) => $F(function (...$args) use ($x, $F) { return $x($x)(...$args); }));
// for loop transformed into a recursive pipe $step = function ($state) { ['i'=>$i,'max'=>$max] = $state; if ($i > $max) return [$state, true]; echo $i . PHP_EOL; return [['i'=>$i+1,'max'=>$max], false]; };
$recurse = $Y(function ($self) { return function ($state, $step) use ($self) { [$next, $done] = $step($state); return $done ? $next : $self($next, $step); }; });
$init = ['i'=>1,'max'=>3]; $recurse($init, $step);
Exit :
1 2 3
๐ฏ Why is this interesting?
- Readability: A linear pipeline is much easier to understand than a 2D graph. * Modularity: Each step is a pure function that can be tested and reused. * Power: Y Combinator allows you to express recursion without relying on imperatives. * Elegance: The structure is clear, from top to bottom, like a data flow.
๐ฎ Conclusion
Node graphs are useful for quick cases, but they quickly become visual spaghetti code. By taking a functional approach with pipes and combinators, we gain in readability, reusability, and expressive power.
๐ Stop drawing your loops as 2D graphs. Express them as functional pipes. It's more concise, clearer, and opens the door to much richer abstractions.
โจ If you want to go further:
๐ I offer free 30-minute coaching sessions to help creators like you automate their processes and save time โฑ๏ธ
๐ Book your free session here: https://www.bonzai.pro/matyo91/lp/4471/je-taide-a-automatiser-tes-process
Thanks for reading! Let's create smart, fast, and automated workflows together ๐ปโก