title: Real world examples of using reduceRight in JavaScript
body: <p>A while ago, I posted a question on StackOverflow showing that the <a href="https://stackoverflow.com/q/27252657/783743">native implementation of <code>reduceRight</code> in JavaScript is annoying</a>. Hence, I created a Haskell-style <code>foldr</code> function as a remedy:</p>
  
  <pre class="lang-js prettyprint-override"><code>function foldr(array, callback, initial) {
      var length = array.length;
  
      if (arguments.length &lt; 3) {
          if (length &gt; 0) var result = array[--length];
          else throw new Error("Reduce of empty array with no initial value");
      } else var result = initial;
  
      while (length &gt; 0) {
          var index = --length;
          result = callback(array[index], result, index, array);
      }
  
      return result;
  }
  </code></pre>
  
  <p>However, I never used this <code>foldr</code> function simply because I never needed to iterate over an array from right-to-left. This got me thinking, why don't I use <code>foldr</code> in JavaScript as much as I do in Haskell and what are some real world examples of using <code>foldr</code> in JavaScript?</p>
  
  <p>I could be wrong, but I believe that the <code>foldr</code> function is used widely in Haskell because of:</p>
  
  <ol>
  <li>Lazy Evaluation (<a href="https://stackoverflow.com/q/3429634/783743">foldl is tail recursive, so how come foldr runs faster than foldl?</a>)</li>
  <li>Short cut fusion using <code>foldr</code>/<code>build</code> (<a href="https://wiki.haskell.org/Correctness_of_short_cut_fusion#foldr.2Fbuild" rel="nofollow noreferrer">Correctness of short cut fusion: <code>foldr</code>/<code>build</code></a>)</li>
  </ol>
  
  <p>This would explain why <code>foldr</code> or <code>reduceRight</code> are not widely used in JavaScript. I have yet to see a real world use of <code>foldr</code> only for its right-to-left iteration order.</p>
  
  <p>This brings me to my two questions:</p>
  
  <ol>
  <li>What are some real world examples of using <code>reduceRight</code> in JavaScript? Perhaps you have used it in an npm package. It would be great if you could link me to your code and explain why you needed to use <code>reduceRight</code> instead of <code>reduce</code>.</li>
  <li>Why is <code>reduceRight</code> not used as widely as <code>reduce</code> is in JavaScript? I already provided my two cents on this matter. I believe that <code>foldr</code> is primarily used only for its laziness, which is why <code>reduceRight</code> is not very useful in JavaScript. However, I could be wrong.</li>
  </ol>
  
  <p>For the first question, I tried to find some real world examples for using <code>reduceRight</code> in JavaScript. However, I didn't find any satisfactory answers. The only examples I found were trivial and theoretical:</p>
  
  <p><a href="https://stackoverflow.com/q/20753527/783743">when to use reduce and reduceRight?</a></p>
  
  <p>What I am looking for is a practical example. When is it practical to use <code>reduceRight</code> in JavaScript instead of <code>reduce</code>?</p>
  
  <p>For the second question, I understand that it is primarily opinion based which is why it's alright if you don't answer it. The main focus of this post is on the first question, not the second.</p>