<?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: akhilmdev</title>
    <description>The latest articles on DEV Community by akhilmdev (@akhilmdev).</description>
    <link>https://dev.to/akhilmdev</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%2F377253%2F2509ba7e-69be-45b5-a158-3dfe7a7a92e6.jpg</url>
      <title>DEV Community: akhilmdev</title>
      <link>https://dev.to/akhilmdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akhilmdev"/>
    <language>en</language>
    <item>
      <title>Two Sum</title>
      <dc:creator>akhilmdev</dc:creator>
      <pubDate>Sat, 04 Sep 2021 12:42:22 +0000</pubDate>
      <link>https://dev.to/akhilmdev/two-sum-eaf</link>
      <guid>https://dev.to/akhilmdev/two-sum-eaf</guid>
      <description>&lt;h2&gt;
  
  
  Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You may assume that each input would have exactly one solution, and you may not use the same element twice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example 1:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Input: nums = [2,7,11,15], target = 9&lt;br&gt;
Output: [0,1]&lt;br&gt;
Output: Because nums[0] + nums[1] == 9, we return [0, 1].&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Example 2:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Input: nums = [3,2,4], target = 6&lt;br&gt;
Output: [1,2]&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Example 3:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Input: nums = [3,3], target = 6&lt;br&gt;
Output: [0,1]&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Analysis
&lt;/h3&gt;

&lt;p&gt;So, we need to have 2 numbers for the array so that the sum of those equal to targeted sum. feel's simple :-)...&lt;br&gt;
But, &lt;strong&gt;Not sorted&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First think that comes to mind is to take each element and find the target complement of element form remaining elements.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So this will take O(n^2) and space complicity of O(1)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Can we have better time complicity ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ya!! we do have!&lt;/p&gt;

&lt;p&gt;We can keep a hash that holds target complement(target - currentValue) as key and index as value and if we find element in hash map then return the current index and index from hash map.&lt;/p&gt;

&lt;p&gt;Lets code that!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var twoSum = function (nums, target) {
    result = [];
    const index_map = new Map();
    // Loop for each element in the array
    for (let i = 0; i &amp;lt; nums.length; i++) {
        let difference = target - nums[i];
        if (index_map.has(difference)) {
            result.push(i);
            result.push(index_map.get(difference));
            break;
        } else {
            index_map.set(nums[i], i);
        }
    }
    return result;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;So, Here the time complicity is O(n) and space complicity of O(n).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The best time complicity algorithm is to use the hash map, but that could take O(n) space complicity.&lt;/p&gt;

&lt;p&gt;Always think of which is the requirement is it time or space and come up with the algorithm.&lt;/p&gt;

&lt;p&gt;Thanks feel free to contact and keep learning!&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>twosum</category>
      <category>algorithms</category>
      <category>datastructure</category>
    </item>
    <item>
      <title>Method chaining &amp; create a custom method chaining!</title>
      <dc:creator>akhilmdev</dc:creator>
      <pubDate>Fri, 04 Jun 2021 16:28:01 +0000</pubDate>
      <link>https://dev.to/akhilmdev/method-chaining-create-a-custom-method-chaining-48c5</link>
      <guid>https://dev.to/akhilmdev/method-chaining-create-a-custom-method-chaining-48c5</guid>
      <description>&lt;h2&gt;
  
  
  Function Chainging
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    A common syntax for invoking mutiple method calls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;em&gt;What ?&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Function Chaining&lt;/strong&gt; is a pattern in JavaScript where &lt;strong&gt;multiple functions&lt;/strong&gt; are called on the &lt;strong&gt;same object consecutively.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;We can understand the above definition with examples below. &lt;/p&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1
&lt;/h4&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;totalExpensesInINR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tripExpenses&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;expense&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;paid&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;expense&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currency&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;USD&lt;/span&gt;&lt;span class="dl"&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;expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                                &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;amount&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="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;amountA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amountB&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;amountA&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;amountB&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the tripExpenses is an array of objects(&lt;em&gt;expense&lt;/em&gt;), thus we can apply &lt;strong&gt;filter&lt;/strong&gt; which is an array method and it returns array (&lt;em&gt;find more about &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"&gt;filter&lt;/a&gt;&lt;/em&gt;). Since filter return an array again we can call &lt;strong&gt;map&lt;/strong&gt; which is an array method(&lt;em&gt;find more about &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;map&lt;/a&gt;&lt;/em&gt;) and so on the get the total of the expense in INR.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here we call &lt;strong&gt;filer&lt;/strong&gt;, &lt;strong&gt;map&lt;/strong&gt; and &lt;strong&gt;reduce&lt;/strong&gt; (&lt;em&gt;multiple methods&lt;/em&gt;) on &lt;strong&gt;tripExpenses&lt;/strong&gt; (&lt;em&gt;same object&lt;/em&gt;) &lt;strong&gt;one after the other&lt;/strong&gt; (&lt;em&gt;consecutively&lt;/em&gt;) to get the require results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  2
&lt;/h4&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&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;number&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;number&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="c1"&gt;// [4, 6, 10, 20]&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;number&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;number&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [6, 10, 20]&lt;/span&gt;
                    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// '6010020'&lt;/span&gt;
                    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ['6', '0', '1', '0', '0', '2', '0']&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;ele&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ele&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;//[6, 0, 1, 0, 0, 2, 0]&lt;/span&gt;

&lt;span class="c1"&gt;// The object that return on previous method or initial object&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;If you check the above examples we can see that after the &lt;strong&gt;join method&lt;/strong&gt; (an array method which returns string). Since the previous method return a string, we can only use a string method to chain further. So we can rewrite the definition of method chaining to&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Function Chaining&lt;/strong&gt; is a pattern in JavaScript where multiple functions are called on the &lt;strong&gt;inital object for first method and object that returns on previous method for conecutive methods&lt;/strong&gt; consecutively. &lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Why ?&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. Large number of parameters.
&lt;/h2&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;maxParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;param2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;param3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...,&lt;/span&gt; &lt;span class="nx"&gt;paramN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// have some complex logic with params&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// convert to mathod chaining&lt;/span&gt;

&lt;span class="nx"&gt;maxParamsObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param2&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="nx"&gt;processM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paramN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Reduce to small and reusable unit(function).
&lt;/h2&gt;

&lt;h2&gt;
  
  
  3. Avoid intermidate variables.
&lt;/h2&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;persons&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="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="s1"&gt;Akhil&lt;/span&gt;&lt;span class="dl"&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;29&lt;/span&gt;&lt;span class="p"&gt;},&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="s1"&gt;Nikhil&lt;/span&gt;&lt;span class="dl"&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;32&lt;/span&gt;&lt;span class="p"&gt;},&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="s1"&gt;Deepak&lt;/span&gt;&lt;span class="dl"&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="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;personsAgeGraterThan25&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;persons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&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;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;personNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;personsAgeGraterThan25&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;person&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;person&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;personNameStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;personNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// This can be done without intermidate variables&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;personNameStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&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;25&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;person&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;person&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;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;,&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;h2&gt;
  
  
  4. Readable and maintainable code.
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Why Not ?&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. Confuses the signature.
&lt;/h2&gt;

&lt;p&gt;If a method used in chaining return another object, That make confusion and hard to identify as the code is abstracted.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Hard to debug.
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;How ?&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/nervous-rosalind-zoz69?initialpath=/src/service.js"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here we created a Recipe class where its has methods like &lt;strong&gt;sliceVegitables&lt;/strong&gt;, &lt;strong&gt;addIngredient&lt;/strong&gt;, &lt;strong&gt;boil&lt;/strong&gt; etc... which return &lt;strong&gt;this&lt;/strong&gt; (which is the instance of class itself). Thats why we can call the function with in the class as chain. &lt;br&gt;
But in the case of &lt;strong&gt;serve&lt;/strong&gt; method we are not return anything, So we won't be able to call any Recipe methods after the serve method is executed(we can call only the methods of undefined type).&lt;/p&gt;

&lt;h2&gt;
  
  
  Concusions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Function Chaining is a pattern in JavaScript where multiple functions are called on the inital object for first method and object that returns on previous method for conecutive methods consecutively.&lt;/li&gt;
&lt;li&gt;For a custom Method chaining, we always return this from methods, so that we can chain the methods of same class.(If we return anything other than this, then we can only chain with the methods of corresponding type).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;keep learning.&lt;/p&gt;

&lt;h1&gt;
  
  
  Thanks!
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>@Attribute Decorator in Angular</title>
      <dc:creator>akhilmdev</dc:creator>
      <pubDate>Fri, 01 May 2020 07:49:32 +0000</pubDate>
      <link>https://dev.to/akhilmdev/attribute-decorator-in-angular-b3j</link>
      <guid>https://dev.to/akhilmdev/attribute-decorator-in-angular-b3j</guid>
      <description>&lt;p&gt;The &lt;code&gt;@Attribute&lt;/code&gt; decorator returns the value of the specified attribute that attached to the selector.In some cases, This can bring a performance boost to the application.&lt;/p&gt;

&lt;p&gt;Suppose we are having a static value, that we need to child component, we mite use &lt;code&gt;@input&lt;/code&gt;. By doing this we are attaching some event and on every change detection cycle it will check for changes.This can affect the performance as this is static value.&lt;/p&gt;

&lt;p&gt;For solving this issue we can use @Attribute decorator which will take value only once.Keep in mind that we won't able to pass variable, it should be passed as string.&lt;/p&gt;

&lt;p&gt;We will inject this decorator as in the code below in the child component. &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/attribute-decorator-kitoo?module=/src/app/hello.components.ts"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;With this changes, Angular will evaluate it once.&lt;/p&gt;

&lt;p&gt;So this is what I understandings about @Attribute decorators. If anyone else have any extra information you can comment. &lt;/p&gt;

&lt;p&gt;keep learning.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>attribute</category>
      <category>decorators</category>
    </item>
  </channel>
</rss>
