<?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: Soumak Dutta</title>
    <description>The latest articles on DEV Community by Soumak Dutta (@arghasoumak).</description>
    <link>https://dev.to/arghasoumak</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%2F107477%2Fe2c123b3-0002-4ca3-9a5a-fea2ef670099.jpg</url>
      <title>DEV Community: Soumak Dutta</title>
      <link>https://dev.to/arghasoumak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arghasoumak"/>
    <language>en</language>
    <item>
      <title>Understanding higher-order functions with map &amp; reduce in JavaScript</title>
      <dc:creator>Soumak Dutta</dc:creator>
      <pubDate>Tue, 12 Feb 2019 12:08:34 +0000</pubDate>
      <link>https://dev.to/arghasoumak/understanding-higher-order-functions-with-map--reduce-in-javascript-3kmk</link>
      <guid>https://dev.to/arghasoumak/understanding-higher-order-functions-with-map--reduce-in-javascript-3kmk</guid>
      <description>&lt;h1&gt;
  
  
  Higher Order Functions
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkbhojsffbglub5o38s92.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkbhojsffbglub5o38s92.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Higher order functions are a hot topic in functional programming. It's used by most of us without knowing it. So in this article, I will clear all confusion about higher-order functions.&lt;/p&gt;

&lt;p&gt;In JavaScript functions are treated as objects. They can be assigned to a variable, passed as a parameter and returned from a function. So we called them first-class objects.&lt;/p&gt;

&lt;p&gt;So, A higher-order functions are functions that received functions as a parameter or return functions as output. &lt;/p&gt;

&lt;p&gt;That means we can pass a function as a parameter to another function. Like..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const foo = call =&amp;gt; call();

const sayHi = () =&amp;gt; "hi";

foo(sayHi)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we pass sayHi function to foo function as an argument. This approach is also called callback.&lt;/p&gt;

&lt;p&gt;Also we can return function as output. Like..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const foo = () =&amp;gt; () =&amp;gt; "Hi";

foo()()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we return an anonymous function from function foo.&lt;/p&gt;

&lt;p&gt;Now we have understood higher-order functions, we could learn some higher order functions built into Javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;p&gt;The map is an higher order array function. It takes a callback function and the function will called for each element in the array. It will return an entirely new array.&lt;/p&gt;

&lt;p&gt;Syntax of map function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.map((current, index, array) =&amp;gt; {})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, 3, 4, 5];

const newArr = arr.map(num =&amp;gt; num * 2);

newArr  // 2, 4, 6, 8, 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we call the function for each number and return the number multiplying 2 with it.&lt;/p&gt;

&lt;p&gt;Note: Map doesn't change the actual array. Instead it creates a new array with modified values.    &lt;/p&gt;

&lt;h2&gt;
  
  
  Reduce
&lt;/h2&gt;

&lt;p&gt;Reduce is also a higher order function. But unlike map it call the callback for each element and return a single reduced value. &lt;/p&gt;

&lt;p&gt;Syntax of reduce function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.reduce((accumulator, current, index, array) =&amp;gt; {}, initialValue)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, 3, 4, 5];

const sum = arr.reduce((total, num) =&amp;gt; total += num, 0);

sum  // 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we find the sum in a list of numbers. Sum is the accumulator. We initialize it with 0 and return the sum. &lt;/p&gt;

&lt;p&gt;We can make use of the accumulator to calculate any type of value in an array.&lt;/p&gt;

&lt;p&gt;We can calculate the minimum number from a list. Like..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const are = [5,7,3,9,6,3];

const minimum = arr.reduce((min,num) =&amp;gt; min &amp;lt; num ? min : num)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, use map if you want to modify each element in an array. And use reduce when you want to reduce the array to a single value.&lt;/p&gt;

&lt;p&gt;I hope you learn something new in this article. Thanks for reading.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding Arrow functions in JavaScript</title>
      <dc:creator>Soumak Dutta</dc:creator>
      <pubDate>Fri, 08 Feb 2019 05:49:46 +0000</pubDate>
      <link>https://dev.to/arghasoumak/arrow-functions-in-javascript-19n7</link>
      <guid>https://dev.to/arghasoumak/arrow-functions-in-javascript-19n7</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fer6hpt4l34quc2ktxkkt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fer6hpt4l34quc2ktxkkt.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Arrow functions were introduced in es6 or es2015, since then it's very popular among developers. And it's the coolest thing that I love the most.&lt;/p&gt;

&lt;p&gt;Arrow functions changed the way we declare a function. It's made functions declaration easy, fast and stylish.&lt;/p&gt;

&lt;p&gt;The arrow in it's name come from it's arrow like syntax. Let's take an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const foo = () =&amp;gt; console.log('Hey, Arrow!');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to use it
&lt;/h2&gt;

&lt;p&gt;Arrow functions works very similar to anonymous functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Anonymous functions
const foo = function () {
    console.log("I'm old!");
}

//Arrow functions
const foo = () =&amp;gt; {
    console.log("I'm new!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But arrow functions provide more flexibility. &lt;br&gt;
Like if you have only one statement in function body then you can remove those curly braces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const foo = () =&amp;gt; console.log("Isn't it cool!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also if you returning some value you don't need to provide the return kayword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const foo = () =&amp;gt; "Wow!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if you need to return an object you would do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const msg = () =&amp;gt; ({
    type: 'msg',
    body: 'Hey'
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, You can pass multiple parameters to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = (a, b) =&amp;gt; a + b;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And last but not the least, if you have only one parameter you also can remove those Parenthesis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const product = a =&amp;gt; a * a;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now look how easy it is to declare a function using arrow functions.&lt;/p&gt;

&lt;p&gt;I hope you learn something from this article. Thanks for reading..&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
