{"path": "/posts/arrows-beautiful-pipelines-haskell/", "title": "Arrows - Beautiful pipelines (Haskell - Typescript) · Bruno Dias", "html": "<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <meta name=\"description\" content=\"I&#x27;m always talking about arrows in Haskell and how it changed the way I think about pipelines. So, let&#x27;s do something with it!\">\n  <title>Arrows - Beautiful pipelines (Haskell - Typescript) · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/arrows-beautiful-pipelines-haskell/\" data-post-metadata>\n  <meta name=\"author\" content=\"Bruno Dias\" data-post-metadata>\n  <meta property=\"og:type\" content=\"article\" data-post-metadata>\n  <meta property=\"og:title\" content=\"Arrows - Beautiful pipelines (Haskell - Typescript)\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/arrows-beautiful-pipelines-haskell/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2025-10-29T03:21:44+00:00\" data-post-metadata>\n  <meta property=\"og:image\" content=\"https://diasbruno.github.io/assets/site/substack/arrows-beautiful-pipelines-haskell/figure-1.png\" data-post-metadata>\n  <link rel=\"icon\" href=\"/assets/site/signature.svg\" type=\"image/svg+xml\">\n  <link rel=\"alternate\" type=\"application/atom+xml\" title=\"diasbruno\" href=\"/atom.xml\">\n  <link rel=\"alternate\" type=\"application/json\" href=\"/posts/arrows-beautiful-pipelines-haskell/content.json\">\n  <link rel=\"stylesheet\" href=\"/assets/site/tokens.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/site.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/footer.css\">\n\n</head>\n<body class=\"light\">\n<div id=\"site-page\">\n<a class=\"skip-link\" href=\"#content\">Skip to content</a>\n<header class=\"site-header\">\n  <a class=\"brand\" href=\"/\" data-site-navigation aria-label=\"Bruno Dias, home\">\n    <img src=\"/assets/site/signature.svg\" width=\"24\" height=\"34\" alt=\"\">\n  </a>\n  <nav aria-label=\"Main navigation\">\n    <ul>\n<li><a class=\"nav-vrn\" href=\"/vrn/\" data-site-navigation><span class=\"nav-label\">vrn</span> <span class=\"nav-badge\">research</span></a></li>\n<li><a class=\"nav-posts\" href=\"/posts/\" data-site-navigation aria-current=\"location\">Posts</a></li>\n<li><a class=\"nav-projects\" href=\"/projects/\" data-site-navigation>Projects</a></li>\n<li><a class=\"nav-about\" href=\"/about/\" data-site-navigation>About</a></li>\n</ul>\n  </nav>\n</header>\n\n<main class=\"section\" id=\"content\" tabindex=\"-1\">\n  <article class=\"post-detail\">\n    <header class=\"section-header\">\n      <p class=\"post-category\">Post</p>\n      <h1>Arrows - Beautiful pipelines (Haskell - Typescript)</h1>\n<p class=\"post-subtitle\">I&#x27;m always talking about arrows in Haskell and how it changed the way I think about pipelines. So, let&#x27;s do something with it!</p>\n      <p><time datetime=\"2025-10-29T03:21:44+00:00\">2025-10-29</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<figure><img src=\"/assets/site/substack/arrows-beautiful-pipelines-haskell/figure-1.png\" alt=\"\" loading=\"lazy\" width=\"1456\" height=\"487\">\n</figure>\n<p>The main idea is how we can generalize the composition of computations. </p>\n<blockquote><p>NOTE: This post will only deal with simple functions. On a next one, we can do other more elaborated stuff.</p>\n</blockquote>\n<p></p>\n<blockquote><p>NOTE 2: All the examples could be simplified, they are just to mere examples.</p>\n</blockquote>\n<p>In theory, it says that an <code>Arrow</code> adds an “interface” for composition. We need a class that can hold a Function and promotes it into an instance of arrow. </p>\n<p>The template variables &lt;I, O&gt; mean input and output.</p>\n<pre><code>class Arrow&lt;I, O&gt; {\n  constructor(public readonly f: (value: I) =&gt; O) {}\n}</code></pre>\n<p>To simplify, let’s make function to help us called `arr`. It just lift a function into an instance of the Arrow class.</p>\n<pre><code>function arr&lt;I, O&gt;(f: (value: I) =&gt; O) {\n  return new Arrow(f);\n}</code></pre>\n<p>In the Haskell package, Control.Arrow, the composition is defined as `&gt;&gt;&gt;`. In our case, we are going to use a method of the Arrow class called `next`.</p>\n<blockquote><p>NOTE: Pay attention that the arrow `a` has its input the output of our current arrow and returns “D”.</p>\n</blockquote>\n<pre><code>public next&lt;D&gt;(a: Arrow&lt;O, D&gt;): Arrow&lt;I, D&gt; {\n  const self = this;\n  return new Arrow((value: I) =&gt; {\n    return a.run(self.run(value));\n  });\n}</code></pre>\n<p>The last thing we need is to create a method to actually run our pipeline.</p>\n<pre><code>class Arrow&lt;I, O&gt; {\n    constructor(public readonly f: (value: I) =&gt; O) {}\n\n    public run(value: I): O {\n        return this.f(value);\n    }\n\n    public next&lt;D&gt;(a: Arrow&lt;O, D&gt;): Arrow&lt;I, D&gt; {\n        const self = this;\n        return new Arrow((value: I) =&gt; {\n            return a.run(self.run(value));\n        });\n    }\n}</code></pre>\n<p>This gives us the ability to compose some simple functions.</p>\n<pre><code>const pipeline = arr(\n    function(x: number) { return x + 1 }\n).next(\n    arr(\n        function (y: number): string { return y.toString(); }\n    )\n);\n\nconsole.log(pipeline.run(1));</code></pre>\n<p>Another useful composition is to send an argument to 2 “parallel” functions. It takes 2 arrows and returns an arrow that returns a pair of the results using one argument for both functions.</p>\n<pre><code>function fork&lt;I, O, OO&gt;(g: Arrow&lt;I, O&gt;, f: Arrow&lt;I, OO&gt;): Arrow&lt;I, [O, OO]&gt; {\n    return arr((value: I) =&gt; {\n        return [g.run(value), f.run(value)];\n    });\n}</code></pre>\n<p>Now, things are starting to get interesting.</p>\n<pre><code>const pipeline = fork(\n    arr(function(x: number) { return x + 1 }),\n    arr(function(y: number): string { return y.toString(); })\n);\n\nconst [v0, v1] = pipeline.run(1);\nconsole.log(v0 == 2 &amp;&amp; v1 == “1”);</code></pre>\n<p>Another example just to illustrate how the composition works.</p>\n<pre><code>const pipeline = arr(\n    function(): number { return 1; } // a constant arrow\n).next(fork(\n    arr(function(x: number) { return x + 1 }),\n    arr(function(y: number): string { return y.toString(); })\n));\n\nconst [v0, v1] = pipeline.run(undefined);\nconsole.log(v0 == 2 &amp;&amp; v1 == “1”);</code></pre>\n<p>As we are working of pairs of values, we can also define 2 more interesting functions: one that acts on the `first` element, and another for the `second` one.</p>\n<pre><code>function first&lt;I, II, O&gt;(g: Arrow&lt;I, O&gt;): Arrow&lt;[I, II], [O, II]&gt; {\n    return arr(([v0, v1]: [I, II]) =&gt; [g.run(v0), v1]);\n}\n\nfunction second&lt;I, II, O&gt;(g: Arrow&lt;II, O&gt;): Arrow&lt;[I, II], [I, O]&gt; {\n    return arr(([v0, v1]: [I, II]) =&gt; [v0, g.run(v1)]);\n}</code></pre>\n<p>Here is what we can do.</p>\n<pre><code>const pipeline = arr(\n    function(x: number) { return x + 1}\n).next(\n    fork(\n        arr(function (x: number) { return x + 1; }), \n        arr(function (x: number) { return x.toString(); })\n    )\n).next(\n    first(arr(function (value: number) { return value * 2; }))\n).next(\n    second(arr(function(txt: string) { return `bla bla bla ${txt}`; }))\n).next(\n    arr(function([v0, v1]) { return `[${v0}] ${v1}`; })\n);\n\nconsole.log(pipeline.run(1) == “[6] bla bla bla 2”);</code></pre>\n<p>There are many ways to compose and create other variants and compositions. With this building blocks, you can go ahead and implement what you need.</p>\n<p>Check out the demo in this <a href=\"https://www.typescriptlang.org/play/#code/MYGwhgzhAECCBO8D2B3APASQDTQPID5oBvAKGnOmCQDsIAXeAV2DqXgAoAHRgIxAEtg0eAFMwAExogAntABmALmjsAbmBCMRSjAEpoAXkK49RAL4kyFbn0HDG1Ves3adS3MUsUKouo3jVoOgALfggAOjlHDREdAG5PaHME6wEhahEADzo0ABF8djAlBGR0XBw81zhEVExywlIvLypaOmgIERA5A0CQiHjG7xFff2h0lCqS9ijnaF0DeoSB8h8-ALAwpgd2zo37aZi4xYpTQ69zJLl7Fn4aaDBEWrx8xWU1aJd5vBMElZGxidQ7DkhwuVzoNwCcn48Hojww2Ce7AA5kVqugEQRKsUagBtBHwgC6OBxZVmGAJCy8vzWiCmOJUAAYcCoAIwEpR4nCEvSGaA4pG7ByMnTMtkgiyXajXW7tZriOEY-IogHoxVYtFoTlkol8jEUjxUoarO609j0pnQVns3Vc8k8wjmnACzaqFk6AnikgAei9ygAZAGdCRJdLIWwANaPUm4AjI1ElKP4HAvbHonAx-DqhMIknp3D6hqDYY0jj7D68wsDal8517N6aEXyQX7d39Y6ekBDaCcficDr8dLdaiMEAgWLQCw9vsCQf6E0cBIh8E0dgZJTDgC2PBE8BMwiNIwy0AA1NAWYkSDowuksuwEvcF0t5GCIcppOvGFud5V6PAB0jiH3YtoGkMJWAAZQYf92DiC8vCDIMSGaCAkE7MIQCQJEuF7ft0mbN0DDnAAiAAmIjDhIKdcJEbo5AjO8vAfIEXxXNdRk-bdd0A6sj1Pc8Tiwe9aSXV92Hfdiv13JRf3-biDwCUCIKg6gsNgk5LwsTtWkdS02W6KiZxEfDDmQ1CjIwrDGUI6ASOgANdOsoiWXI+JKJwwzuiYxcWIcSpN04uTgJZcdzCvG86CBeihI4ETWI-SS9x4k8z0SEVouYqVlwCMT4s4n9lIAoggONRSkEgv8VJgkKEIonSrX09yByMl17HEEQoXScQTJoFC0Is1QGWs2z7NZRznIogyms84SfNXXLv0C41eLPE5r0yCLFyip8mNi7K2P8haiqSvjqpwI5yB2nzlH2jjDuKw8wLKgqqtShIELW29F2hegChmzLRPrLQJLyxaRkB6AACobOq97woYihZRocRfpi2a6CyaSCsS+ToAAAz4MBoAJonwGgAASIh0boUxcZhy8Po2xi-tDM1GVFD1QYCXGcQpxlTAJcmiFZGnqsvVzTL6zDsOnJrjMcnEADYBeJlXSbIuIgA\">Typescript Playground</a>.</p>\n<p>On this topic, the next post we are going to compose a lot more stuff.</p>\n<p></p>\n<p>References:</p>\n<p><a href=\"https://www.cse.chalmers.se/~rjmh/Papers/arrows.pdf\">Generalizing Monads to Arrows</a></p>\n<p><a href=\"https://www.haskell.org/arrows/index.html\">Arrows: A General Interface to Computation</a></p>\n<p><a href=\"https://en.wikibooks.org/wiki/Haskell/Understanding_arrows\">Haskell/Understanding arrows</a></p>\n\n      </section>\n      <aside class=\"post-links\" aria-labelledby=\"post-links-heading\">\n        <h2 id=\"post-links-heading\">Links</h2>\n        <ul>\n<li><a href=\"/posts/\" data-site-navigation>All posts</a></li>\n<li><a href=\"https://diasbruno.substack.com/p/arrows-beautiful-pipelines-haskell\">Original on Substack</a></li>\n        </ul>\n      </aside>\n    </div>\n  </article>\n</main>\n\n<footer class=\"site-footer\">\n  <div class=\"site-footer-inner\">\n    <a class=\"site-footer-home\" href=\"/\" data-site-navigation>Bruno Dias</a>\n    <ul aria-label=\"Footer links\">\n      <li><a href=\"https://github.com/diasbruno\">GitHub</a></li>\n      <li><a href=\"/atom.xml\">Atom feed</a></li>\n    </ul>\n  </div>\n</footer>\n\n</div>\n<script type=\"module\" src=\"/assets/site/navigation.js\"></script>\n</body>\n</html>\n"}
