<?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: Fahad Hossain</title>
    <description>The latest articles on DEV Community by Fahad Hossain (@fa7ad).</description>
    <link>https://dev.to/fa7ad</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%2F60107%2Face8e073-cf3b-405c-b13e-def05e085777.jpg</url>
      <title>DEV Community: Fahad Hossain</title>
      <link>https://dev.to/fa7ad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fa7ad"/>
    <language>en</language>
    <item>
      <title>The Trials and Tribulations. A Fool's Guide To Writing Functional JS (Part 4)</title>
      <dc:creator>Fahad Hossain</dc:creator>
      <pubDate>Sun, 16 Aug 2020 17:38:30 +0000</pubDate>
      <link>https://dev.to/fa7ad/the-trials-and-tribulations-a-fool-s-guide-to-writing-functional-js-part-4-95</link>
      <guid>https://dev.to/fa7ad/the-trials-and-tribulations-a-fool-s-guide-to-writing-functional-js-part-4-95</guid>
      <description>&lt;p&gt;It's been a while since my &lt;a href="https://dev.to/fa7ad/part-3-some-higher-order-functions-a-fool-s-guide-to-writing-functional-js-478l"&gt;last article&lt;/a&gt; so, hopefully you didn't forget what we last discussed. Throughout the last 3 articles I alluded to some nifty tricks and nice-ties like &lt;code&gt;compose&lt;/code&gt;. So in this article we will try to cover some of these topics.&lt;/p&gt;

&lt;p&gt;Let's see some problems that we come across when starting out our FP journey in JS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem #1
&lt;/h2&gt;

&lt;p&gt;If you have been writing functional code, or even just trying to avoid mutations and side-effects, you probably ended up writing something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// some data&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;doSomethingCompletelyDiff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doSomethingElse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At some point, making this kinda nested function calls becomes inevitable; specially if you don't want to make really specific, single-use functions. But this kind of function calls are not only ugly to look at, sometimes they are difficult to follow as well. Specially if you add some higher-order functions in between, then you've got a nasty mess of parentheses.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What if we had something that could combine multiple functions to a single function?&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;We do have precisely that exact tool. (Un)surprisingly, it's another one of those things that we borrow from math.&lt;/p&gt;

&lt;p&gt;Let's see it in math first.&lt;/p&gt;

&lt;p&gt;Lets define a function &lt;em&gt;f(x)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dBQxX2oW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3Bf%26space%3B:%26space%3Bx%26space%3B%255Cmapsto%26space%3Bx%26space%3B%26plus%3B%26space%3B1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dBQxX2oW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3Bf%26space%3B:%26space%3Bx%26space%3B%255Cmapsto%26space%3Bx%26space%3B%26plus%3B%26space%3B1" title="\large f : x \mapsto x + 1" width="189" height="30"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And another function &lt;em&gt;g(x)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WeW_2R6Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3Bg%26space%3B:%26space%3Bx%26space%3B%255Cmapsto%26space%3Bx%26space%3B%2A%26space%3B2" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WeW_2R6Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3Bg%26space%3B:%26space%3Bx%26space%3B%255Cmapsto%26space%3Bx%26space%3B%2A%26space%3B2" title="\large g : x \mapsto x * 2" width="180" height="29"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets take a letter &lt;code&gt;y&lt;/code&gt; and let its value be...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BXkb5EgH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3By%26space%3B%3D%26space%3Bf%28g%2810%29%29" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BXkb5EgH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3By%26space%3B%3D%26space%3Bf%28g%2810%29%29" title="\large y = f(g(10))" width="178" height="33"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see our old friend nested function call here again... So how did math come up with a solution?&lt;/p&gt;

&lt;p&gt;They made an operator, of course. This operator allows you to define a function by &lt;strong&gt;&lt;em&gt;composing&lt;/em&gt;&lt;/strong&gt; multiple functions and as such, it's called the &lt;em&gt;composition&lt;/em&gt; operator. Visually, it looks like a tiny circle.&lt;/p&gt;

&lt;p&gt;Here's composition operator put to use,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DDKWJX-p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3Bh%26space%3B%3D%26space%3Bf%26space%3B%255Ccirc%26space%3Bg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DDKWJX-p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3Bh%26space%3B%3D%26space%3Bf%26space%3B%255Ccirc%26space%3Bg" title="\large h = f \circ g" width="129" height="30"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;em&gt;h&lt;/em&gt; is defined as the composition of &lt;em&gt;g&lt;/em&gt; and &lt;em&gt;f&lt;/em&gt;. Functionally, calling &lt;code&gt;h(x)&lt;/code&gt; is the same as calling &lt;code&gt;f(g(x))&lt;/code&gt; So now, we can do this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aO2wNbq4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3By%27%26space%3B%3D%26space%3Bh%2810%29" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aO2wNbq4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.codecogs.com/png.latex%3F%255Cinline%26space%3B%255Cdpi%257B200%257D%26space%3B%255Cbg_black%26space%3B%255Clarge%26space%3By%27%26space%3B%3D%26space%3Bh%2810%29" title="\large y' = h(10)" width="143" height="33"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Great"&lt;/em&gt; I hear you say, &lt;em&gt;"How does that help me in code, I can't just put a big ol' dot between two functions in JS can I?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You can!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No, not put a big ol' dot... you can compose functions in JS just not using an operator.&lt;/p&gt;

&lt;p&gt;It goes something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newSuperFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lastFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;firstFn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Doesn't look too horrible, if you ask me 🤷&lt;/p&gt;

&lt;p&gt;"But where did this &lt;em&gt;compose&lt;/em&gt; function come from, and how do I get one?" I hear you say&lt;/p&gt;

&lt;p&gt;I'm glad you asked, remember our old friend the fold (reduce)? We can very easily define compose using a right fold (we discussed a left fold before, this is the same thing but from the other direction).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;funcs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;funcs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduceRight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;gx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;gx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fx&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, our &lt;code&gt;compose&lt;/code&gt; function takes a &lt;em&gt;variadic&lt;/em&gt; number of arguments, all of which are functions.&lt;/p&gt;

&lt;p&gt;Because of the rest operator we get the functions as an array, so we can call the built-in &lt;code&gt;reduceRight&lt;/code&gt; function from the array prototype.&lt;/p&gt;

&lt;p&gt;Then, we pass in a argument to the &lt;code&gt;reduceRight&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;We pass a function that receives two functions as arguments, and returns an inline variadic function that calls the two functions successively with its arguments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The variadic (any number of arguments) bit was added so that we can compose non-unary (more than one argument) functions too&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Happy composing, Enjoy!&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem #2
&lt;/h2&gt;

&lt;p&gt;Once you understood map, filter and reduce you just can't go back. You write code that looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataSet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Some dataset&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;finalData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dataSet&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someTransformation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someFurtherTransformation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;somePredicate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;anotherPredicate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evenMoreTransformation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;combinatorFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this code is &lt;strong&gt;&lt;em&gt;very&lt;/em&gt;&lt;/strong&gt; contrived but you get the idea. If you don't want to make a bunch of one-off transformations and predicates you end up doing this.&lt;/p&gt;

&lt;p&gt;Even if it might not seem like a problem at first, you will start to see a crash coming from miles away as soon as your dataset becomes large enough.&lt;/p&gt;

&lt;p&gt;The problem with this kind of code is that every map and filter and reduce call is an iteration. In imperative code you might be used to doing a lot of transformation and &lt;em&gt;filtering&lt;/em&gt; out in a single loop, which looks almost impossible to do here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;Just a word of warning, there are better solutions out there. But, for now we can discuss some simple ones that will help you write better code regardless of better solutions. If these do not help your use case enough, do a little digging into &lt;em&gt;transducers&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combine successive transformations/maps
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someTransformation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someFurtherTransformation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can be rewritten using our friend &lt;code&gt;compose&lt;/code&gt; like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someFurterTransformation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;someTransformation&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the benefit may not be obvious, what you are essentially doing is that you are running 2 transformations in a single iteration rather than running 2 iterations with 1 transformation each. Which means, 1 less iteration but the same result&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combine successive filters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You might get the idea that we can combine filters the same way we combined maps.&lt;br&gt;
  However, the moment you go to do such a thing, you realise that you forgot that composing predicates just won't work.&lt;/p&gt;

&lt;p&gt;But we can get around that by implement a logical &lt;code&gt;and&lt;/code&gt; function to combine predicates, since that's essentially what two successive filters do anyway.&lt;br&gt;&lt;br&gt;
   Let's try to implement a binary &lt;code&gt;and&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pred1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pred2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;pred1&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;pred2&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now we can rewrite this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;somePredicate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;anotherPredicate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;and&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;somePredicate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;anotherPredicate&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can combine more than two predicates by nesting calls to the &lt;code&gt;and&lt;/code&gt; function. But I recommend checking out &lt;em&gt;Ramda&lt;/em&gt;'s &lt;code&gt;allPass&lt;/code&gt; and &lt;code&gt;both&lt;/code&gt; functions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why re-invent the wheel when the wheel is so nice?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Problem #3
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;I write too many helper functions like compose, pipe, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a comment I hear from friend very often. And... I'll admit, I had this problem too. While writing helper functions isn't bad, it can be bad when you spend more time writing them than writing your business logic.&lt;/p&gt;

&lt;p&gt;The reason why so many JS devs have this problem is that our language gives the very basic tools to get us hooked on FP and then leaves us high and dry and wanting more. So we end up implementing a lot of basic fundamental FP constructs ourselves.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution (not really, but it is what it is)
&lt;/h3&gt;

&lt;p&gt;A lot of this can be avoided by adding a Functional utility library to your toolchain. I &lt;em&gt;highly recommend&lt;/em&gt; &lt;strong&gt;Ramda&lt;/strong&gt;, because it gives a lot of functionality at a very reasonable file size. All of its functions are at least on par with some language built-in implementations, if not better, in terms of performance. And the cherry on top, its tree-shakeable; so almost any build system like webpack or rollup can remove the functions you don't use from the final JS bundle.&lt;/p&gt;




&lt;h2&gt;
  
  
  That's enough problems for a day
&lt;/h2&gt;

&lt;p&gt;Enjoy writing more FP code. I'll try to write about a few more problems and solutions.&lt;/p&gt;

&lt;p&gt;Till then, Peace ✌️&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3EXsJPQE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--5QyGr6Q0--/c_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880/https://i.loli.net/2020/06/13/YxJLIu3ADvrHfog.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3EXsJPQE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--5QyGr6Q0--/c_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880/https://i.loli.net/2020/06/13/YxJLIu3ADvrHfog.gif" alt="peace" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>functional</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Some Higher-Order Functions. A Fool's Guide To Writing Functional JS (Part 3)</title>
      <dc:creator>Fahad Hossain</dc:creator>
      <pubDate>Fri, 12 Jun 2020 16:41:10 +0000</pubDate>
      <link>https://dev.to/fa7ad/part-3-some-higher-order-functions-a-fool-s-guide-to-writing-functional-js-478l</link>
      <guid>https://dev.to/fa7ad/part-3-some-higher-order-functions-a-fool-s-guide-to-writing-functional-js-478l</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/fa7ad/part-2-functions-a-fool-s-guide-to-writing-functional-js-4p5f"&gt;last article&lt;/a&gt; we discussed the basics of &lt;strong&gt;Functions&lt;/strong&gt;. We saw some definitions and examples of &lt;em&gt;Higher-Order Functions&lt;/em&gt;. But that might have left you high and dry.&lt;/p&gt;

&lt;p&gt;You could be wondering,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How is any of this terminology useful to me?&lt;/p&gt;

&lt;p&gt;Sure, now I know what functions are. How do I use them?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All these are very valid responses to that article, I didn't cover any day-to-day uses for Higher-Order Functions (&lt;em&gt;the article was already getting too long&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;So, in this article, we will try to use some common higher-order functions. Namely, &lt;em&gt;map&lt;/em&gt;, &lt;em&gt;filter&lt;/em&gt;, and &lt;em&gt;fold&lt;/em&gt;(reduce).&lt;/p&gt;

&lt;h2&gt;
  
  
  A little refresher
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A function is a mapping between some input and some output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Higher-Order function is a function that maps between function(s) and data (as either input and/or output)&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Let's get to it!
&lt;/h3&gt;

&lt;h2&gt;
  
  
  map
&lt;/h2&gt;

&lt;p&gt;We'll get right to the definition.&lt;/p&gt;

&lt;p&gt;According to wikipedia (and most literature),&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;map&lt;/strong&gt; is the name of a higher-order function that applies a given function to each element of a functor&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might be cursing and saying&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What the F@#$ is  a functor?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's ignore that for now, and try to define map in a way that sounds (&lt;em&gt;a bit more&lt;/em&gt;) human,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;map&lt;/strong&gt; is a function that takes a container data structure and applies a function to &lt;em&gt;each&lt;/em&gt; value and creates a new container with the results of said application&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Or,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;map&lt;/strong&gt; is a thing that takes a collection of things and gives you a new collection of things by applying a function over each thing&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Notice how I'm trying to avoid naming any data structures?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That's partially to not piss of the FP neckbeards and the Lambda gods, but also to make it clear that map can be implemented in any data structure*. Like most FP concepts, it's very abstract and can be applied to a whole grocery list of things. &lt;/p&gt;

&lt;p&gt;JavaScript only implements &lt;em&gt;map&lt;/em&gt; (natively) in only one Data Structure, &lt;strong&gt;Array&lt;/strong&gt;. It's implemented as a function on the Array prototype. &lt;em&gt;But, it doesn't have to be tied down to Arrays (😉)&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: If you are already familiar with Array.prototype.map, try not to think of it as a 1:1 implementation of the concept of map&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's look at an example of using JavaScript's map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;carrot&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// The collection&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstLetter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// Our transformation&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstLetters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstLetter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// The new collection.&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; ['a', 'b', 'c']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, what's happening here?&lt;/p&gt;

&lt;p&gt;Let's start from the top, we defined an array named &lt;code&gt;fruits&lt;/code&gt; and stored a few strings in it.&lt;/p&gt;

&lt;p&gt;Next, we defined a function named &lt;code&gt;firstLetter&lt;/code&gt; that takes a string input and return's its first character.&lt;/p&gt;

&lt;p&gt;Then, we make a call to &lt;em&gt;Array.prototype.map&lt;/em&gt; by invoking &lt;code&gt;fruits.map&lt;/code&gt; with the argument &lt;code&gt;firstLetter&lt;/code&gt;. What this is doing is, telling the map function to iterate over every element contained by &lt;code&gt;fruits&lt;/code&gt; and &lt;em&gt;apply&lt;/em&gt; &lt;code&gt;firstLetter&lt;/code&gt; to each element, store the results in a new array, and then return the new resulting array. This return value is what we assign to &lt;code&gt;firstLetters&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EQ_WhFfU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.loli.net/2020/05/30/Ee5o3WysuqVGP46.png" alt="array-map" width="880" height="733"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Illustration adapted from &lt;a href="https://atendesigngroup.com/blog/array-map-filter-and-reduce-js"&gt;John Ferris' article&lt;/a&gt;&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Libraries like &lt;em&gt;Ramda&lt;/em&gt;(seriously awesome, check it out) allow you to map over additional data structures such as objects. Let's try to implement a map (using mutable code) that works for both containers (object &amp;amp; array).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ftor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ftor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// Some exotic container given, degrade to Object&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;ftor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ftor&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this map there's a bunch of different things happening, but keep in mind that for an ordinary array, its functionally same.&lt;/p&gt;

&lt;p&gt;Lets try to break it down,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arguments:&lt;/strong&gt; this function takes two arguments, &lt;code&gt;func&lt;/code&gt; and &lt;code&gt;ftor&lt;/code&gt;. As the name might imply, &lt;code&gt;func&lt;/code&gt; is our function (the transformation). &lt;code&gt;ftor&lt;/code&gt; might seem like a weird name for the second argument, this argument is your data structure (array, object, etc.). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So why is it called &lt;code&gt;ftor&lt;/code&gt; and not something like &lt;code&gt;data&lt;/code&gt; or &lt;code&gt;array&lt;/code&gt;? Remember that word we used in the first definition (from Wikipedia)? Yeah &lt;strong&gt;Functor&lt;/strong&gt;, &lt;code&gt;ftor&lt;/code&gt; is my way of writing functor. A &lt;strong&gt;Functor&lt;/strong&gt; is basically any data structure that you can map over*. So, in our case Object and Array (and potentially other data structures that store key-&amp;gt;value) are both Functors from the perspective of our map function even though natively, only Arrays might be considered &lt;em&gt;Functors&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Congrats! Now you know another FP buzzword/jargon.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Line 8-9:&lt;/strong&gt; here, we are iterating through the keys of the container (indices in case of arrays) and applying the function &lt;code&gt;func&lt;/code&gt; to each value and associating it with the same key in the resulting container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; this function returns a container of the same type as the functor (by calling its constructor), in cases where it fails, I've decided to degrade down to a plain object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;This comes in handy when you need to make a collection of things from an existing collection by &lt;em&gt;transforming&lt;/em&gt; each value.&lt;/p&gt;

&lt;h2&gt;
  
  
  filter
&lt;/h2&gt;

&lt;p&gt;Once again, here goes the wikipedia definition&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;filter&lt;/strong&gt; is a higher-order function that processes a data structure (usually a list) in some order to produce a new data structure containing exactly those elements of the original data structure for which a given predicate returns the boolean value &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This time, I think the wiki definition is very expressive. Put differently,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;filter&lt;/strong&gt; is a function that goes through a &lt;em&gt;Filterable&lt;/em&gt; collection and makes a new collection that contains only the values for which the &lt;em&gt;predicate&lt;/em&gt; returns &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That might sound like a mouthful, but the concept is simple (you will see for yourself once we go through an example).&lt;/p&gt;

&lt;p&gt;Once again, JS has a native implementation of &lt;em&gt;filter&lt;/em&gt;, but only in &lt;strong&gt;Arrays&lt;/strong&gt;. Same as &lt;strong&gt;map&lt;/strong&gt;, its implemented in the Array prototype. &lt;em&gt;But it could be used with any **Filterable&lt;/em&gt;* data structure.*&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Similar to &lt;em&gt;Functors&lt;/em&gt;, a &lt;em&gt;Filterable&lt;/em&gt; is a data structure that you can &lt;em&gt;filter&lt;/em&gt;. Most &lt;em&gt;Functors&lt;/em&gt; tend to be &lt;em&gt;Filterable&lt;/em&gt; as well, but there is no guarantee that a &lt;em&gt;Functor&lt;/em&gt; is &lt;em&gt;Filterable&lt;/em&gt;. If we assume that &lt;em&gt;Array.prototype.filter&lt;/em&gt;* is the only possible implementation of filter, then only &lt;strong&gt;Array&lt;/strong&gt;s can be considered &lt;em&gt;Filterable&lt;/em&gt;. But, because we can write a function that can &lt;em&gt;filter&lt;/em&gt; &lt;strong&gt;objects&lt;/strong&gt;, we can consider plain JS objects as &lt;em&gt;Filterable&lt;/em&gt; too.&lt;/p&gt;

&lt;p&gt;Now you know another Category of data structures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; It should go without saying, Array.prototype.filter may/may not be 1:1 with the spec of filter&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's look at an example of JavaScript's filter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The collection&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// The predicate (a function that returns either true or false)&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isCitrus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruit&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="sr"&gt;/lemon|lime|orange|grapefruit/i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// The new collection&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;citrusFruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isCitrus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's start from the top, we defined an array named &lt;code&gt;fruits&lt;/code&gt; and stored a few strings in it (same as our &lt;em&gt;map&lt;/em&gt; example).&lt;/p&gt;

&lt;p&gt;Next, we defined a function named &lt;code&gt;isCitrus&lt;/code&gt; that takes a string input and checks it against a regular expression and returns either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, we make a call to &lt;em&gt;Array.prototype.filter&lt;/em&gt; by invoking &lt;code&gt;fruits.filter&lt;/code&gt; with the argument &lt;code&gt;isCitrus&lt;/code&gt;. What this does is, tell the filter function to iterate over every element contained by &lt;code&gt;fruits&lt;/code&gt; and call &lt;code&gt;isCitrus&lt;/code&gt; with each element as argument, if &lt;code&gt;isCitrus&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt; that elements is kept, otherwise the element is skipped over and the next element is checked. This process is repeated for all the elements of the array. An array is constructed containing only the elements for which &lt;code&gt;isCitrus&lt;/code&gt; returned &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sSMqU-in--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.loli.net/2020/06/02/mc417p3ewoKInJr.png" alt="array-filter" width="880" height="733"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Illustration adapted from &lt;a href="https://atendesigngroup.com/blog/array-map-filter-and-reduce-js"&gt;John Ferris' article&lt;/a&gt;&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's try to implement a filter (using mutable code) that works for different containers (object &amp;amp; array).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;filterable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;filterable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error on trying to call &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;filterable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arrKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filterable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;filterable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filterable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isArray&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;arrKey&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;filterable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this filter there's a bunch of different things happening, but keep in mind that for an ordinary array, its functionally the same as calling Array.protorype.filter.&lt;/p&gt;

&lt;p&gt;Lets try to break it down,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arguments:&lt;/strong&gt; this function takes two arguments, &lt;code&gt;predicate&lt;/code&gt; and &lt;code&gt;filterable&lt;/code&gt;. As the name might imply, &lt;code&gt;predicate&lt;/code&gt; is our predicate (a function that takes a value and returns either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;). The argument &lt;code&gt;filterable&lt;/code&gt; is your data structure (array, object, etc.). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 11-16:&lt;/strong&gt; here, we are iterating through the keys of the container (indices in case of arrays) and checking if the &lt;code&gt;predicate&lt;/code&gt; returns true for a particular value. If a value does return true, we are keeping it in the &lt;code&gt;result&lt;/code&gt; container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; this function returns a container of the same type as the filterable (by calling its constructor), in cases where it fails, I've decided to degrade down to a plain object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;This comes in handy when you need to make a collection of things from an existing collection by keeping values that meet a certain criteria.&lt;/p&gt;

&lt;h2&gt;
  
  
  fold (reduce)
&lt;/h2&gt;

&lt;p&gt;You know the drill, wiki first&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;fold&lt;/strong&gt; (also termed &lt;strong&gt;reduce&lt;/strong&gt;...) are ... functions that analyze a recursive data structure and through use of a given combining operation, recombine the results of recursively processing its constituent parts, building up a return value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lots of stuff to unpack there, but lets try to get to the gist of it&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;fold&lt;/strong&gt; is a function that goes trough a &lt;em&gt;Foldable&lt;/em&gt; collection and accumulates a value using an &lt;em&gt;accumulating function&lt;/em&gt; and then finally returns the accumulated value&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To a shock to nobody, JavaScript has a native implementation of &lt;em&gt;fold&lt;/em&gt; as well, its named &lt;em&gt;Array.prototype.reduce&lt;/em&gt;. Once again we have to make the note that JS's &lt;em&gt;reduce/fold&lt;/em&gt; can only fold arrays, &lt;em&gt;but it doesn't have to be tied down to just JS arrays&lt;/em&gt;. A fold can be implemented for any data structure that can be classified as &lt;em&gt;Foldable&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A data type is &lt;em&gt;Foldable&lt;/em&gt; if we can implement some form of &lt;em&gt;fold&lt;/em&gt; for it. But due to the nature of the &lt;em&gt;fold&lt;/em&gt; operation, Foldables are usually list(array)-like or have a valid list-representation. While not strictly required, most &lt;em&gt;Foldables&lt;/em&gt; tend to be &lt;em&gt;Functor&lt;/em&gt; and  &lt;em&gt;Filterable&lt;/em&gt; as well. This is because both &lt;em&gt;map&lt;/em&gt; and &lt;em&gt;filter&lt;/em&gt; can be implemented using &lt;em&gt;fold&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Another note, there are many variations of &lt;em&gt;fold&lt;/em&gt; out there. The essential functionality is the same but some implementation details change the nature and name of the fold. In this article we will look at a left-fold as the native &lt;em&gt;reduce&lt;/em&gt; method is a left-fold. This fold is also called &lt;em&gt;foldl&lt;/em&gt; or &lt;em&gt;fold_left&lt;/em&gt; in some languages and libraries.&lt;/p&gt;

&lt;p&gt;Once again, disclaimer: JS reduce may not follow the spec of &lt;em&gt;fold&lt;/em&gt; 100%.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's try using &lt;em&gt;Array.prototype.reduce&lt;/em&gt; to do something.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The collection&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// The accumulating function&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;makeSalad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;salad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;salad&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;

&lt;span class="c1"&gt;// Inital Salad&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;saladStarter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;salad&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;// The Result&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;salad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;makeSalad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;saladStarter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//=&amp;gt; orange-banana-apple-salad&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's start from the top again, we defined an array named &lt;code&gt;fruits&lt;/code&gt; and stored a few strings in it.&lt;/p&gt;

&lt;p&gt;Next, we defined a function named &lt;code&gt;makeSalad&lt;/code&gt; that takes two strings and returns a string by concatenating them.&lt;/p&gt;

&lt;p&gt;We then define another variables, this time its a string named &lt;code&gt;saladStarter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, we make a call to &lt;em&gt;Array.prototype.reduce&lt;/em&gt; by invoking &lt;code&gt;fruits.reduce&lt;/code&gt; with the arguments &lt;code&gt;makeSalad&lt;/code&gt; and &lt;code&gt;saladStarter&lt;/code&gt;. What this does is, it tells the fold function to iterate over every element contained in &lt;code&gt;fruits&lt;/code&gt; and call &lt;code&gt;makeSalad&lt;/code&gt; with an &lt;em&gt;accumulated value&lt;/em&gt; and an element from &lt;code&gt;fruits&lt;/code&gt;. For the first iteration, there is no accumulated value, so &lt;code&gt;makeSalad&lt;/code&gt; is called with &lt;code&gt;saladStarter&lt;/code&gt; as the &lt;em&gt;accumulated value&lt;/em&gt;. For every subsequent iteration, &lt;code&gt;makeSalad&lt;/code&gt; is called with the return value of the previous iteration as the &lt;em&gt;accumulated value&lt;/em&gt; and the next item in the array. This process is continued until &lt;code&gt;makeSalad&lt;/code&gt; has been called with the accumulated value from its previous iteration and the last item in &lt;code&gt;fruits&lt;/code&gt;. Finally the return value from the final call is passed on as the return value for the &lt;em&gt;reduce&lt;/em&gt; call and stored in the variable named &lt;code&gt;salad&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--59aS7Zh---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.loli.net/2020/06/13/mv6y8d7qrEHenFs.png" alt="array-reduce" width="880" height="733"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Illustration adapted from &lt;a href="https://atendesigngroup.com/blog/array-map-filter-and-reduce-js"&gt;John Ferris' article&lt;/a&gt;&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's try to implement a &lt;em&gt;fold&lt;/em&gt; of our own. Using mutable and imperative code, of course.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fold_left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;folding_fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inital_value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;foldable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;accumulated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;inital_value&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;foldable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;accumulated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;folding_fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulated&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;foldable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulated&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might be thinking...&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What? Where is all the code?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Folds are notoriously very simple to implement, but they are so useful that you'll find yourself wondering why more people don't use them.&lt;/p&gt;

&lt;p&gt;I think its pretty obvious how this function works, so I won't bore you with the explanation. Let's instead come back to our claim that we can usually &lt;em&gt;map&lt;/em&gt; and &lt;em&gt;filter&lt;/em&gt; using a &lt;em&gt;fold&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  map
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// le folded map&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ftr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;fold_left&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="nx"&gt;ftr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;ftr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yeah, this code is not very readable, but its not meant to be. This is a &lt;strong&gt;one-liner&lt;/strong&gt; that shows a very simple implementation of map using fold. It works because fold carries the return value from the accumulating function on to the next iteration, allowing us to successively construct a larger list of values resulting from applying &lt;code&gt;fn&lt;/code&gt; to &lt;code&gt;val&lt;/code&gt;. Try and tinker with it a little bit, and I have faith that you will figure it out.&lt;/p&gt;

&lt;p&gt;On to the next one...&lt;/p&gt;

&lt;h3&gt;
  
  
  filter
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// le folded filter&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pred&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;flt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;fold_left&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;pred&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;flt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;flt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once again, this is a &lt;strong&gt;one-liner&lt;/strong&gt;. This follows the same principle as map, except we are only concatenating to the list if the predicate is satisfied by the value (i.e., &lt;code&gt;pred(val)&lt;/code&gt; returns &lt;em&gt;true&lt;/em&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;Folds should come in handy when you need to,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iterate over a list and carry over a value to the next iteration&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Fold&lt;/em&gt; a list onto itself to arrive at a single value&lt;/li&gt;
&lt;li&gt;Transform a list to a single value (even if the resulting value is of a completely different type, like transforming the items of a list to be items of a Map or a Set)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Appendix / Additional Links
&lt;/h2&gt;

&lt;p&gt;I talk briefly about a few &lt;em&gt;Categories&lt;/em&gt; of data types. If you wanna look at more of these categories, take a look at the wonderful &lt;a href="https://github.com/fantasyland/fantasy-land"&gt;&lt;strong&gt;fantasy-land&lt;/strong&gt;&lt;/a&gt; specifications that defines &lt;em&gt;Algebraic&lt;/em&gt; Data Types in terminology we JS devs can understand.&lt;/p&gt;

&lt;p&gt;Also check out the amazing &lt;a href="https://ramdajs.com/"&gt;&lt;strong&gt;ramda&lt;/strong&gt;&lt;/a&gt; library for more useful functional utilities like &lt;em&gt;performant&lt;/em&gt; and &lt;em&gt;curried&lt;/em&gt; implementations of &lt;em&gt;map&lt;/em&gt;, &lt;em&gt;filter&lt;/em&gt;, &lt;em&gt;reduce&lt;/em&gt; or even helper functions that help you easily combine these operations into a &lt;em&gt;transducer&lt;/em&gt; (more on this in a later article).&lt;/p&gt;

&lt;p&gt;If you are even slightly mathematically minded, lookup Category theory as well as Algebraic Data Types. These are wonderful topics to study regardless, but they also help us understand the world of FP even better.&lt;/p&gt;




&lt;h2&gt;
  
  
  That's all for today, folks.
&lt;/h2&gt;

&lt;p&gt;Peace ✌️&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5QyGr6Q0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.loli.net/2020/06/13/YxJLIu3ADvrHfog.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5QyGr6Q0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.loli.net/2020/06/13/YxJLIu3ADvrHfog.gif" alt="doggy-dogg-peace" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>functional</category>
      <category>javascript</category>
      <category>functions</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Functions. A Fool's Guide To Writing Functional JS (Part 2)</title>
      <dc:creator>Fahad Hossain</dc:creator>
      <pubDate>Sat, 25 Apr 2020 17:34:39 +0000</pubDate>
      <link>https://dev.to/fa7ad/part-2-functions-a-fool-s-guide-to-writing-functional-js-4p5f</link>
      <guid>https://dev.to/fa7ad/part-2-functions-a-fool-s-guide-to-writing-functional-js-4p5f</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/fa7ad/a-fool-s-guide-to-writing-functional-js-part-1-the-introduction-nco"&gt;last article&lt;/a&gt; I introduced some core concepts of Functional Programming. In that article we quickly glossed over Pure Functions. In this article I try to discuss functions in more details.&lt;/p&gt;

&lt;p&gt;If there is one thing that you can call absolutely necessary to do functional programming is a good understanding of &lt;em&gt;Functions&lt;/em&gt;. &lt;em&gt;(I guess you could infer that from the name, *Function*al Programming)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a function?
&lt;/h2&gt;

&lt;p&gt;If you are a programmer of any kind, you are probably already familiar with functions. But, I will still ask you, what is a function?&lt;/p&gt;

&lt;p&gt;I hear some JS folk going, I know what a function is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Its this (👆) thing. Or this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or the real clever guy thinking, its this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, all of those are functions.&lt;br&gt;
But those are examples of functions. What I want you to think about is,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is a function? What does it do?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you may/may not be aware, the world of computer science is very intertwined with the world of mathematics. Functions are one of many things that we borrow from the world of mathematics.&lt;/p&gt;

&lt;p&gt;However, math is very abstract (for good reason). And so, if you look for a definition of functions in mathematics, you will find multiple. One that I like is,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A function is a relation between 2 sets&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Or in less abstract, computer science-y terms&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A function is a mapping between an input and an output&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, we can say that a function is a &lt;em&gt;thing&lt;/em&gt; that takes some &lt;strong&gt;input&lt;/strong&gt; and gives back some &lt;strong&gt;output&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now there are several rules that a &lt;em&gt;thing&lt;/em&gt; must follow before it can be considered a function, the most important ones are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple inputs can map to a single output
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// for a function fx,&lt;/span&gt;
  &lt;span class="nx"&gt;fx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; true&lt;/span&gt;
  &lt;span class="nx"&gt;fx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Same input can not map to multiple outputs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is because that would lead to non deterministic behaviour and this is undesirable in both computer science and math.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;fx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// it should not be true once and false the next second.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you may be thinking,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What about functions that don't return anything.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I don't know (nor care for the purposes of this discussion) about the behaviour of this in other languages, but in JS your functions always return something whether you want to or not.&lt;/p&gt;

&lt;p&gt;If you do return something, that's well and good.&lt;br&gt;
However, if you don't, JS returns &lt;code&gt;undefined&lt;/code&gt; for you.&lt;/p&gt;

&lt;p&gt;So your function that returns nothing, is actually a mapping from some input to &lt;code&gt;undefined&lt;/code&gt;. But more importantly, you might be getting the results of such a function in some other place, maybe the function is pushing the result on to a variable outside its scope.&lt;br&gt;
In that case, &lt;strong&gt;Its an impure function&lt;/strong&gt; and its causing side effect. And you should probably avoid doing that (when possible).&lt;/p&gt;

&lt;p&gt;But you might be thinking,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What about functions that don't take an input.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This can go one of several ways,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Your function always returns something valuable if you call it with no input.&lt;br&gt;&lt;br&gt;
Your function is a mapping from a null set (nothing) to a value, &lt;strong&gt;Its &lt;br&gt;
a function&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Your function takes no input and returns nothing (or &lt;code&gt;undefined&lt;/code&gt;) as we discussed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Its useless (i.e. not doing anything), but &lt;strong&gt;Its a function&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Its useful (i.e. gives some output), but its output is not available as a return value, &lt;strong&gt;Its (probably) not a (&lt;em&gt;pure&lt;/em&gt;) function&lt;/strong&gt; and you should try and avoid these as its making a &lt;em&gt;side effect!&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, we now know what functions are, that's it right? that's all there is to know about functions?&lt;/p&gt;

&lt;p&gt;No, my dear reader. You can dig yourself into a tunnel, learning about functions. But, for our purposes of learning FP in JS. We can talk about some special types of functions that follow the rules above and do some interesting things.&lt;/p&gt;
&lt;h2&gt;
  
  
  Recursive Functions
&lt;/h2&gt;

&lt;p&gt;Do you know what factorials are?&lt;br&gt;
Its this thing you express in math with an exclamation mark after a number like &lt;code&gt;5!&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What's it do? Its an interesting bit of math, its useful for a lot of things that we are not going to discuss right now. The important bit is that we can not just use a number followed by and exclamation mark after it to get a factorial in code. We need to make that functionality ourselves.&lt;/p&gt;

&lt;p&gt;Luckily, factorials are very intuitive. There's just two rules about factorials, and you can get the factorial of any number with those.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Factorial of 0 is 1.&lt;br&gt;
Or,&lt;br&gt;
&lt;code&gt;0! = 1&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Factorial of a number X is X multiplied by the factorial of (X-1).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Or,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;n! = n ✕ (n-1)!&lt;/code&gt;&lt;br&gt;
   Example:&lt;br&gt;
   &lt;code&gt;5! = 5 * 4!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So if we wanted to find the factorial of 3, it would go something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3! = 3 * 2!
2! = 2 * 1!
1! = 1 * 0!
0! = 1

Simplifying,
3! = 3 * 2 * 1 * 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we wanted to implement this behaviour in a function and call it &lt;code&gt;fac&lt;/code&gt;. How would you go about doing that?&lt;/p&gt;

&lt;p&gt;You're probably thinking of loops and if-else statements. But there is a very simple way where we can take the rules of factorial word for word and translate that into code and it would work.&lt;/p&gt;

&lt;p&gt;This is how it goes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fac&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// this is our rule number 1 being satisfied.&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;fac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// rule number 2 being satisfied&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thats it! This is a very simple and functioning implementation of factorial.&lt;/p&gt;

&lt;p&gt;So, how does it work?&lt;/p&gt;

&lt;p&gt;This is an example of a &lt;strong&gt;recursion&lt;/strong&gt;; a function that calls itself to do something or get a result.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;every&lt;/strong&gt; (intensional) &lt;em&gt;recursion&lt;/em&gt; in the world, there are always at least 2 logical cases.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A base case where the function does not call itself (so the recursion is not infinitely spiralling out).&lt;/li&gt;
&lt;li&gt;A recursive case where the function calls itself.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the example above,&lt;br&gt;
Line#2 is our base case. As you might have noticed, this is usually something that's either easily computable or known.&lt;br&gt;
Line#3 is our recursive case, This is usually where we put the repetitive behaviour.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A word of warning,&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;While JavaScript is usually a good enough language for exploring functional concepts, this is one of the places where functional and JS doesn't necessarily agree.&lt;/p&gt;

&lt;p&gt;A recursion is usually fine for simple algorithms that don't need too many recursive calls in JS. But, if your algorithms does a recursive call way too many times, its either not gonna perform well or going to crash because of a stack overflow.&lt;/p&gt;

&lt;p&gt;In future, We might discuss techniques such as memoization to get over this kind of limitations or to make our code more performant than a naive recursion.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Higher-Order Functions
&lt;/h2&gt;

&lt;p&gt;Lets get straight to the point, As previously discussed... A function is a mapping from input to an output.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Higher-Order Function&lt;/strong&gt; is a &lt;em&gt;function&lt;/em&gt; that maps,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From function(s) (&lt;em&gt;input&lt;/em&gt;) to output&lt;/li&gt;
&lt;li&gt;From input to function&lt;/li&gt;
&lt;li&gt;From function (input) to function (output)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Take a moment to absorb all of that. If a function does any of those 3 things, its a higher order function.&lt;/p&gt;

&lt;p&gt;Lets see a few examples,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;propSatisfies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pred&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pred&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isAdult&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;propSatisfies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//=&amp;gt; true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above,&lt;/p&gt;

&lt;p&gt;Our function &lt;code&gt;propSatisfies&lt;/code&gt; takes in 3 parameters(inputs),&lt;br&gt;
&lt;code&gt;prop&lt;/code&gt;: a string (name of a property)&lt;br&gt;
&lt;code&gt;pred&lt;/code&gt;: a function that takes an input and returns true or false&lt;br&gt;
&lt;code&gt;obj&lt;/code&gt;: an object whose &lt;code&gt;prop&lt;/code&gt; property will be checked against &lt;code&gt;pred&lt;/code&gt;.&lt;br&gt;
Our function returns a value, either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On the last line, we call the &lt;code&gt;propSatisfies&lt;/code&gt; function using 3 values,&lt;br&gt;
&lt;code&gt;prop&lt;/code&gt; =&amp;gt; &lt;code&gt;'age'&lt;/code&gt;&lt;br&gt;
&lt;code&gt;pred&lt;/code&gt; =&amp;gt; &lt;code&gt;isAdult&lt;/code&gt;&lt;br&gt;
&lt;code&gt;obj&lt;/code&gt;   =&amp;gt; &lt;code&gt;data&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;isAdult&lt;/code&gt; is a simple function that takes an age and returns &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; (i.e. a predicate). &lt;strong&gt;This is not a Higher-order Function&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;'age'&lt;/code&gt; is a string literal, so not a Higher-order Function&lt;br&gt;
&lt;code&gt;obj&lt;/code&gt; is an object, not a Higher-order Function.&lt;/p&gt;

&lt;p&gt;So, which one is the Higher-order function? &lt;code&gt;propSatisfies&lt;/code&gt;&lt;br&gt;
Why? Because it maps a function (&lt;code&gt;isAdult&lt;/code&gt;) to a value &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lets look at another Higher-order function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;addOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//=&amp;gt; this returns a function&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;addTen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//=&amp;gt; this too returns a function&lt;/span&gt;

&lt;span class="nx"&gt;addTen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//=&amp;gt; returns 11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example,&lt;br&gt;
&lt;code&gt;add&lt;/code&gt; is a function that takes in 1 parameter, &lt;code&gt;a&lt;/code&gt; (A number).&lt;br&gt;
Our function returns another function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;add&lt;/code&gt;, here, is a Higher-order Function, because it returns a function.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;add&lt;/code&gt; function also employs another interesting FP concept called a &lt;em&gt;closure&lt;/em&gt;, we will discuss how our function and closures work another day. For the purposes of this discussion just understand that &lt;code&gt;add&lt;/code&gt; is a Higher-order function because &lt;em&gt;it returns another function&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's look at a final function that does both of these things, take in function(s) &lt;strong&gt;and&lt;/strong&gt; returns a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;double&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dSquare&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//=&amp;gt; a function that doubles and then squares a number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example,&lt;br&gt;
&lt;code&gt;combine&lt;/code&gt; takes in two arguments &lt;code&gt;outer&lt;/code&gt; and &lt;code&gt;inner&lt;/code&gt;, both of which have to be functions. So, we can already see that it is a Higher-order Function&lt;br&gt;
&lt;code&gt;combine&lt;/code&gt; also returns a function that *combine*s the functionality of both inner and outer. Once again, its a higher-order function (because its returning a function)&lt;/p&gt;

&lt;p&gt;When we call combine with &lt;code&gt;square&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt; as arguments, it returns a function that takes an argument &lt;code&gt;arg&lt;/code&gt; and returns a value by calling &lt;code&gt;inner&lt;/code&gt; with &lt;code&gt;arg&lt;/code&gt; and then calling &lt;code&gt;outer&lt;/code&gt; with the return value of the &lt;code&gt;inner&lt;/code&gt; call. In essence, &lt;em&gt;combining&lt;/em&gt; the two functions. So, &lt;code&gt;dSquare&lt;/code&gt; is now a function that, when given a number, doubles it first and then squares it and then returns the result of those operations.&lt;/p&gt;

&lt;p&gt;In later articles, We will discuss a function that's basically &lt;code&gt;combine&lt;/code&gt; but much more flexible and powerful. (For the really curious people, look up &lt;code&gt;compose&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  That's all for today, folks.
&lt;/h3&gt;

&lt;p&gt;Peace ✌️&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/NG6dWJC9wFX2/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/NG6dWJC9wFX2/giphy.gif" alt="https://i.giphy.com/media/NG6dWJC9wFX2/giphy.gif" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>functional</category>
      <category>javascript</category>
      <category>functions</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Introduction. A Fool's Guide To Writing Functional JS (Part 1)</title>
      <dc:creator>Fahad Hossain</dc:creator>
      <pubDate>Fri, 10 Apr 2020 18:57:22 +0000</pubDate>
      <link>https://dev.to/fa7ad/a-fool-s-guide-to-writing-functional-js-part-1-the-introduction-nco</link>
      <guid>https://dev.to/fa7ad/a-fool-s-guide-to-writing-functional-js-part-1-the-introduction-nco</guid>
      <description>&lt;p&gt;You're a developer going about your day writing loops and pushing and deleting stuff from arrays and what not. And then some day you think 🤔&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;maybe I should test my code...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;"Shouldn't be too hard", you say.&lt;/p&gt;

&lt;p&gt;And then you get started, you learn the basics of unit testing and even write some tests. But as soon as your code gets a bit more complex, it starts to seem like almost impossible to test. But, you are determined, you're not gonna give up that easy; you start creating and destroying objects after every test. Maybe your chosen test framework even offers hooks for those.&lt;/p&gt;

&lt;p&gt;But Imagine a world where you could test almost every piece of functionality in your code, without jumping through those hoops. &lt;/p&gt;

&lt;p&gt;Well, that's not what I'm gonna teach you today 😅.&lt;/p&gt;

&lt;p&gt;What we will try to discuss, however, are some of the basics of a paradigm called Functional Programming (or FP for short). What you can eventually get by following the principles of FP is what we previously discussed and more. Namely, easily testable code, minimal setup in testing, cool developer creds, happiness* 😅.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;P.S. I can't promise that last one, what I can promise is good DX (Developer Experience) which may/may not correlate to happiness.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Right, Lets start!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is FP?
&lt;/h2&gt;

&lt;p&gt;I'm not gonna get into some complex mathematical/theoretical explanation of what FP is. I'll try to explain FP in very simple terms...&lt;/p&gt;

&lt;p&gt;FP is a paradigm(a way of thinking about problems) where we solve problems in terms of functions&lt;sup&gt;😉&lt;/sup&gt; and state that does not change(immutable state).&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Foreword&lt;/em&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A word of warning, FP (and almost all literature about FP) is filled with jargon such as "immutable", "functor", "monad", etc. Don't get discouraged, once you get familiar with them, you'll start to see why people use them (hint: to express ideas in short).&lt;/p&gt;

&lt;p&gt;Unlike most traditional FP tutorials, I won't be showing any code in any purely functional language like Haskell or OCaml. While those are great language, I'm not great at them and I think JavaScript is good enough as a language to explore at least the basics of FP 😃&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Core Principles (of FP)
&lt;/h2&gt;

&lt;p&gt;Almost every religion comes with some core tenants that you &lt;strong&gt;must&lt;/strong&gt; follow. While FP is not a religion, it does come with some rules that must be obeyed at all times (otherwise the FP gods will be very unpleased and you will suffer as a developer). But, this being me and not some FP guru, I might miss some and am most definitely paraphrasing all of them.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;NEVER&lt;/strong&gt;* mutate state&lt;/li&gt;
&lt;li&gt;Don't cause side effects&lt;/li&gt;
&lt;li&gt;If you do need have side effects, isolate and limit them to a small number of functions&lt;/li&gt;
&lt;li&gt;Every piece of code that &lt;em&gt;does something&lt;/em&gt; should be a function&lt;/li&gt;
&lt;li&gt;Oh, and did I mention? &lt;strong&gt;NEVER MUTATE STATE&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Don't worry about the jargon for now, we will discuss those terms shortly.&lt;/p&gt;

&lt;h2&gt;
  
  
  State (and mutation? 🤷‍♀️)
&lt;/h2&gt;

&lt;p&gt;Lets begin by examining some objects in real life. Do you remember those digital watches (CASIO et al) from back in the day that lit up when you pressed a button on the side?&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F8%2F89%2FCasio_W-86_digital_watch_electroluminescent_backlight_%2528ii%2529.jpg%2F480px-Casio_W-86_digital_watch_electroluminescent_backlight_%2528ii%2529.jpg" alt="Casio W-86 / W86 / W-86-1VQES "&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Image&lt;/strong&gt;: &lt;em&gt;Casio W-86 / W86 / W-86-1VQES "Illuminator" digital watch&lt;/em&gt;. &lt;a href="https://commons.wikimedia.org/wiki/File:Casio_W-86_digital_watch_electroluminescent_backlight_(ii).jpg" rel="noopener noreferrer"&gt;Multicherry&lt;/a&gt; / &lt;a href="https://creativecommons.org/licenses/by-sa/4.0" rel="noopener noreferrer"&gt;CC BY-SA&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Lets examine that mechanism. The "backlight" on those watches is a very simple green-ish LED soldered besides the digital display, powered by the on-board battery and sometimes a tiny little solar panel. If we think of that backlight and button mechanism as a simple program. We can observe a few things,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The backlight is initially &lt;strong&gt;OFF&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Once you press the button &lt;em&gt;something happens&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Now the backlight is &lt;strong&gt;ON&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this very crude explanation of a backlight, ON and OFF are states. In code, you probably represent that as a variable.&lt;/p&gt;

&lt;p&gt;"But what about this mutation thing", you ask.&lt;/p&gt;

&lt;p&gt;Well, in imperative/OO code, you are probably used to &lt;em&gt;changing&lt;/em&gt; the value of variables. This is what's known as a mutation (i.e. change). Functional Programming &lt;strong&gt;strongly discourages&lt;/strong&gt; mutating state; Instead you create distinct, new states.&lt;/p&gt;

&lt;p&gt;Say you have a list of two numbers (or array or whatever) and now you want to add one more. If your list structure were mutable, you might append another number two it. In immutable land, however, you create a new list containing the items from your old list and one more.&lt;/p&gt;

&lt;p&gt;The cool thing is you don't even need an inherently immutable list structure, you can just use JavaScript's good old Array type. To make your life easier, TC39 people even threw in &lt;strong&gt;Array.prototype.concat&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Mutable code&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;itemList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;mutList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Immutable code&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;twoItemList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;threeItemList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;twoItemList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Immutability might seem a little counter-intuitive at first but it is one of the things that will give you the promised testability. &lt;/p&gt;

&lt;p&gt;Think about it, &lt;em&gt;itemList&lt;/em&gt; might have 2 items at one point of your code and 3 at some other... Your test could be expecting it to have two items but it has three, now you're wondering where in your program life cycle it changed. Maybe you forgot to clear the junk from a previous test? Maybe you changed it on a different method... You have to sit there and figure that out.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;em&gt;twoItemList&lt;/em&gt; always has these two items and never changes, so if you are getting a test failed because your function returned 3 items instead of 2, you know what happened. You returned the wrong list ​😇.​ &lt;/p&gt;

&lt;h2&gt;
  
  
  Side Effects (and pure functions)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Relax, we are not gonna discuss pharmaceuticals.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the core tenants of FP is to try and avoid side-effects.&lt;/p&gt;

&lt;p&gt;"But, what on earth is a side effect?", you ask.&lt;/p&gt;

&lt;p&gt;Glad you asked. A side effect is anything you do that does not involve your own state. Lets get back to this in a bit. But first, functions&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions
&lt;/h3&gt;

&lt;p&gt;"But I already know everything I need to know about functions", you say.&lt;/p&gt;

&lt;p&gt;Do you?&lt;/p&gt;

&lt;p&gt;In FP a function (or rather a pure function) is a piece of code that may/may not take something as input (arguments) and returns something and does nothing more. It does not:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Perform some I/O task&lt;/li&gt;
&lt;li&gt;Change the state of anything outside its scope&lt;/li&gt;
&lt;li&gt;Return different things for the same input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, getting back to out Side Effects discussion, a side effect is any and all of the above discussed despicable things except the last one.&lt;/p&gt;

&lt;p&gt;Or in other words, if a function performs some I/O task instead of or in addition to returning an output, or it changes the global state somehow; it is said to have side effects and is referred to as an impure function*&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A function can also be impure if its output is non-deterministic. That is, gives different output for the exact same input.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  That's all for today
&lt;/h2&gt;

&lt;p&gt;I feel like the above topics are a good enough indication of how things work in FP. We'll get into more details about how to do certain things in the next article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Till then, Peace ✌️&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/NG6dWJC9wFX2/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/NG6dWJC9wFX2/giphy.gif" alt="Deuces"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>functional</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>simple</category>
    </item>
    <item>
      <title>Hello, Blog!</title>
      <dc:creator>Fahad Hossain</dc:creator>
      <pubDate>Sun, 05 Jan 2020 04:55:32 +0000</pubDate>
      <link>https://dev.to/fa7ad/hello-blog-23ik</link>
      <guid>https://dev.to/fa7ad/hello-blog-23ik</guid>
      <description>&lt;p&gt;Once again, I’ve switched blogging platforms.&lt;/p&gt;

&lt;p&gt;I’ve switched between {WordPress, Ghost, Jekyll, Medium, Hexo} and now this is Postach.io (or dev.to)&lt;/p&gt;

&lt;p&gt;So...&lt;/p&gt;

&lt;p&gt;Hello, World!&lt;/p&gt;

&lt;p&gt;Lets try to make a bunch of "Hello, world" programs in as many languages as I can 🙃&lt;/p&gt;

&lt;p&gt;Let’s start with my favourite language, JavaScript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Hello, world")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.write('Hello, world')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Onto the next few language&lt;/p&gt;

&lt;p&gt;C&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;





int main() {

 printf("%s", "Hello, world");

 return 0;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C++&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;

int main() {

 std::cout &amp;lt;&amp;lt; "Hello, world" &amp;lt;&amp;lt; std::endl;

 return 0;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import "fmt"





func main() {

 fmt.Println("hello world")

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to some concise syntax&lt;/p&gt;

&lt;p&gt;Python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Hello, world")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Haskell&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main::IO()

main = putStrLn "Hello, world"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ReasonML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Js.log("Hello, world");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the piece de resistance,&lt;/p&gt;

&lt;p&gt;My least favourite ones&lt;/p&gt;

&lt;p&gt;Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class HelloWorld {

 public static void main (String[] args) {

 System.out.println("Hello, world");

 }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C#&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System;



namespace HelloWorld {

 class Program {

 static void Main(string[] args) {

 Console.WriteLine("Hello, world");

 }

 }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PHP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

echo "Hello, world";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s about all I could think of. This post is nothing but a test to see what a blog post looks like.&lt;/p&gt;

&lt;p&gt;Peace ✌️&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P1NscyiM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images.postach.io/bd60774b-4032-4847-980a-8fb4b77bad54/eff98a79-f6ea-46d2-8a0d-2bc05d0afae8/51a24f2a-6a3a-44f8-b6e9-547f6c0862b4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P1NscyiM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images.postach.io/bd60774b-4032-4847-980a-8fb4b77bad54/eff98a79-f6ea-46d2-8a0d-2bc05d0afae8/51a24f2a-6a3a-44f8-b6e9-547f6c0862b4.gif" alt="" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testpost</category>
      <category>helloworld</category>
    </item>
  </channel>
</rss>
