<?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: Mitch Henderson</title>
    <description>The latest articles on DEV Community by Mitch Henderson (@z2yjxfnrkb).</description>
    <link>https://dev.to/z2yjxfnrkb</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%2F646165%2Fc969b9fb-2a88-4a8e-a940-17b66327842f.jpg</url>
      <title>DEV Community: Mitch Henderson</title>
      <link>https://dev.to/z2yjxfnrkb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/z2yjxfnrkb"/>
    <language>en</language>
    <item>
      <title>foldLeft, map and filter in Scala</title>
      <dc:creator>Mitch Henderson</dc:creator>
      <pubDate>Sat, 14 Aug 2021 19:49:25 +0000</pubDate>
      <link>https://dev.to/z2yjxfnrkb/foldleft-map-and-filter-in-scala-3l6e</link>
      <guid>https://dev.to/z2yjxfnrkb/foldleft-map-and-filter-in-scala-3l6e</guid>
      <description>&lt;p&gt;What exactly are &lt;code&gt;foldLeft&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, and &lt;code&gt;filter&lt;/code&gt; in Scala? They are higher order functions, they operate on lists by taking in functions and applying these functions to a list(s).  These are a way of NOT writing loops, we don't need to think about the implementation of the loop. But, with these higher order functions, we are still thinking about what we want to do with each element in a list, and even though this method is not any faster, it's about 'expressivity' and can simplify what we want to say with our code.&lt;/p&gt;

&lt;p&gt;Let's start with &lt;code&gt;foldLeft&lt;/code&gt;, it is a useful method that takes a list, a function and a starting point and uses the function to operate on the list element by element.   &lt;/p&gt;

&lt;p&gt;For example, how can we get the sum of a list using foldLeft? Note that 'a' here is what we are folding into, whereas the 'b' is the next element from the list.&lt;/p&gt;

&lt;p&gt;//Get sum of a list using fold&lt;br&gt;
&lt;code&gt;List(1,2,3).foldLeft(0)((a,b) =&amp;gt; a+b)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In another example, here I show how can we add 1 to a list using &lt;code&gt;foldLeft&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;//Add a 1 to the list using fold&lt;br&gt;
&lt;code&gt;List(1,2,3).foldLeft(List[Int]())((a,b) =&amp;gt; a++List(b+1))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here I show how can we return a list containing only even numbers using &lt;code&gt;foldLeft&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;//Return a list w/ only even numbers using fold&lt;br&gt;
&lt;code&gt;List(1,2,3).foldLeft(List[Int]())((a,b) =&amp;gt; if (b%2 ==0) a ++ List(b) else a)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Looking at &lt;code&gt;map&lt;/code&gt;, on the other hand, we see that it takes a list and a function, and returns another list made up from the result of the function applied to each element. &lt;code&gt;map&lt;/code&gt; does not take a starting element. Here, 'a' is an element of the list, and 'a+1' is our operation.&lt;/p&gt;

&lt;p&gt;//adding one to each element of a list using &lt;code&gt;map&lt;/code&gt;&lt;br&gt;
&lt;code&gt;List(1,2,3).map((a) =&amp;gt; a+1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we look at &lt;code&gt;filter&lt;/code&gt;. &lt;code&gt;filter&lt;/code&gt; takes a list and a function, and the function takes an element from the list and returns a boolean. Here we have our list, we are using filter, using element 'a' and applying the modulo 2 in order to keep the even elements of the list.&lt;/p&gt;

&lt;p&gt;//return a list of only even elements using filter&lt;br&gt;
&lt;code&gt;List(1,2,3).filter((a) =&amp;gt; (a%2 == 0))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As can be seen, &lt;code&gt;foldLeft&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, and &lt;code&gt;filter&lt;/code&gt; are more about making our code more readable. We also see the concept 'expressivity', allowing us to more easily convey an idea to the reader of the code what we are trying to accomplish without necessitating a 'for' loop. Although so much of what I've learned has been about speed, this is a concept where the runtime speed is not decreased (or increased for that matter), but it does make the code more legible to the next person having to read it.&lt;/p&gt;

</description>
      <category>scala</category>
      <category>foldleft</category>
      <category>map</category>
      <category>filter</category>
    </item>
  </channel>
</rss>
