{"path": "/posts/processes-and-pipes-in-oop/", "title": "Processes and pipes in OOP · 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=\"Processes and pipes in OOP\">\n  <title>Processes and pipes in OOP · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/processes-and-pipes-in-oop/\" 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=\"Processes and pipes in OOP\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/processes-and-pipes-in-oop/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2022-03-30T15:00:00Z\" 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/processes-and-pipes-in-oop/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>Processes and pipes in OOP</h1>\n\n      <p><time datetime=\"2022-03-30T15:00:00Z\">2022-03-30</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<p>This article is partially based on John Hughes'\n(Arrows)[https://www.haskell.org/arrows/index.html]\nand the idea of unix processes on the OOP land.</p>\n<p>The idea of Arrows is a way to compose functions\nin such a way that it look like a circuit. This is\ngreat, because we can encode a pipeline process\njust like we would write a diagram.</p>\n<p>(Add arrow's diagram picture)</p>\n<p>Also, we know how to compose processes on the terminal,\nand they are pretty simple.</p>\n<p>Each process has 4 components:</p>\n<ul>\n<li>arguments</li>\n<li>stdin</li>\n<li>stdout</li>\n<li>stderr</li>\n</ul>\n<p>We can imagine a pipeline that may looks like this:</p>\n<pre><code class=\"language-sh\">echo &quot;1&quot; | add --value 4 - | sub --value 1\n// 4\n</code></pre>\n<p>Looking at <code>add</code>, it has an argument <code>-s 2</code>,\nit use <code>stdin</code> with the <code>-</code>syntax, might use <code>stderr</code>\nand the result is piped to <code>sub</code> through <code>stdout</code>,\nand received as <code>stdin</code> on sub.</p>\n<p>Let's start with a simple function composition...</p>\n<p>(we are going to use <code>compose = flip (.)</code> for the entire article)</p>\n<pre><code class=\"language-rb\">def compose(*fs)\n  -&gt;(arg) { fs.reduce(arg) do |acc, f| f.call(acc); end }\nend\n\ncompose(-&gt;(x) { x + 1 }, -&gt;(y) { y + 2 }).call(2) == 5\n</code></pre>\n<p>This will give use the simpliest composition we know...</p>\n<p>Some processes might need some configuration so it can\nuse later on when we run the computation. We can\nuse a closure for this.</p>\n<pre><code class=\"language-rb\">adder = -&gt;(y) { -&gt;(x) { x + y } }\n\ncompose(adder.call(2), -&gt;(y) { y + 2 }).call(2) == 6\n</code></pre>\n<p>Now that we know how to configure our function,\nnext step is improve it using a <code>class</code>.</p>\n<pre><code class=\"language-rb\">class Adder\n  def self.make(x); self.new(2).method(:calc); end\n\n  attr_accessor :value\n\n  def initialize(i); @value = i; end\n\n  def calc(x); @value + x; end\nend\n\ncompose(Adder.make(2), Adder.make(1)).call(2).value == 5\n</code></pre>\n<p>Given a bounded methods to the compose function\nit still works!!</p>\n<p>The return type can be anything on this simple\npipeline, but it doesn't help when we need to deal with\nerror and exceptions.</p>\n<p>We are going to add 3 classes to simulate the unix pipeline:</p>\n<ul>\n<li><code>Result</code> (stdin - stdout)</li>\n</ul>\n<p>Can be used to initialize the pipeline\nand hold the result of the computation.</p>\n<ul>\n<li><code>Failure</code> (stdin - stdout)</li>\n</ul>\n<p>Indicates that we failed, but no exception was thrown.</p>\n<ul>\n<li><code>Except</code> (stderr)</li>\n</ul>\n<p>We failed miserable...</p>\n<pre><code class=\"language-rb\">class BaseValue\n  attr_accessor :value\n  def initialize(value); @value = value; end\nend\n\nclass Result &lt; BaseValue; end\nclass Failure &lt; BaseValue; end\nclass Except &lt; BaseValue; end\n\nclass Adder\n  def self.make(x); self.new(2).method(:calc); end\n  def initialize(i); @i = i; end\n\n  def calc(x)\n\tResult.new(@i + x.value)\n  end\nend\n\nclass Double\n  def self.make; self.new.method(:calc); end\n  def initialize; end\n\n  def calc(x)\n\tResult.new(2 * x.value)\n  end\nend\n\ncompose(Adder.make(2), Double.make, Result.new(2)).value == 8\n</code></pre>\n<p>This will help us to propagate the value across\nthe entire pipeline.</p>\n<p>We can use than to run the continuation\nwhen we match the correct result type.</p>\n<pre><code class=\"language-rb\">class Result &lt; BaseValue\n  def on_success(f); f.call(value); end\n  def on_failure(f); self; end\n  def on_exception(f); self; end\nend\n\nclass Failure &lt; BaseValue\n  def on_success(f); self; end\n  def on_failure(f); f.call(value); end\n  def on_exception(f); self; end\nend\n\nclass Except &lt; BaseValue\n  def on_success(f); self; end\n  def on_failure(f); self; end\n  def on_exception(f); f.call(value); end\nend\n\nResult.new(2).on_success(-&gt; (x) { Result.new(x + 1) }).value == 3\nFailure.new(2).on_success(-&gt; {}).value == 2\n</code></pre>\n<p>The only thing missing is the pipe.</p>\n<p>We can write a class that can glue everything togheter.</p>\n<pre><code class=\"language-rb\">class Pipe\n  attr_accessor :work\n  def initialize(initial_work); @work = initial_work; end\n\n  def id(*x); x; end\n\n  def pipe(work, on_failure = nil, on_exception = nil)\n\tpw = @work\n\thandle_failure = (on_failure or self.id)\n\thandle_exception = (on_exception or self.id)\n\n\t@work = -&gt;(*input) {\n\t  pw.call(\n\t\t*input\n\t  ).on_success(\n\t\twork\n\t  ).on_failure(\n\t\thandle_failure\n\t  ).on_exception(\n\t\thandle_exception\n\t  )\n\t}\n\n\tself\n  end\n\n  def run(*input)\n\t@work.call(*input)\n  end\nend\n</code></pre>\n<p>Here is a simple usage of this lib (non-sense)...</p>\n<pre><code class=\"language-rb\">class CreateUser\n  def self.make(service)\n    self.new(service).method(:execute)\n  end\n\n  def initialize(service)\n    @service = service\n  end\n\n  def execute(data)\n    Result.new(@service.create_user(data))\n  rescue =&gt; e\n    Except.new(e)\n  end\nend\n\nclass SendWelcomeEmail\n  def self.make(mailerService)\n    self.new(mailerService).method(:execute)\n  end\n\n  def initialize(service)\n    @service = service\n  end\n\n  def execute(user)\n    mailer = @service.prepare_email(user)\n    if mailer.send\n      Result.new(user)\n    else\n      Failure.new({ error: 'failed_to_send_email', 'user': user })\n  rescue =&gt; e\n    Except.new(e)\n  end\nend\n\nclass EnqueueEmail\n  ...\n  def execute(data) do\n    if @mailer_job.enqueue_mail(data)\n      Result.new(data.user)\n    else\n      Failure.new({ error: 'failed_to_enqueue_email', user: data.user })\n  end\n  ... \nend\n\nPipe.new(CreateUser.make(UserService.new))\n\t.pipe(SendWelcomeEmail.make(MailerService.new))\n\t.pipe(-&gt;(user) { user }, ReenqueueEmail.make())\n\t.run(user_input)\n</code></pre>\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        </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"}
