<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nick Bauman</title>
    <description>The latest articles on DEV Community by Nick Bauman (@nickbauman).</description>
    <link>https://dev.to/nickbauman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F615774%2F1cedf6e1-e1b9-4ad4-9872-9618a3fd4c9e.png</url>
      <title>DEV Community: Nick Bauman</title>
      <link>https://dev.to/nickbauman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nickbauman"/>
    <language>en</language>
    <item>
      <title>From loop recur to reduce</title>
      <dc:creator>Nick Bauman</dc:creator>
      <pubDate>Tue, 28 Dec 2021 11:49:12 +0000</pubDate>
      <link>https://dev.to/nickbauman/from-loop-recur-to-reduce-4e44</link>
      <guid>https://dev.to/nickbauman/from-loop-recur-to-reduce-4e44</guid>
      <description>&lt;p&gt;In Clojure, do you find yourself reaching for a loop recur but you know you should be using reduce? Here's a really simple way to break out of that habit.&lt;/p&gt;

&lt;p&gt;Let's start with and example. Starting with &lt;code&gt;a2d&lt;/code&gt; which is the result of invoking &lt;code&gt;(to-array-2d [[11 2 4] [4 5 6] [10 8 -12]])&lt;/code&gt; and &lt;code&gt;len&lt;/code&gt; is the size of the matrix. Which is 3.&lt;/p&gt;

&lt;p&gt;This function, written with a loop/recur, calculates the "diagonal sum" of the values of a square matrix of some size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(defn accu [a2d len]
 (reduce +
  (loop [vals []
         i    (- len 1)]
   (if (&amp;gt; len (count vals))
    (recur (conj vals (aget a2d i i)) (dec i))
    vals))))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So invoking this function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(accu (to-array-2d [[11 2 4] [4 5 6] [10 8 -12]]) 3)
=&amp;gt; 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returns &lt;code&gt;4&lt;/code&gt; because 11 + 5 + -12 = 4&lt;/p&gt;

&lt;p&gt;So what does the &lt;code&gt;reduce&lt;/code&gt; version look like?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(defn accu [a2d len]
 (reduce +
  (reduce #(conj %1 (aget a2d %2 %2)) [] (reverse (range len)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the relevant code is almost exactly the same. Both use &lt;code&gt;(conj vals (aget a2d i i)) (dec i)&lt;/code&gt; to solve the problem. The loop/recur version has much more housekeeping and is not as fast as the reduce version.&lt;/p&gt;

</description>
      <category>clojure</category>
      <category>functional</category>
      <category>tutorial</category>
      <category>lisp</category>
    </item>
  </channel>
</rss>
