<?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: Asidipta</title>
    <description>The latest articles on DEV Community by Asidipta (@asi309).</description>
    <link>https://dev.to/asi309</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%2F480088%2F409331c0-9437-4993-a92a-0de5b71bd4f9.jpeg</url>
      <title>DEV Community: Asidipta</title>
      <link>https://dev.to/asi309</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asi309"/>
    <language>en</language>
    <item>
      <title>Redux: The Concept</title>
      <dc:creator>Asidipta</dc:creator>
      <pubDate>Sun, 18 Jun 2023 15:54:25 +0000</pubDate>
      <link>https://dev.to/asi309/redux-the-concept-3pno</link>
      <guid>https://dev.to/asi309/redux-the-concept-3pno</guid>
      <description>&lt;p&gt;You probably heard of &lt;em&gt;Redux&lt;/em&gt; while learning React. It has been a buzz word really with the huge growth in popularity of &lt;em&gt;React.js&lt;/em&gt; There are so many tutorials out there that explain redux with react and there is nothing wrong with that.&lt;/p&gt;

&lt;p&gt;The point I am trying to put forward is that Redux is not a mere javascript library that you can use with React. It is more of a &lt;em&gt;state management concept&lt;/em&gt; that you can use anywhere to manage states in your applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR;
&lt;/h2&gt;

&lt;p&gt;In short, redux is based on the following principles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintain a global data structure which is the single source of truth for your entire application. We call this the &lt;em&gt;store&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The store is immutable.&lt;/li&gt;
&lt;li&gt;Any event that can update &lt;em&gt;store&lt;/em&gt; needs to fire an &lt;em&gt;action&lt;/em&gt; with/without data.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Actions&lt;/em&gt; are sent to the store through pure functions known as &lt;em&gt;reducers&lt;/em&gt; that take inputs from actions and send new state to the store.&lt;/li&gt;
&lt;li&gt;Store will notify all the components of your application if anything has changed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's start from the start
&lt;/h2&gt;

&lt;p&gt;Okay, so I will try to keep this as simple as possible without using any react state management keywords for you to grasp the power of redux as a concept.&lt;br&gt;
Why do we need to understand &lt;em&gt;state management&lt;/em&gt;? State management is a crucial part of building any application whether it is frontend or backend.&lt;br&gt;
You might say with REST APIs we do not need to manage state in our server anymore. Well you would be partially correct since that statement is only true for user data. You might have to keep certain states of variables in memory since database calls are expensive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux concepts
&lt;/h2&gt;

&lt;p&gt;Moving along, let us understand what we need to know to grasp this concept.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Store
&lt;/h3&gt;

&lt;p&gt;The store is a data structure that is maintained globally. In other words, it is stored in a location that is accessible to all parts of your application.&lt;br&gt;
It is the single source of truth for your application, which is just a complicated way of saying that if you face any conflict in state, the store's state prevails.&lt;br&gt;
The store is &lt;em&gt;immutable&lt;/em&gt;. You need to ensure that neither of your functions directly accesses or writes over the state maintained in the store.&lt;br&gt;
The store will provide accessor functions to read the state from.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Action
&lt;/h3&gt;

&lt;p&gt;Action is a function that is triggered whenever you want to change a state. So, lets say component A wants to increase global counter to 5. It will call the suitable action and pass along 5. Action is responsible for triggering a state change request from user.&lt;br&gt;
Actions can even handle any side effects you want to include like something from a database,&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Reducer
&lt;/h3&gt;

&lt;p&gt;A reducer is a pure function which means a reducer is not affected by anything happening in the application. It reads the store and copies the state. It gets details of what to do from the action and might also receive some data parameters from the action that user has called. It will process these data and perform certain operations on it's copy of the state and return the new state.&lt;/p&gt;

&lt;p&gt;These 3 parts make the fundamentals of redux. Really! I promise. All other nitty-gritties you might have heard are all implementation details that keeps on changing as various libraries evolve.&lt;br&gt;
So, how does these components all work together? Here is a schematic diagram to explain just that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tMtqNhgo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0g9di75rpudk8wrbs4kp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tMtqNhgo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0g9di75rpudk8wrbs4kp.jpg" alt="Components of Redux together" width="691" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Want to see how these simple concepts in redux come together to form great applications?&lt;br&gt;
Here is a simple web app to show it in action&lt;br&gt;
&lt;a href="https://windbnb-319.netlify.app/"&gt;Windbnb&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/asi309/windbnb"&gt;Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you liked this approach of looking at things, please follow me and share this with other developers as well.&lt;/p&gt;

</description>
      <category>redux</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>New Array Methods in JS</title>
      <dc:creator>Asidipta</dc:creator>
      <pubDate>Fri, 09 Jun 2023 19:17:11 +0000</pubDate>
      <link>https://dev.to/asi309/new-array-methods-in-js-4mj4</link>
      <guid>https://dev.to/asi309/new-array-methods-in-js-4mj4</guid>
      <description>&lt;p&gt;If you are a javascript developer like me, you probably have used array methods quite a lot. While the array methods in javascript are performant, in terms of immutability we did face a shortage of methods. Let me make it clear with 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;// Define an array
const arr = [1, 2, 5, 8];

// Replace the second element with 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you might say, it's easy, just do this: &lt;code&gt;arr[1] = 7&lt;/code&gt; and you won't be wrong. It definitely works. But in doing so, you have mutated(changed) the original array. If you now print the array, it will be something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(arr); //Prints [1, 7, 5, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  So, what is the alternative?
&lt;/h2&gt;

&lt;p&gt;Upto this point, the easiest way is to make a copy and apply the transformations.&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, 5, 8];
const copyArr = [...arr];
copyArr[1] = 7;
console.log(copyArr); // Prints [1, 7, 5, 8]
console.log(arr); // Prints [1, 2, 5, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But now we have a few &lt;em&gt;extra&lt;/em&gt; methods that make our life easier and makes the code more understandable.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;with&lt;/em&gt; method
&lt;/h2&gt;

&lt;p&gt;The with method essentially makes a copy of the array and changes the value at a given index in the array. So, now instead of doing something like this: &lt;code&gt;const copyArr = [...arr]&lt;/code&gt; and &lt;code&gt;copyArr[1] = 7&lt;/code&gt;, we can copy the array and change the value at &lt;em&gt;index&lt;/em&gt; 1 with this snippet:&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, 7, 5, 8];
const copyArr = arr.with(1, 70);
console.log(copyArr); // Prints [1, 70, 5, 8]
console.log(arr); // Prints [1, 7, 5, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;em&gt;toSorted&lt;/em&gt; method
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;sort&lt;/em&gt; method, as you might be familiar with already, sorts the array &lt;em&gt;in-place&lt;/em&gt;. To ensure immutability, we first have to copy the array and then apply the &lt;em&gt;sort&lt;/em&gt; method. Which means there's at least 2 passes on the array, thus giving &lt;code&gt;O(2n)&lt;/code&gt; time complexity at best.&lt;br&gt;
However, with &lt;em&gt;toSorted&lt;/em&gt; javascript only does one pass over the array to copy and sort 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 arr = [1, 7, 5, 8];
const copyArr = arr.toSorted();
console.log(copyArr); // Prints [1, 5, 7, 8]
console.log(arr); // Prints [1, 7, 5, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;em&gt;toReversed&lt;/em&gt; method
&lt;/h2&gt;

&lt;p&gt;This method is similar to the &lt;em&gt;toSorted&lt;/em&gt; method. It replaces the already existing &lt;em&gt;reverse&lt;/em&gt; method for immutability.&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, 7, 5, 8];
const copyArr = arr.toReversed();
console.log(copyArr); // Prints [8, 5, 7, 1]
console.log(arr); // Prints [1, 7, 5, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;em&gt;toSpliced&lt;/em&gt; method
&lt;/h2&gt;

&lt;p&gt;This method is the copying instance version of the popular &lt;em&gt;splice&lt;/em&gt; method which provides many overloads to delete from an index, insert into an index, delete all entries after an index. So, it goes without saying that it helps us a lot in performing immutable array operations.&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, 7, 5, 8];
const copyArr = arr.toSpliced(0, 3, 44);
console.log(copyArr); // Prints [44, 8]
console.log(arr); // Prints [1, 7, 5, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;These new array methods are all available in prototype of Array.  However, these are new features and might not be supported by all browsers. As of the writing of this blog, these are not yet supported in any version of Firefox browser and only recent versions of Chrome browser supports these new methods. Safari, Edge and Opera also support these methods in newer versions (&lt;em&gt;Ref&lt;/em&gt;: &lt;a href="https://caniuse.com"&gt;https://caniuse.com&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Setup SSH on windows</title>
      <dc:creator>Asidipta</dc:creator>
      <pubDate>Sat, 18 Mar 2023 16:36:44 +0000</pubDate>
      <link>https://dev.to/asi309/setup-ssh-on-windows-5agp</link>
      <guid>https://dev.to/asi309/setup-ssh-on-windows-5agp</guid>
      <description>&lt;p&gt;How do you generally connect to your windows machine remotely?&lt;/p&gt;

&lt;p&gt;I bet you use some 3rd party software or use Remote Desktop connection (uses RDP) to connect.&lt;/p&gt;

&lt;p&gt;Now RDP is a SOAP based connection protocol and it is not that great either is it? I mean you can not easily use RDP from any other platform such as macOS or linux can you? Situation is even worse if you need to do it programmatically.&lt;/p&gt;

&lt;p&gt;But hey! Did you know you could very easily use SSH? Well I did not!&lt;/p&gt;

&lt;p&gt;It turns out our old faithful PCs can connect quite easily over SSH. We just have to enable a few settings and what better way to do this than firing up the Powershell?&lt;/p&gt;

&lt;p&gt;First, let's clarify the terminology. The system that we will connect to is referred to as &lt;em&gt;server&lt;/em&gt; (&lt;u&gt;This has got to be a windows machine&lt;/u&gt;). The system that we will use to connect is referred to as the &lt;em&gt;client&lt;/em&gt; (This can be running any desktop OS).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open Powershell on the server as &lt;u&gt;Administrator&lt;/u&gt; and check if openssh services are available.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;It can be that neither of the services are available. But mostly on newer windows installations, open-ssh clients are available. So, I will show how to install the server. Client can be installed similarly.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Now start the open ssh server
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start-Service sshd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Check if the services are running. Open SSH server needs to be running in order to make the device act as a server.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Get-Service sshd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;(Optional)&lt;/em&gt; Set the services to startup automatically on windows boot.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set-Service -Name sshd -StartupType 'Automatic'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! You can now ssh directly from your client terminal into this windows machine (server).&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>windows</category>
    </item>
    <item>
      <title>Set up docker and kubernetes in ubuntu 22.04</title>
      <dc:creator>Asidipta</dc:creator>
      <pubDate>Sat, 18 Mar 2023 15:54:03 +0000</pubDate>
      <link>https://dev.to/asi309/set-up-docker-and-kubernetes-in-ubuntu-2204-ncb</link>
      <guid>https://dev.to/asi309/set-up-docker-and-kubernetes-in-ubuntu-2204-ncb</guid>
      <description>&lt;p&gt;There are a lot of resources on the official websites for this topic. Then why am I writing this blog?&lt;br&gt;
This blog is for someone who is trying to set up docker and kubernetes in a ubuntu machine, but has no experience in dev-ops whatsoever and wants to setup the environment quickly for running some piece of code from a developer bootcamp or any tutorial.&lt;br&gt;
So let's dive into it without further ado.&lt;/p&gt;

&lt;p&gt;We will be using &lt;a href="https://www.docker.com/"&gt;docker&lt;/a&gt; and &lt;a href="https://microk8s.io/"&gt;microk8s&lt;/a&gt; from Canonical.&lt;br&gt;
For running our software during development, we will be using &lt;a href="https://skaffold.dev/"&gt;skaffold&lt;/a&gt; which is a great tool developed by Google.&lt;/p&gt;



&lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  PART 1: Docker
&lt;/h2&gt;



&lt;p&gt;Docker makes development easier, more efficient and predictable. It solves the age old issue of developers (&lt;em&gt;it worked fine on my device&lt;/em&gt;) by running the code in small instances (containers) of popular server OSes. Multiple containers can be run in a simple laptop machine hardware without needing any special tools or expertise in setting up OS.&lt;/p&gt;
&lt;h3&gt;
  
  
  Installation Guide
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;a href="https://docs.docker.com/desktop/install/ubuntu/"&gt;docker docs for ubuntu&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Follow the steps provided in this site. These steps are very comprehensive and easy to follow. So we will not be discussing further steps here.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Just follow the steps on the site and you are good to go!&lt;/p&gt;



&lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  PART 2: Kubernetes
&lt;/h2&gt;



&lt;p&gt;Kubernetes is a tool for automating certain aspects of &lt;em&gt;operations&lt;/em&gt; part of IT. It is popularly used to manage deployments, roll out changes to deployed applications, scaling applications horizontally, monitoring and much more.&lt;/p&gt;

&lt;p&gt;We will be using kubernetes (hereafter referred to as &lt;strong&gt;&lt;em&gt;k8s&lt;/em&gt;&lt;/strong&gt;) for creating an orchestration of all our different services. In other words, the kubernetes environment will contain all the logic for inter-service connection and exposing the functionalities of the overall application, i.e., it will create a black-box environment for the outside world and only expose a few API endpoints for the functionality.&lt;/p&gt;
&lt;h3&gt;
  
  
  Installation Guide
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use snap to install the microk8s module. We will go with channel 1.26 which is a stable channel as of the writing of this blog.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo snap install microk8s --classic --channel=1.26
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Join the group, so that the user does not need root access to run any command from microk8s
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo usermod -a -G microk8s $USER
sudo chown -f -R $USER ~/.kube

su - $USER
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Check status of microk8s. We have completed installation of microk8s by this point. Sadly, many more steps to come.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;microk8s status
# Wait till the microk8s cluster is ready
microk8s status --wait-ready
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Deploy an application (say &lt;em&gt;nginx&lt;/em&gt;) and test
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;microk8s kubectl create deployment nginx --image=nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here the application name is nginx and we are using the docker image &lt;em&gt;nginx&lt;/em&gt; which is the official image provided by nginx team.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check if the application was deployed properly.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;microk8s kubectl get pods
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The pod(s) should have STATUS as &lt;strong&gt;&lt;em&gt;Running&lt;/em&gt;&lt;/strong&gt; and READY as &lt;strong&gt;&lt;em&gt;1/1&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;2/2&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;not&lt;/strong&gt; like &lt;strong&gt;&lt;em&gt;0/1&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, we want to be able to use kubectl command in the &lt;em&gt;microk8s&lt;/em&gt; context without having to type &lt;code&gt;microk8s kubectl&lt;/code&gt;. We want to type only &lt;code&gt;kubectl&lt;/code&gt; and that should be enough to process our request inside the microk8s cluster. For this, follow the below steps:
a. &lt;code&gt;sudo snap unalias kubectl&lt;/code&gt; in case kubectl is already aliased somewhere in your environment.
b. &lt;code&gt;sudo snap install kubectl --classic&lt;/code&gt;
c. &lt;code&gt;microk8s.kubectl config view --raw &amp;gt; $HOME/.kube/config&lt;/code&gt; will copy the microk8s kubectl configs into our user's HOME, so that they can be directly accessed by the user using the kubectl command.&lt;/li&gt;
&lt;li&gt;At this point, we can use microk8s normally and deploy applications. Further steps are specific to our use-cases.&lt;/li&gt;
&lt;li&gt;Install ingress-nginx
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;microk8s enable ingress
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Install skaffold for development in k8s&lt;br&gt;
a. Get the stable binaries only. &lt;a href="https://skaffold.dev/docs/install/"&gt;Installation Guide&lt;/a&gt;. Go to the Linux tab and follow the instructions to get the stable binaries&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# For Linux x86_64 (amd64)
curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-amd64 &amp;amp;&amp;amp; \
sudo install skaffold /usr/local/bin/
------------------------------------------------------------------------------
# For Linux ARMv8 (arm64)
curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-arm64 &amp;amp;&amp;amp; \
sudo install skaffold /usr/local/bin/
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now if you run &lt;code&gt;skaffold dev&lt;/code&gt; in your project that contains a &lt;em&gt;skaffold.yaml&lt;/em&gt; file, you will get some error related to the build process. So, basically skaffold will complain that it can not find the tags that it itself associated with the images present in the yaml file during build. &lt;strong&gt;&lt;em&gt;Please note&lt;/em&gt;&lt;/strong&gt;, that this error is specific to &lt;em&gt;microk8s&lt;/em&gt;. It does not occur with Docker Desktop kubernetes implementations for Mac/Windows OS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;We will have to therefore, use private registry in the local environment to build the containers from images during development. &lt;strong&gt;&lt;em&gt;Please note&lt;/em&gt;&lt;/strong&gt; that skaffold and microk8s kubectl can still pull images from docker registry during production CI/CD deployments.&lt;br&gt;
a. Go to &lt;code&gt;/etc/docker&lt;/code&gt; folder.&lt;br&gt;
b. Create a &lt;em&gt;daemon.json&lt;/em&gt; file in this location. &lt;code&gt;sudo nano daemon.json&lt;/code&gt;. This will open up nano editor with this new file.&lt;br&gt;
c. Put the following entry inside the file.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
"insecure-registries" : ["localhost:32000"]
}
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;d. Press &lt;code&gt;Ctrl + O&lt;/code&gt; then press &lt;code&gt;Enter&lt;/code&gt;. This will save the file.&lt;br&gt;
e. Now press &lt;code&gt;Ctrl + X&lt;/code&gt;. This will exit the editor.&lt;br&gt;
f. Now we have to restart docker to make sure the changes are accepted. For this, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart docker
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;g. After you run this command, wait for a few moments and then check if the docker engine has restarted with the command:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status docker
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Check if the &lt;em&gt;Active&lt;/em&gt; state is in green and it says &lt;em&gt;'(running)'&lt;/em&gt;. Then proceed further.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Change the image value in skaffold.yaml file like so:&lt;br&gt;&lt;br&gt;
&lt;code&gt;username/image-name&lt;/code&gt; -&amp;gt; &lt;code&gt;localhost:32000/image-name&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;skaffold dev&lt;/code&gt; command now. It will take a few minutes for the pods to be created and stabilized.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it! I promise there is nothing left to do to start running microservices codes on your ubuntu machine, be it during development or during deployment.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>kubernetes</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Demystifying 'this' keyword in JS</title>
      <dc:creator>Asidipta</dc:creator>
      <pubDate>Sun, 22 May 2022 13:55:02 +0000</pubDate>
      <link>https://dev.to/asi309/demystifying-this-keyword-in-js-3130</link>
      <guid>https://dev.to/asi309/demystifying-this-keyword-in-js-3130</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;This&lt;/em&gt; keyword holds a special place in any object-oriented programming language and has been a mind-bender in Javascript. Needless to say, it has thus been a favourite topic of interviewers everywhere.&lt;br&gt;
So, let's dive into what &lt;em&gt;this&lt;/em&gt; signifies and what its value is in different contexts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This&lt;/strong&gt; keyword returns the execution context of the function or code block in which &lt;em&gt;this&lt;/em&gt; has been used.&lt;br&gt;
For example, if we perform the following operations in global context (outside of any functions), irrespective of 'strict' mode or not, we will get the results as such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = "XYZ";
console.log(window.name); // Prints XYZ
console.log(this === window) // Prints true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Context
&lt;/h3&gt;

&lt;p&gt;In this case, the value of &lt;em&gt;this&lt;/em&gt; depends upon how the function is called.&lt;br&gt;
When &lt;em&gt;not&lt;/em&gt; in 'strict' mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function f() {
  console.log(this === window)
}

f(); // prints true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In 'strict' mode, if &lt;em&gt;this&lt;/em&gt; is not set when entering an execution context, &lt;em&gt;this&lt;/em&gt; remains &lt;em&gt;undefined&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function f() {
  'use strict';
  console.log(this===undefined);
}

f();  // prints true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Note:&lt;/code&gt; You can set &lt;em&gt;this&lt;/em&gt; using apply() or call() functions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const x = {val: 'x-val'};
const val = 'global-val';

function f() {
  console.log(this.val);
}

f() // prints 'global-val'
f.call(x) // prints 'x-val'
f.apply(x) // prints 'x-val'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In non-strict mode, if the value of &lt;em&gt;this&lt;/em&gt; (first argument to call() and apply()) is not an object, attempt is made to convert it into an object. 'null' or 'undefined' becomes the global object. Primitives like 1 or 'text' becomes object using respective constructors, Number and String in this example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Class Context
&lt;/h3&gt;

&lt;p&gt;The behaviour of &lt;em&gt;this&lt;/em&gt; is similar in class and function contexts as class in javascript is just syntactical sugar. Basically, it is just a function.&lt;/p&gt;

&lt;p&gt;Within the &lt;em&gt;constructor&lt;/em&gt; function of a class, the &lt;em&gt;this&lt;/em&gt; is just an ordinary object. All non-static methods of the class are added to the prototype of &lt;em&gt;this&lt;/em&gt; object.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;derived classes&lt;/strong&gt;, there is no &lt;em&gt;this&lt;/em&gt; binding. One has to call &lt;code&gt;super()&lt;/code&gt; function from the derived class. What this does is call the constructor of the base class and assign the &lt;em&gt;this&lt;/em&gt; object from that constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this = new Base()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Note:&lt;/code&gt; Derived classes cannot return without calling &lt;code&gt;super()&lt;/code&gt; or returning an object, because that will cause &lt;em&gt;ReferenceError&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bind method
&lt;/h3&gt;

&lt;p&gt;ES5 introduced the &lt;em&gt;bind&lt;/em&gt; method on functions. Calling &lt;code&gt;f.bind()&lt;/code&gt; creates a new function with same body and scope as f. The &lt;em&gt;this&lt;/em&gt; however is bound to the first argument passed to the bind() function. Once &lt;em&gt;this&lt;/em&gt; is bound by using bind(), it cannot be changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function f() {
  console.log(this.val);
}

x = f.bind({val: 'new val'});
x(); // prints 'new val'

y = x.bind({val: 'change val'})
y(); // prints 'new val'

const obj = {val: 1, f:f, x: x, y: y};
obj.f(); // prints 1
obj.x(); // prints 'new val'
obj.y(); // prints 'new val'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Arrow functions
&lt;/h3&gt;

&lt;p&gt;In arrow functions, &lt;em&gt;this&lt;/em&gt; retains the value of the enclosing context's &lt;em&gt;this&lt;/em&gt;. In global context, that would mean the global object. In function context, it will be determined as like we discussed in the sections above.&lt;/p&gt;

&lt;p&gt;For 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 obj= {
  x: function() {
    const y = () =&amp;gt; this;
    return y;
  }
};

// calling x with obj context and setting its reference to f
const f = obj.x();

// Calling f would normally return window or global object or //undefined in strict mode
console.log(f() === obj) // true

// However, in this case above behaviour will not be true
const f = obj.x; // we are just taking the function

// Calling f from global context
console.log(f()() === obj) // false, this will equal global
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>React.js Lifecycle Methods</title>
      <dc:creator>Asidipta</dc:creator>
      <pubDate>Tue, 17 May 2022 19:19:49 +0000</pubDate>
      <link>https://dev.to/asi309/reactjs-lifecycle-methods-e8</link>
      <guid>https://dev.to/asi309/reactjs-lifecycle-methods-e8</guid>
      <description>&lt;p&gt;React.js uses a declarative approach of programming. In other words, the developer only needs to declare the changes as per different states of the application. &lt;br&gt;
For this, React uses the Virtual DOM. The actual visual changes are performed using some &lt;strong&gt;lifecycle methods&lt;/strong&gt; which provide the developer additional control over what should mount or what should update or what are the cleanups to perform before unmounting a component.&lt;/p&gt;

&lt;p&gt;Today, we are going to discuss these lifecycle methods.&lt;/p&gt;
&lt;h2&gt;
  
  
  Lifecycle methods while mounting
&lt;/h2&gt;

&lt;p&gt;In this section, we will discuss the life cycle methods that are invoked when a component is initialized or mounted to the DOM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. constructor():&lt;/strong&gt; The constructor method initializes the state and any other variables for the component. This is the first method that is called when initializing a component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. static getDerivedStateFromProps():&lt;/strong&gt; This method is called just after the constructor initializes the component. It can update the state of the component based on the props as required.&lt;/p&gt;

&lt;p&gt;If state is to be updated, return the updated state from this function. Otherwise, return null.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static getDerivedStateFromProps(props, state) {
  // some logic for syncing state and props
  // if updates --&amp;gt; return updated state
  // else --&amp;gt; return null
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. render():&lt;/strong&gt; Render method is called for mounting the JSX to the DOM after the &lt;em&gt;getDerivedStateFromProps&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;This is the only method &lt;strong&gt;required&lt;/strong&gt; in a component.&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;This method returns the JSX for the component that is to be mounted to the DOM. We can also return arrays and React Fragments from render method.&lt;/p&gt;

&lt;p&gt;If &lt;em&gt;nothing&lt;/em&gt; is to be mounted we can return null from this method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() {
  if(// some condition) {
    return null; // will not render anything
  } else {
    return (//JSX or array or React.Fragment)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. componentDidMount():&lt;/strong&gt; Immediately after the render method returns and the component is mounted to the DOM, this method is called.&lt;/p&gt;

&lt;p&gt;Typical usecase for this method is to select any element from the component that was just mounted. This can then further be used to perform any subscription or make any network requests for the component just mounted to the DOM.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidMount() {
  const x = document.getElementById('abc');
  // perform any operation on x here
  x.addEventListener() // This is also possible
  // fetch('https://google.com').then(); is also possible here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Lifecycle methods while updating
&lt;/h2&gt;

&lt;p&gt;In this section we will discuss the lifecycle methods called when updating a component which is already mounted to the DOM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. static getDerivedStateFromProps():&lt;/strong&gt; This method runs first whenever any component is to be updated. This has been discussed earlier, so, I am skipping this here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. shouldComponentUpdate()&lt;/strong&gt;: This method is called after the &lt;em&gt;getDerivedStateFromProps()&lt;/em&gt; method. This method returns &lt;em&gt;True&lt;/em&gt; or &lt;em&gt;False&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This method is used to control whether this component should be updated in the DOM based on the changes to state or props. If &lt;em&gt;True&lt;/em&gt; is returned, it will proceed to update else no update will take place in the DOM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. render(): ** This runs after the &lt;em&gt;shouldComponentUpdate()&lt;/em&gt; method, **if and only if&lt;/strong&gt; &lt;em&gt;shouldComponentUpdate()&lt;/em&gt; returns True.&lt;br&gt;
This method has already been discussed above, so skipping.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;4. getSnapshotBeforeUpdate(): *&lt;/em&gt; This method is invoked immediately after &lt;em&gt;render()&lt;/em&gt; method runs to update the DOM. This takes a snapshot of the DOM before the update while the visual DOM is still being updated asynchronously.&lt;/p&gt;

&lt;p&gt;This method gets argument the previousProps and previousState which were the props and state before the update.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getSnapshotBeforeUpdate(prevProps, prevState) {
  return value || null; // value can be any valid javascript value
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value returned from this method is further passed to the next lifecycle method, &lt;em&gt;componentDidUpdate()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. componentDidUpdate():&lt;/strong&gt; This lifecycle method is called after &lt;em&gt;getSnapshotBeforeUpdate()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;This method receives &lt;strong&gt;previousProps&lt;/strong&gt;, &lt;strong&gt;previousState&lt;/strong&gt; and &lt;strong&gt;snapshot&lt;/strong&gt; as argument, where &lt;em&gt;snapshot&lt;/em&gt; is the value passed from the &lt;em&gt;getSnapshotBeforeUpdate()&lt;/em&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lifecycle methods while unmounting
&lt;/h2&gt;

&lt;p&gt;In this section, we will discuss the lifecycle method called for unmounting or removing the component from the DOM.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;1. componentWillUnmount(): *&lt;/em&gt; This method is called immediately before the component is unmounted from the DOM. This function is suitable for performing any cleanup(s) before the component is removed from the DOM.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentWillUnmount() {
  // remove any subscriptions or timers or unresolved network requests
  x.removeEventListener()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Advantages and Disadvantages of React.js</title>
      <dc:creator>Asidipta</dc:creator>
      <pubDate>Tue, 17 May 2022 14:52:23 +0000</pubDate>
      <link>https://dev.to/asi309/advantages-and-disadvantages-of-reactjs-bl2</link>
      <guid>https://dev.to/asi309/advantages-and-disadvantages-of-reactjs-bl2</guid>
      <description>&lt;p&gt;React.js is a Javascript &lt;em&gt;library&lt;/em&gt; created by Facebook. In recent years, it has become very popular over the likes of other javascript frameworks or libraries. So, in this post, I will try to discuss the benefits of using this library and why this has become the &lt;em&gt;de-facto&lt;/em&gt; js library in web development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of React.js
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Flexibility:&lt;/strong&gt; Since React.js is a _library _and not a framework, it offers control to the developer to use any style or pattern for development. The developer is not fixed to a particular pattern. However, there are certain best practices that one can follow while creating SPAs using react.js&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Performance:&lt;/strong&gt; React.js uses a &lt;em&gt;declarative&lt;/em&gt; style of programming and at its core uses a &lt;strong&gt;Virtual DOM&lt;/strong&gt; to track changes and render the DOM to the browser. It also supports Server-Side Rendering, which enables webpages to be quickly and seamlessly sent to the user. This all means web apps created using React.js are fast as well as resource efficient.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Reusability:&lt;/strong&gt; React.js best practices include dividing the code into &lt;em&gt;components&lt;/em&gt;, thereby creating component trees. State can be managed as per the granularity required and only components below in the tree would update. But, components also mean developer has to write &lt;em&gt;less&lt;/em&gt; code and can reuse the code for similar components in different sections of the SPA. HOC pattern also exists so that different components can be processed in a similar way thus increasing reusability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Speed of development:&lt;/strong&gt; Owing to the reusability of components and various patterns along with vast and rich libraries available reduce the time required for developing apps using React.js very less. The developer community is also very active in this library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Ease of learning:&lt;/strong&gt; The learning curve for someone new to React.js is not very steep. Good knowledge of javascript is sufficient to be successful at developing apps using React. However, some may face challenges initially in using &lt;em&gt;JSX&lt;/em&gt; which is the basis of the library. But knowing HTML5 and some basic knowledge of XML can prove really helpful in that case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Testing is easier:&lt;/strong&gt; React comes with &lt;em&gt;Jest&lt;/em&gt;, a testing library made especially for React. Along with that, there are many other javascript test tools which can easily be used to test react apps. The declarative code is also easier to test from the user's perspective rather than testing implementation details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disadvantages of React.js
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Rapid pace of development of React&lt;/strong&gt;: React.js is a library which is still evolving. Every few years a major change is expected that changes the way of development. So developers need to be up to date with their knowledge. For example, in React 16.8 and above, they introduced the hooks and deprecated the use of class components and lifecycle methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Documentation:&lt;/strong&gt; With such a rapid pace of development, the documentation does not always remain updated. Although this has improved recently and we get updated documents very quickly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Only caters to the View part:&lt;/strong&gt; Using React.js has been debated as being an overkill in many cases where developers have argued that in &lt;strong&gt;MVC&lt;/strong&gt; pattern of developing web apps, React only creates the View part. So such complex logic handling capabilities are a waste. This is not very credible however, as handling some view logic using client side javascript libraries reduces network calls and seperates view logic from business logic. Using React reduces the use of the DOM and rather uses the client side memory to perform javascript operations only.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Extensive use of JSX:&lt;/strong&gt; This is seen as one of the largest drawback of the library. It can be daunting for someone trying to learn React.js. But this can be overcome quite easily and the learning curve becomes flatter with some exposure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So that was my take on the very popular library that is React.js. Personally, I have found React.js to be very easy to learn and stay updated with the extensive community support from so many talented developers across the world. I have been using React for 2 years now and I have not faced any major difficulty or setbacks in this time.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>discuss</category>
    </item>
    <item>
      <title>LeetCode Daily Challenge Series #1</title>
      <dc:creator>Asidipta</dc:creator>
      <pubDate>Thu, 05 May 2022 17:32:40 +0000</pubDate>
      <link>https://dev.to/asi309/leetcode-daily-challenge-series-1-18o0</link>
      <guid>https://dev.to/asi309/leetcode-daily-challenge-series-1-18o0</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Before implementing any code, let's think about how to proceed. There are 2 ways to solve this problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;#### Using 2 queues: We can use 2 queues to implement a stack. We can process this in 2 ways -
&lt;ol&gt;
&lt;li&gt;Making push method expensive&lt;/li&gt;
&lt;li&gt;Making the pop method expensive&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The decision is based on the application. In our case, either one is fine.&lt;/p&gt;

&lt;h5&gt;
  
  
  Expensive Push method:
&lt;/h5&gt;

&lt;p&gt;Push the element into Queue2 while rest of the elements are stored in Queue1. After this, pop elements from Queue1 and push into Queue2. Swap the names of Queue1 and Queue2.&lt;br&gt;
If we repeat this process each time a new element is added, we will have the elements in the order we would expect to pop from a stack. Thus, we have achieved our stack.&lt;/p&gt;
&lt;h5&gt;
  
  
  Expensive Pop method:
&lt;/h5&gt;

&lt;p&gt;Each time we have to pop - Dequeue all elements from Queue1 except the last element and enqueue into Queue2. Dequeue the last element from Queue1 and store it (this is the &lt;em&gt;result&lt;/em&gt; that we will return). Swap the names of Queue1 and Queue2. Return the stored element. Thus, we will have the LIFO order while popping.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;#### Using 1 queue: It is possible to achieve the above behaviour using just 1 queue.
&lt;ol&gt;
&lt;li&gt;
Take the length of the queue, &lt;em&gt;say n&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Enqueue&lt;/em&gt; the element
&lt;/li&gt;
&lt;li&gt;
Run a loop till &lt;em&gt;n&lt;/em&gt;. Dequeue the element from the queue and enqueue it. This will make the new element the first element in the queue. 
If we keep following this every time we push a new element in the queue, we will have the elements in the proper order as we expect from a queue.
&lt;/li&gt;
&lt;li&gt;
Since, we have corrected the ordering of the elements in the queue, &lt;strong&gt;pop&lt;/strong&gt; operation should be as simple as dequeue and return the element.
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;

&lt;p&gt;Here is python implementation of class handling the stack using a single queue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from collections import deque

class MyStack:

    def __init__(self):
        self.q1 = deque()


    def push(self, x: int) -&amp;gt; None:
        size = len(self.q1)
        self.q1.append(x)
        for i in range(size):
            el = self.q1.popleft()
            self.q1.append(el)

    def pop(self) -&amp;gt; int:
        return self.q1.popleft()


    def top(self) -&amp;gt; int:
        return self.q1[0]

    def empty(self) -&amp;gt; bool:
        return len(self.q1) == 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Here we are using &lt;em&gt;deque&lt;/em&gt; collection from python, but we are using it as a normal queue only. We have not used any of the double-ended properties of &lt;em&gt;deque&lt;/em&gt;&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>computerscience</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>A Beginner's Guide to Authentication</title>
      <dc:creator>Asidipta</dc:creator>
      <pubDate>Mon, 19 Apr 2021 02:46:41 +0000</pubDate>
      <link>https://dev.to/asi309/a-beginner-s-guide-to-authentication-3866</link>
      <guid>https://dev.to/asi309/a-beginner-s-guide-to-authentication-3866</guid>
      <description>&lt;h2&gt;
  
  
  What is Authentication?
&lt;/h2&gt;

&lt;p&gt;Authentication is the process of verifying requests to the server to find out if the user/client sending the request is a valid user. This can further be used to restrict user activities (authorization) or the resources that can be used by the particular user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Authentication
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Server Sessions&lt;/li&gt;
&lt;li&gt;Authentication Tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Server Sessions
&lt;/h3&gt;

&lt;p&gt;As the name suggests, in this method we create a session on the server for each incoming authentication request. In other words, when a user logs in to his/her account, we create a separate state on the server that only serves responses for that particular user.&lt;br&gt;
When an authentication request is received by the server, it validates the user credentials provided and creates a state on the server and provides a response to the client with a unique identifier for that state or session (usually in form of &lt;em&gt;cookies&lt;/em&gt;). This identifier is sent by client along with every request.&lt;br&gt;
This method of authentication is popular with MVC pattern of web development where the server serves the pages as response for user requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication Tokens
&lt;/h3&gt;

&lt;p&gt;In this method, whenever the server receives an authentication request, it validates the user credentials, generates a &lt;em&gt;unique id&lt;/em&gt; with some hashing algorithm and sends the &lt;em&gt;id&lt;/em&gt; back to the client. The client will use this id with every request sent to the server. &lt;br&gt;
This "id" is not encrypted. It is just hashed to include some data that only makes sense to the server that has to validate the request using this id.&lt;br&gt;
In this case, the server does not create or store a session. It is the client's responsibility to provide the proper id along with each and every request. Therefore, the server remains stateless.&lt;br&gt;
This form of authentication is very popular in Single Page Applications (SPAs) and more generally with REST APIs, since these APIs donot store state.&lt;/p&gt;

&lt;p&gt;I will discuss about how to use authentication in SPAs in my next post.&lt;br&gt;
Stay tuned. Happy Coding!!&lt;/p&gt;

</description>
      <category>authentication</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
