<?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: Saba Shavidze</title>
    <description>The latest articles on DEV Community by Saba Shavidze (@shavidze).</description>
    <link>https://dev.to/shavidze</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%2F153323%2F7f69bb86-a609-4539-9413-1daf669b9a77.jpg</url>
      <title>DEV Community: Saba Shavidze</title>
      <link>https://dev.to/shavidze</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shavidze"/>
    <language>en</language>
    <item>
      <title>Functional Programming - Javascript</title>
      <dc:creator>Saba Shavidze</dc:creator>
      <pubDate>Sat, 24 Dec 2022 17:22:18 +0000</pubDate>
      <link>https://dev.to/shavidze/functional-programming-javascript-3daa</link>
      <guid>https://dev.to/shavidze/functional-programming-javascript-3daa</guid>
      <description>&lt;p&gt;Today I want to talk about functional programming, which has become very popular for JavaScript developers.&lt;br&gt;
Simply put, functional programming is a paradigm where applications are composed using pure functions,&lt;br&gt;
avoiding shared mutable state and side-effects. JavaScript has the most important features needed for functional programming:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First class functions: The ability to use functions as data values: pass functions as arguments, return functions, and assign functions to variables and object properties.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Anonymous functions and concise lambda syntax: x =&amp;gt; x + 3 is a valid function expression in JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Closures: When a function is defined inside another function, it has access to the variable bindings in the outer function, even after the outer function exits.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three listed properties come from lambda calculus, which we will touch on in the next post, how it started and where the idea of lambda calculus came from. Functional programming is entirely based on the lambda calculus.&lt;/p&gt;

&lt;p&gt;Functional programming is usually more declarative than imperative, meaning that we express what to do rather than how to do it.&lt;br&gt;
Functional code is more concise, more predictable, we are almost sure what the result will be and accordingly it becomes easier to test than arbitrary imperative or object-oriented code.&lt;br&gt;
Let's explain all the basic and significant concepts we have in functional programming. First of all, this is it &lt;strong&gt;pure functions&lt;/strong&gt;, let's explain it. A pure function is a function that, first: always returns the same outputs for the same input.For example in mathematics the pure function is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sinx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know exactly and always that when x will be&lt;br&gt;
30 degrees, the answer will be 1/2.&lt;br&gt;
&lt;strong&gt;We have no side effects&lt;/strong&gt; in functional programming that means no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams.&lt;br&gt;
&lt;strong&gt;Function composition&lt;/strong&gt;: This is the process when we combine one or more functions for some computation.For example, if we have a function g(x) and another function composition of them, it will be new function h(x) = f(g(x));&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shared state:&lt;/strong&gt; is state which is shared between more than one function or more than one data-structure. When the state is immutable (can't be changed), this is relatively harmless and is basically a memory saving mechanism. If shared state is mutable and used simultaneously by multiple threads, then the program will need to use locks or other mechanisms to operate on the state.&lt;br&gt;
&lt;strong&gt;Immutability&lt;/strong&gt;: Immutability is a key concept because without it we lose all the flow of states, we lose the history of state changes.&lt;br&gt;
  &lt;strong&gt;Side effects&lt;/strong&gt;: Any operation that is not directly related to the final output of the function is called a Side Effect. For example Writing to a file is a side effect, or modify any external variable or object property ((e.g., a global variable, or a variable in the parent function scope chain).&lt;br&gt;
Side effects are mostly avoided in functional programming, which makes the effects of a program easier to extend,&lt;br&gt;
refactor, debug, test, and maintain.&lt;/p&gt;

&lt;p&gt;Let's consider an example of composition, let's say we have 2 functions and we want that between the composing log the current values.&lt;br&gt;
Let's take the following two function for more visibility.&lt;/p&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ttxutxn5g6rt3p4o1ne.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ttxutxn5g6rt3p4o1ne.png" alt="code of two give functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's write the union of these three functions:&lt;/p&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzpwqtg7ipt9dw9jfqy01.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzpwqtg7ipt9dw9jfqy01.png" alt="union of the functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We know from algebra that (f ◦ g)(x) = f (g(x)) , let's rewrite the same by code, we will have something like this&lt;br&gt;
&lt;a href="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%2Fegcivq76j3u6tqbgphll.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fegcivq76j3u6tqbgphll.png" alt="union of the functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is it, we can describe this union of functions in an even more general way, directly getting an array of functions as an argument.&lt;/p&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sf44yfgapbovrn807fe.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sf44yfgapbovrn807fe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we know we want to log the results returned by each function.&lt;br&gt;
Let's write a logger function that takes the label and the value we want to print, and returns it, so the chaining process is not broken, and from this value we calculate a new value.&lt;br&gt;
&lt;a href="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%2Fgvfe6wf4jnctvgddoycj.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgvfe6wf4jnctvgddoycj.png" alt="trace function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's put the functions in order, after which we want them to be executed.&lt;/p&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm3o8l7bbw5kg1k2pbpym.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm3o8l7bbw5kg1k2pbpym.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>functional</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>ES6: Promises</title>
      <dc:creator>Saba Shavidze</dc:creator>
      <pubDate>Thu, 22 Dec 2022 15:53:12 +0000</pubDate>
      <link>https://dev.to/shavidze/es6-promises-po3</link>
      <guid>https://dev.to/shavidze/es6-promises-po3</guid>
      <description>&lt;p&gt;ES6 Promises is probably the topic that is the worst explained everywhere and always 🫣 . &lt;br&gt;
Promise is an object✨, if you open web inspector, click the console and print :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmhhlj5utrwckxc3q1r65.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmhhlj5utrwckxc3q1r65.png" alt="Image description" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will see that it has actually 3 properties but we are interested in two:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;[[PromiseState]]&lt;/strong&gt; - a state that has 3 states itself,&lt;/li&gt;
&lt;li&gt;and &lt;strong&gt;[[PromiseResult]]&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's explain what &lt;strong&gt;[[PromiseState]]&lt;/strong&gt; values are and what each one means. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First one is &lt;strong&gt;fulfilled&lt;/strong&gt;: it means that everything was completed successfully, &lt;/li&gt;
&lt;li&gt;Second one is &lt;strong&gt;rejected&lt;/strong&gt;: an error occurred and it was rejected, &lt;/li&gt;
&lt;li&gt;and last one is &lt;strong&gt;pending&lt;/strong&gt;: we are still waiting for a response, neither resolved nor rejected. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As for &lt;strong&gt;[[PromiseResult]]&lt;/strong&gt;, it has two states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;undefined&lt;/strong&gt; - when nothing has been returned yet.&lt;/li&gt;
&lt;li&gt;and &lt;strong&gt;the value that we pass to the either the resolved or rejected method as their argument.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's explain how these conditions change. In the promise printed above, we put an arrow function that does nothing, however, the promise callback function is generally given two arguments, resolve and reject, which will be called depending on how successfully the callback function completed. Let's make it clearer so that we can write something like that&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7pzne8h6uflkcu677m9t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7pzne8h6uflkcu677m9t.png" alt="Image description" width="800" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will see that &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PromiseState&lt;/strong&gt; - is already &lt;strong&gt;fullfiled&lt;/strong&gt; (even though it prints resolved in the console, it is a bug of the browser)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;and &lt;strong&gt;PromiseResult&lt;/strong&gt;: &lt;strong&gt;completed&lt;/strong&gt;, similarly, if we returned rejected("reject"), the state would be: &lt;strong&gt;"rejected"&lt;/strong&gt; and value: "reject" .&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we take a look into the Promise prototype, we will see 3 built-in methods: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;then()&lt;/strong&gt; - which is called if the Promise is resolved, &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;catch()&lt;/strong&gt; - which is called if it is rejected,&lt;/li&gt;
&lt;li&gt;and &lt;strong&gt;finally()&lt;/strong&gt; - which is always called, regardless of the return state. 
&lt;strong&gt;then()&lt;/strong&gt; and &lt;strong&gt;catch()&lt;/strong&gt; returns a Promise itself, which gave us possibility to pass another callback function , so
it allows us to build a whole chain of promises.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's talk about where the promise goes in the event loop. As we know, the asynchronous calls that the browser sends first go to &lt;strong&gt;web api&lt;/strong&gt; -&amp;gt; then the results goes in the &lt;strong&gt;queue&lt;/strong&gt; -&amp;gt; and when the call stack finishes, they will return in sequence from the queue. &lt;br&gt;
&lt;strong&gt;Queue&lt;/strong&gt;- itself is divided into 2 types of queues,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;the first is the queue of macrotasks&lt;/strong&gt;: setTimeOut, setInterval, setImmediate etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;the second is the queue of microtasks&lt;/strong&gt;: process.nextTick, Promise callback, async functions, queueMicrotask...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the stack is finished, first it returns microtasks and then the macrotasks are released from the queue. That is, the promise will always return from the queue earlier than setTimeOut 🤌.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
