<?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: Mahmoud Hassan</title>
    <description>The latest articles on DEV Community by Mahmoud Hassan (@trezeguit).</description>
    <link>https://dev.to/trezeguit</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%2F464643%2Fe8163552-4523-4469-9cc7-90dad8a18fa5.jpeg</url>
      <title>DEV Community: Mahmoud Hassan</title>
      <link>https://dev.to/trezeguit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/trezeguit"/>
    <language>en</language>
    <item>
      <title>Functional Programming with JS</title>
      <dc:creator>Mahmoud Hassan</dc:creator>
      <pubDate>Mon, 18 Oct 2021 19:26:17 +0000</pubDate>
      <link>https://dev.to/trezeguit/functional-programming-with-js-1bgd</link>
      <guid>https://dev.to/trezeguit/functional-programming-with-js-1bgd</guid>
      <description>&lt;p&gt;&lt;strong&gt;Functional programming&lt;/strong&gt; is a programming paradigm designed to handle pure mathematical functions. This paradigm is totally focused on writing more compounded and pure functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functional programming&lt;/strong&gt; is a particular kind of &lt;strong&gt;declarative programming&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;First, you need to know the difference between declarative programming and imperative programming, they are js paradigms or techniques to organize your code.&lt;br&gt;
In imperative programming, we specify the program logic describing the flow control.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "Ali";
let Greeting = "Hi, ";
console.log(Greeting , name);  // Hi,  Ali
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the opposite, declarative programming we specify the program logic without describing the flow control&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Greeting = (name) =&amp;gt; {
    console.log('Hi, ',name);
}

Greeting("Ali"); // Hi, Ali
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, as you have noticed, the &lt;strong&gt;functional programming&lt;/strong&gt; focuses on the code being clean, organized, and reused through&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pure Functions&lt;/strong&gt;: 
  are simple and reusable. They are completely independent of the outside state(global variables), easy to refactor, test and debug.
A pure function is a function which given the same input, will always return the same output.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = (x,y) =&amp;gt; x+y;
add(5,4) // 9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Math.random&lt;/code&gt; is a popular example of not pure function.&lt;br&gt;
another example for not pure function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let count = 0;
const incCount = (value) =&amp;gt; count += value;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Higher-Order Functions&lt;/strong&gt;: they can receive a function as a parameter(callback) and also can return a function, they are very helpful for writing complex functions.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const animals = ["Cat", "Dog", "Elephant",
 "Giraffe", "Lion", "Monkey"];
const zooWithoutCat = animals.filter(animal =&amp;gt; animal !== "Cat");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note ==&amp;gt; Don't Iterate&lt;/strong&gt; you can use higher-order functions like map, filter, reduce, find...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [2, 4, 6];
let numbersX2 = numbers.map(number =&amp;gt; number*2); // [ 4, 8, 12 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Avoid Mutability&lt;/strong&gt;: you must avoid changing the data.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num1 = [1, 2, 3];
let num2 = num1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;any change in num2 affects num1 (mutability), we can fix this problem by using higher-order functions or spread operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num2 = [...num1];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Persistent Data Structures for Efficient Immutability&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of all data as immutable, never changing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;the problem with immutability is that you need to copy all data for a little change and this can give you efficiency problems, because you will use a lot of space, so &lt;strong&gt;What is the solution?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Don't Worry&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
there are many of js libraries that handle this problem like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mori&lt;/li&gt;
&lt;li&gt;Immutable.js&lt;/li&gt;
&lt;li&gt;Underscore&lt;/li&gt;
&lt;li&gt;Lodash&lt;/li&gt;
&lt;li&gt;Ramda
they depend on structural sharing idea 
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdu4myz2e9lpubaoxljlp.jpg" alt="Image description"&gt;
Note that the yellow squares are shared between 2 variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Thanks for your time&lt;/strong&gt;&lt;br&gt;
you can add me on LinkedIn: &lt;a href="https://www.linkedin.com/in/mahmoudhassan7764" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
