{"path": "/posts/zippers/", "title": "zippers · 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=\"zippers\">\n  <title>zippers · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/zippers/\" 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=\"zippers\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/zippers/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2023-03-23T12:47:32Z\" data-post-metadata>\n  <meta property=\"og:image\" content=\"https://diasbruno.github.io/images/20230323.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/zippers/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>zippers</h1>\n\n      <p><time datetime=\"2023-03-23T12:47:32Z\">2023-03-23</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<p>references:</p>\n<p><a href=\"https://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf\">Functional pearl - The zipper - Gérard Huet 1997</a></p>\n<hr />\n<p>zipper is way to navigate a data structure keeping\na focal point.</p>\n<p>the main operations are: <code>left</code>, <code>right</code>, <code>up</code> and <code>down</code>.</p>\n<p>they are very handy because when a point of interest\nis found, you can edit in-place with zero cost.</p>\n<p>let's right a simple zipper for an array (it will be a mutable\nstructure for convinience).</p>\n<p>a array zipper will have 3 properties:</p>\n<ul>\n<li>left</li>\n</ul>\n<p>keep the elements we walked, from start to focal point.</p>\n<ul>\n<li>at</li>\n</ul>\n<p>the current focusing element.</p>\n<ul>\n<li>right</li>\n</ul>\n<p>keep the elements from focal point to right.</p>\n<pre><code class=\"language-js\">function ArrayZipper(array) {\n  this._left = [];\n  this._at = array[0];\n  this._right = array.slice(1);\n}\n\nArrayZipper.of = (array) =&gt; new ArrayZipper(array);\n\nconst azp = ArrayZipper.prototype;\n\n// just get the focusing element\nazp.at = function() {\n  return this._at;\n};\n\n// replace the focusing element with a new value\nazp.edit = function(v) {\n  this._at = v;\n};\n</code></pre>\n<p>with our zipper, we can walk right...</p>\n<pre><code class=\"language-js\">azp.right = function() {\n  this._left.push(this._at);\n  this._at = this._right.shift();\n};\n\nlet z = ArrayZipper.of([1, 2, 3]);\n\nz.right(), z;\n// ArrayZipper { _left: [ 1 ], _at: 2, _right: [ 3 ] }\n\nz.right(), z;\n// ArrayZipper { _left: [ 1, 2 ], _at: 3, _right: [] }\n</code></pre>\n<p>or walking left...</p>\n<pre><code class=\"language-js\">azp.left = function() {\n  this._right = [this._at].concat(this._right);\n  this._at = this._left.pop();\n};\n\n// walking from our previous defined zipper\n\nz.edit(10), z;\n// ArrayZipper { _left: [ 1, 2 ], _at: 10, _right: [] }\n\nz.left(), z\n// ArrayZipper { _left: [ 1 ], _at: 2, _right: [ 10 ] }\n\nz.left(), z\n// ArrayZipper { _left: [], _at: 1, _right: [ 2, 10 ] }\n</code></pre>\n<p>but it can also conform to other interfaces.</p>\n<pre><code class=\"language-js\">azp.map = function(f) {\n  this._left = this._left.map(f);\n  this._at = f(this._at);\n  this._right = this._right.map(f);\n  return this;\n};\n\nz\n// ArrayZipper { _left: [], _at: 1, _right: [ 2, 10 ] }\n\nz.map(x =&gt; x + 1), z;\n// ArrayZipper { _left: [], _at: 2, _right: [ 3, 11 ] }\n</code></pre>\n<p>focus (just like <code>find</code>, but to look at an element).</p>\n<pre><code class=\"language-js\">azp.focus = function(f) {\n  const tmp = this._left.concat(this._at).concat(this._right);\n  const position = tmp.findIndex(f);\n  this._left = tmp.slice(0, position);\n  this._at = tmp[position];\n  this._right = tmp.slice(position + 1, tmp.length);\n};\n\nz\n// ArrayZipper { _left: [], _at: 2, _right: [ 3, 11 ] }\n\nz.focus(v =&gt; v == 11), console.log(z);\n// ArrayZipper { _left: [ 2, 3 ], _at: 11, _right: [] }\n</code></pre>\n<p>or...find, find...</p>\n<pre><code class=\"language-js\">azp.find = function(f) {\n  return this._left.find(f) ||\n\t(f(this._at) &amp;&amp; this._at) ||\n\tthis._right.find(f) ||\n\tnull;\n};\n\nz.find(v =&gt; v == 11)\n// 11\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"}
