<?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: Rahul Ranjan</title>
    <description>The latest articles on DEV Community by Rahul Ranjan (@ambitiousrahul).</description>
    <link>https://dev.to/ambitiousrahul</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%2F782978%2F87697294-1d31-4f9a-aab0-4df1c4eea784.png</url>
      <title>DEV Community: Rahul Ranjan</title>
      <link>https://dev.to/ambitiousrahul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ambitiousrahul"/>
    <language>en</language>
    <item>
      <title>Function Composition In JavaScript with multiple parameters</title>
      <dc:creator>Rahul Ranjan</dc:creator>
      <pubDate>Sat, 07 May 2022 15:08:39 +0000</pubDate>
      <link>https://dev.to/ambitiousrahul/function-composition-in-javascript-with-multiple-parameters-57e</link>
      <guid>https://dev.to/ambitiousrahul/function-composition-in-javascript-with-multiple-parameters-57e</guid>
      <description>&lt;p&gt;Function composition in JavaScript is process of combining several function calls into one function.&lt;/p&gt;

&lt;p&gt;Ways in which function composition can be done&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating a high-order function and pass the necessary functions as parameters in this method.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;reduce()&lt;/code&gt; or &lt;code&gt;reduceright()&lt;/code&gt; function and pass all the independent functions that needs to be combined.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see how can this be done&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach 1 : Using higher order functions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;UseCase:&lt;/strong&gt; If we have a scenario where we need to print details of employee on some kind of employee card for each employee and we have three independent functions.&lt;br&gt;
Code for the example is written below&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;// function1 to print employee id&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;printId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;employeeId&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Printing| EmployeeId:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;employeeId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//function2 to print employee name&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Printing| Name:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//function3 to print email and designation among other info.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;printOther&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;designation&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Printing| Email: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ,Designation: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;designation&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="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//function 4 is an higher order function that runs the functions being passed into it&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;print&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;funcA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;funcB&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;param_ToFunctionB&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;funcA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;funcB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param_ToFunctionB&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;


    &lt;span class="c1"&gt;//printing the combination of above js functions&lt;/span&gt;
    &lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;printId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1234&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;printname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rahul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;printOther&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rahul@web.com&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;software engineer&lt;/span&gt;&lt;span class="dl"&gt;"&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="c1"&gt;//output&lt;/span&gt;
    &lt;span class="nx"&gt;Printing&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;EmployeeId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1234&lt;/span&gt;
    &lt;span class="nx"&gt;Printing&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Rahul&lt;/span&gt;
    &lt;span class="nx"&gt;Printing&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;rahul&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;com&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;Designation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;software&lt;/span&gt; &lt;span class="nx"&gt;engineer&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;the &lt;code&gt;print(funcA, funcB)&lt;/code&gt; function takes a bigger form .i.e. more lines of code when we need to add more functions to the composition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach 2: Using reduce() or reduceRight() functions for function composition
&lt;/h3&gt;

&lt;p&gt;For same use case as above , second way of doing function composition in JavaScript is by using &lt;code&gt;reduce()&lt;/code&gt; or &lt;code&gt;reduceRight()&lt;/code&gt; functions.&lt;/p&gt;

&lt;p&gt;JavaScript &lt;code&gt;reduceRight()&lt;/code&gt; function runs a function on each element of the array to reduce it into a single value. Also , &lt;code&gt;reduceRight()&lt;/code&gt; function executes elements from the right, whereas reduce() method executes elements from left.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is noteworthy that functions objects being passed to the &lt;code&gt;reduce()&lt;/code&gt; or &lt;code&gt;reduceRight()&lt;/code&gt; function should have same number of arguments in each of them, and it should also return one value &lt;br&gt;
So, any function can be made composable by converting that function into a curry function&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Currying&lt;/em&gt; is the technique in functional way of programming of transforming a function taking multiple input arguments (two, three or more- polyadic) into set of internally related functions by using closures, where each function is single input, single output function, and final output returned from the outermost function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Simply written, &lt;em&gt;currying&lt;/em&gt; is the way of reshaping the function type &lt;code&gt;f(a, b, c)&lt;/code&gt; to &lt;code&gt;f(a)(b)(c)&lt;/code&gt; .&lt;br&gt;
    e.g.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="c1"&gt;//normal function call&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;departmentEmployees&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;departmentName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;empId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;empName&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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;departmentName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; Info| &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;empId&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;empName&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;//transformed carried function&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;departmentCarryTransformed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;departmentName&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;empId&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;empName&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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;departmentName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; Info | &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;empId&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;empName&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;//carry function benefit&lt;/span&gt;
    &lt;span class="c1"&gt;//we can create single global variable for a department and use it elsewhere across the file or the page&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;accountsDepartment&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;departmentCarryTransformed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Accounts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;accountsDepartment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bob&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;accountsDepartment&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;So, functions in this case&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;// function1 to print employee id&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;printId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attributes&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Printing| EmployeeId:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;employeeId&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;attributes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="c1"&gt;//function2 to print employee name&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printname&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Printing| Name:&lt;/span&gt;&lt;span class="dl"&gt;"&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="nx"&gt;name&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;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


    &lt;span class="c1"&gt;//function3 to print email and designation among other info.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;printOther&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attributes&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;`Printing| Email: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ,Designation: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;designation&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="p"&gt;};&lt;/span&gt;


    &lt;span class="c1"&gt;// TO make the function composition easier, we can use rest parameters to pass indefinite number of arguments to the function&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;smarterPrint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;functions&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;params&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;functions&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="nx"&gt;p&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="o"&gt;=&amp;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;p&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


    &lt;span class="c1"&gt;// parameters that we need to pass to different functions to print specific info&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fn_params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;employeeId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1234&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rahul ranjan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rahul@web.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;designation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;software engineer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="c1"&gt;// this function composition way takes lesser code of lines.&lt;/span&gt;
    &lt;span class="nx"&gt;smarterPrint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;printOther&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;printname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;printId&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;fn_params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// second way&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;smarterPrintOtherWay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paramsObj&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;printOther&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;printname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;printId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paramsObj&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

    &lt;span class="nx"&gt;smarterPrintOtherWay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn_params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;//output is same &lt;/span&gt;
    &lt;span class="nx"&gt;Printing&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;EmployeeId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1234&lt;/span&gt;
    &lt;span class="nx"&gt;Printing&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Rahul&lt;/span&gt;
    &lt;span class="nx"&gt;Printing&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;rahul&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;com&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;Designation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;software&lt;/span&gt; &lt;span class="nx"&gt;engineer&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;The benefit of this approach for function composition is visually seen as our code base increases.&lt;/p&gt;

&lt;p&gt;References&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://javascript.info/currying-partials"&gt;https://javascript.info/currying-partials&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jrsinclair.com/articles/2022/javascript-function-composition-whats-the-big-deal/"&gt;https://jrsinclair.com/articles/2022/javascript-function-composition-whats-the-big-deal/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/hackernoon/javascript-functional-composition-for-every-day-use-22421ef65a10"&gt;https://medium.com/hackernoon/javascript-functional-composition-for-every-day-use-22421ef65a10&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>functional</category>
    </item>
    <item>
      <title>Artificial Neural Network Structure Interpretability Notes</title>
      <dc:creator>Rahul Ranjan</dc:creator>
      <pubDate>Mon, 25 Apr 2022 01:45:43 +0000</pubDate>
      <link>https://dev.to/ambitiousrahul/neural-network-basic-structure-interpretability-notes-1i7k</link>
      <guid>https://dev.to/ambitiousrahul/neural-network-basic-structure-interpretability-notes-1i7k</guid>
      <description>&lt;p&gt;Neural Network has been a raze in machine learning these days , but now there is a growing sense that neural networks output needs to be more interpretable to the human because with interpretability of the results given by the model, trust will increase.&lt;/p&gt;

&lt;p&gt;So, I am sharing my notes which I had made in order to understand some basic terms used in ANN, understanding of which is critical for getting intuitive absorption of any variant of a general neural network  architecture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. What are neurons?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Neuron is the building block/node of a neural network .It is basically the transformed data after applying the filter. Each neuron contains set of data inputs, weights and a bias value. So, if a filter(3,3,3) applied on the input array , that means that neuron(filter) has 27 trainable weights. And also each neuron(filter) only looks over a particular region in the input called as receptive field. A neuron can , hence ,consist multiple inputs.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;2. What are weights and bias?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Weights are the parameter of the neural network that leverages the degree of importance by which transformation of the input data within the hidden layer shall take place. Bias is also a learnable parameter of neural network. Bias, essentially represents how far off are the prediction from their intended value. Each interconnection between two neurons of different layer has a weight and bias associated with it.  When the model training starts, weights and bias for the network's first hidden layer are randomly taken.&lt;/p&gt;

&lt;p&gt;Weights are then assigned , first by feedforward approach till the output layer is reached, and then are often tweaked/improved backwards usually through backpropagation technique (&lt;em&gt;other approaches also exists like swarm intelligence&lt;/em&gt;) till input layer , so as to produce the desired output.&lt;/p&gt;

&lt;p&gt;Weights for a single neuron have shape as [width,height,input_ channels]&lt;/p&gt;

&lt;p&gt;As weights are learnt during training through backpropagation usually(other algorithms are also there like swarm intelligence), so this basically means applied filters impact are learnt during backpropagation.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;3. What is activation function?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Activation Function is the function that determines the way in which sum of weights will be transformed to the output. &lt;/p&gt;

&lt;p&gt;It also explores the functional sub-space of the NN model training data inputs to learn to represent weights. AFs define the output of a neuron and introduces non-linearities in the network. A neural network without AF will be like a linear regression model, so cannot perform any complex modelling.&lt;/p&gt;

&lt;p&gt;These functions outputs the combinations of neurons called as activations, that fire up in response to a particular input.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;4. Parametric and Non-parametric ML Activation Functions?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Parametric ML algorithms assumes some fixed function shape(with parameters) based on which model is trained. Although the number of parameters in a parametric AF is fixed, but it can be trainable/adaptive. So , all traditional AFs such as sigmoid, tanh, and ReLU are fixed or parametric activation functions. &lt;br&gt;
Few Adaptive AF which has fixed number of trainable parameters also comes as fixed or parametric AF. e.g. Swish, AReLU, Mish, PReLU etc&lt;/p&gt;

&lt;p&gt;Non-parametric Activation functions are the one in which neither the number of parametric is fixed , not their values.  i.e. non-parametric  AF has to be adaptive , but every adaptive activation function need not be non-parametric AF&lt;/p&gt;

&lt;p&gt;Three Approaches for implementing NP-AFs are &lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Adaptive piecewise linear method (APL)&lt;/li&gt;
&lt;li&gt;Spline AFs&lt;/li&gt;
&lt;li&gt;Maxout network based AFs&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;5. What are optimizers?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An optimizer is a concept that works over the loss function of the neural network model to determine how weights will be updated. Usually backpropagation algorithm is the core technique of most of the optimizers.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;6. What are layers in neural network?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Layers are the components of a neural network through which the network learns the training dataset by progressively blending the data, transforming and wrapping it up into a form (called as &lt;br&gt;
&lt;em&gt;&lt;code&gt;representations&lt;/code&gt;&lt;/em&gt;) that is easy to understand by computers, to solve the task. Each representation can be thought as a type. Each layer is just technically a function wrapped over activation function on the inputs of that layer which is basically the output of the previous layer. Each layer has to discover a new representation of the input. This newly learnt or outputted representation can be seen as three-dimensional cube , where each cell in the cube is an activation(output from the AF, or the amount a neuron fires).&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--plK_4zvP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m07fgvuy648cttlv7cid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--plK_4zvP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m07fgvuy648cttlv7cid.png" alt="Image description" width="505" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, neural network as a whole, is just chain of composed functions. This composed network is then, optimized to perform a task.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Making sense of the activations returned from the hidden layer is hard because we usually work with them as abstract numerical vectors. However, with feature visualization techniques , we can transform this abstract vector into a more meaningful semantic dictionary.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;7. What are feature maps?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Collection of multiple filters(neurons) which looks at different region of the input image with the same weights , but to extract the same feature( but from different regions of the input).&lt;/p&gt;

&lt;p&gt;So, technically feature maps has number of same filters applied on the input. e.g. feature map dimensions:- 64(3,3,3) which means filter size (3, 3, 3), applied 64 times&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;8. What are pooling layer in CNN , and its uses?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pooling layer aggregates the features learnt after applying feature maps over input array. Looks at the larger region of the image and captures an aggregate statistics(max, average, etc) of each region. This helps in identifying if the learnt features are the one that we were interested or not, and also makes the network invariant to the local transformations. &lt;/p&gt;

&lt;p&gt;Most commonly used Pooling layer Types &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;a. Max pooling&lt;br&gt;
b. Average pooling &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Pooling operates on each feature map independently . It reduces the size(width and height) of each feature map, but number of feature maps remains constant.&lt;/p&gt;

&lt;p&gt;By doing so , it makes the representation more compact by reducing the spatial size, (potential disadvantage)- can cause information loss at other end.&lt;/p&gt;

&lt;p&gt;Deep CNNs used without pooling layers are called &lt;em&gt;&lt;code&gt;capsule networks&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;


&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;9. Which layer contains the trainable parameters in the neural network model and which  does not?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Convolution layer and fully connected layers contain trainable parameters , pooling layer does not.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;10. What is neural network training like and what are its objectives?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The training of neural network is actually an non-convex optimization problem in which the objective is to find  the optimum weights parameters(which minimizes the total loss or error), which are searched and found out through backpropagation algorithm. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;► Overall Steps in a broader sense&lt;/p&gt;

&lt;p&gt;a. Firstly , data inputs is transformed into representations that computer can understand though activation functions, these activations holds the initialized weights (through feedforward).&lt;/p&gt;

&lt;p&gt;b. Now, a convex loss function for the linear weighted combinations of those activation(outputs of AFs wrapped in a hidden layer) applied to an input is defined to find the optimum weight needed to minimize the loss in solving the problem.&lt;/p&gt;

&lt;p&gt;c. Lastly, the optimum weights are searched iteratively during training process by adapting suitable optimisation strategies based on backpropagation usually for the weight parameters by minimizing the loss function in the functional sub-space.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://deepai.org/definitions"&gt;https://deepai.org/definitions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://colah.github.io"&gt;http://colah.github.io&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ieeexplore.ieee.org/document/9675813"&gt;https://ieeexplore.ieee.org/document/9675813&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://distill.pub/2018/building-blocks/"&gt;https://distill.pub/2018/building-blocks/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>deeplearning</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
