title: Mimic Immutability by Generating Data Structures of the Same/Similar Shape (not the Same Type)
body: <p>Lately I frequently end up with different data structures of the same shape just to avoid altering the existing ones, so in order to mimic immutability. To make it easier to distinguish the layers of nested data structures and to keep them consistent with each other I wrap them in types that merely serve as "semantic wrappers". Here is the simplified pattern:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const ListX = xs =&gt;
    ({runListX: xs, [Symbol.toStringTag]: "ListX"});
  
  const ListY = xs =&gt;
    ({runListY: xs, [Symbol.toStringTag]: "ListY"});
  
  const foo = [ListX([ListY([1, 2, 3]), ListY([4, 5, 6])])];
  
  // later I gather more data that is related to foo but instead of
  // altering foo I just create a new data structure
  
  const bar = [ListX([ListY(["a", "b", "c"]), ListY(["d", "e", "f"])])];
  
  const fold = f =&gt; init =&gt; xs =&gt;
    xs.reduce((acc, x, i) =&gt; f(acc) (x, i), init);
  
  const combining = foo =&gt; bar =&gt;
    fold(acc1 =&gt; (tx, i) =&gt;
      fold(acc2 =&gt; (ty, j) =&gt;
        fold(acc3 =&gt; (x, k) =&gt; {
          const y = bar[i].runListX[j].runListY[k];
          return (acc3.push(x + y), acc3)
        }) (acc2) (ty.runListY)) (acc1) (tx.runListX)) ([]) (foo);
  
  console.log(
    combining(foo) (bar)); // ["1a","2b","3c","4d","5e","6f"]</code></pre>
  </div>
  </div>
  </p>
  
  <p>It works for me for programs with, say, 1000 lines of code. However, I wonder if it would still work for a larger code base. What are the long term drawbacks of this approach (maybe compared to real immutability gained by libs like immutable.js)? Has this approach/pattern even a name?</p>
  
  <p>Please note that I am aware that this kind of question is probably too broad for SO. Still, it's all over my head.</p>