<?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: Karim Elghamry</title>
    <description>The latest articles on DEV Community by Karim Elghamry (@karimelghamry).</description>
    <link>https://dev.to/karimelghamry</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%2F428613%2Fb6fb35e9-adb7-43b1-b446-031dbadf817f.jpeg</url>
      <title>DEV Community: Karim Elghamry</title>
      <link>https://dev.to/karimelghamry</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karimelghamry"/>
    <language>en</language>
    <item>
      <title>Authentication and Authorization: A comprehensive guide</title>
      <dc:creator>Karim Elghamry</dc:creator>
      <pubDate>Sun, 19 Nov 2023 13:10:53 +0000</pubDate>
      <link>https://dev.to/karimelghamry/authentication-and-authorization-a-comprehensive-guide-47e9</link>
      <guid>https://dev.to/karimelghamry/authentication-and-authorization-a-comprehensive-guide-47e9</guid>
      <description>&lt;p&gt;Hello there 👋 In this article, we will go through the basics of Auth, outline the pros and cons of each type, and how to decide on an Auth solution based on your system requirements. &lt;/p&gt;



&lt;ul&gt;
&lt;li&gt;I- Basic Auth flow explained&lt;/li&gt;
&lt;li&gt;
II- Authentication

&lt;ul&gt;
&lt;li&gt;
Stateful Authentication

&lt;ul&gt;
&lt;li&gt;Pros&lt;/li&gt;
&lt;li&gt;Cons&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Stateless Authentication

&lt;ul&gt;
&lt;li&gt;Pros&lt;/li&gt;
&lt;li&gt;Cons&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

III- Authorization

&lt;ul&gt;
&lt;li&gt;
Role Based Access Control (RBAC)

&lt;ul&gt;
&lt;li&gt;Pros&lt;/li&gt;
&lt;li&gt;Cons&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Attribute Based Access Control (ABAC)

&lt;ul&gt;
&lt;li&gt;Pros&lt;/li&gt;
&lt;li&gt;Cons&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Discretionary Access Control (DAC)

&lt;ul&gt;
&lt;li&gt;Pros&lt;/li&gt;
&lt;li&gt;Cons&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

IV- How to choose your Auth solution

&lt;ul&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Authorization&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;V- Conclusion&lt;/li&gt;

&lt;/ul&gt;






&lt;h2&gt;
  
  
  I- Basic Auth flow explained
&lt;/h2&gt;

&lt;p&gt;To protect business resources in any system, there are two fundamental building layers that protect access to such resources: &lt;strong&gt;Authentication&lt;/strong&gt; and &lt;strong&gt;Authorization&lt;/strong&gt;. The following diagram illustrates how a basic Auth flow works:&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%2Fi6cuki6s6tu8j6iuqtao.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%2Fi6cuki6s6tu8j6iuqtao.png" alt="basic authentication and authorization flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authentication:&lt;/strong&gt; this layer is responsible for checking if the requesting user is &lt;strong&gt;part of the system&lt;/strong&gt;, nothing more nothing less. This can be done in endless ways, typically by providing a username and a password to verify the user's identity. If the user has been logged in previously, the authentication layer might have issued an "access token" for the user to use for subsequent requests to avoid sending the username/password with each request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authorization:&lt;/strong&gt; this layer is responsible for checking if the requesting user &lt;strong&gt;has access to the business resource&lt;/strong&gt;. Authorization assumes that the user has been already authenticated and is part of the system. There are many ways to authorize the user's permissions, which we will talk about later.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Essentially, on each client request, the request has to go through both layers &lt;strong&gt;in order&lt;/strong&gt; before granted access to the said resource. Let's dive deeper into how each of these layers work.&lt;/p&gt;




&lt;h2&gt;
  
  
  II- Authentication
&lt;/h2&gt;

&lt;p&gt;In essence, authentication can be classified into two different categories: &lt;strong&gt;Stateful&lt;/strong&gt; and &lt;strong&gt;Stateless&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stateful Authentication
&lt;/h3&gt;

&lt;p&gt;In this authentication model, once the user provides valid login credentials, the authentication layer creates a new session for the requesting user and stores the session data on the server side. Subsequently, the authentication layer returns a "Session ID" for the client to use for any subsequent request. Essentially, with each client request, the authentication layer fetches the session details for the corresponding session ID and validates it. The following diagram outlines a stateful authentication flow, assuming the user has been previously logged in:&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%2Fpwtyjry68ayi09wor4lj.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%2Fpwtyjry68ayi09wor4lj.png" alt="Stateful authentication flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server-side control:&lt;/strong&gt; a user session is entirely managed/controlled by the backend, which gives the ability for the admins to revoke access on demand. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easier to mitigate security breaches:&lt;/strong&gt; if a session ID was compromised by any means, it is easy to revoke the session from the server-side to deny access entirely.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Database read with each request:&lt;/strong&gt; since it is required to fetch the session data with each request, additional latency and complexity might be introduced to the system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt; as mentioned previously, each request maps to one database read operation, which accumulates additional operational costs. Assume we have thousands of requests coming into the system each minute, it would be complex to scale this solution with reasonable costs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Stateless Authentication
&lt;/h3&gt;

&lt;p&gt;Unlike stateful authentication, stateless authentication does not store any session data on the server side, and validates the user request on the fly. This is typically done using the following methodology; once the user provides valid login credentials, the authentication layer issues a signed "access token" (using a secret key only known to the server) and returns it to the client. The client then uses this token to make subsequent requests to the system. The token signature is validated on the fly by the server on each request, eliminating the need to store additional session data. The following diagram outlines a typical stateless authentication flow, assuming the user has been previously logged in:&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%2Fz3l4jssfarlgs388cxai.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%2Fz3l4jssfarlgs388cxai.png" alt="stateless authentication flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In essence, an access token may include the following data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;non-confidential user information&lt;/li&gt;
&lt;li&gt;token expiry date/time&lt;/li&gt;
&lt;li&gt;token signature and signing algorithm&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are various stateless authentication implementations out there, including JSON Web Token (JWT) and Platform Agnostic Security Tokens (PASETO).&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No session data stored:&lt;/strong&gt; an access token is validated on the fly, which saves us from storing any additional session data on the server side. This dramatically cuts the storage costs and does not introduce the database latency to our system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt; Eliminating the need for a database read on each request allows for better scalability, and enables you to integrate with third party identity providers in your system with ease.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No way to revoke access on demand:&lt;/strong&gt; the server does not keep any session information to be able to keep track of the currently logged in users. As long as the client provides a valid token, there's virtually no way to revoke the user's access to the protected resources, unless you change the token signature key. This makes it hard for admins to manage and monitor the currently logged in users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Potential security breaches:&lt;/strong&gt; if an access token of a certain user has been somehow leaked or compromised, there's no way you could revoke the user's access to the protected resources. One way to work around this issue is to set a short expiry date on the issued tokens, but that does not solve the problem entirely.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  III- Authorization
&lt;/h2&gt;

&lt;p&gt;As previously mentioned, authorization is the process of identifying the requesting user's permissions and validating whether the user has the required permissions to access the target business resource. Authorization comes in many shapes and forms, and we'll talk about a few of them in this section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Role Based Access Control (RBAC)
&lt;/h3&gt;

&lt;p&gt;Role based access control, or RBAC for short, is one of the most popular and versatile authorization frameworks that allows you to map system users to one or many organizational roles. In RBAC, you create a role, or a set of roles, that govern one or more granular permissions to guard access to resources. There are four main entities that are always present in an RBAC environment:&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%2F6ck7i9mfff1iswkst4zc.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%2F6ck7i9mfff1iswkst4zc.png" alt="RBAC entities"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User:&lt;/strong&gt; this is the entity that describes a unique user in a given system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Permission:&lt;/strong&gt; is the lowest level of access control that describes an action that can be done over a resource in a system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Role:&lt;/strong&gt; is an entity that governs a collection of permissions, in addition to other meta-data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Role assignment:&lt;/strong&gt; is an entity that describes the mapping between a system user and an organizational role. This may be present as a part of the issued access token, or explicitly defined in the system's database.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RBAC is a purely additive system, meaning that we assume that a user does not have any permissions to access resources unless explicitly defined in a role assignment. It is always a best practice to assign the minimum required permissions to a certain user to avoid any malicious activities in your system.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;re-usability:&lt;/strong&gt; RBAC allows you to define a role and assign it to as many as users as you want, increasing the role's resuability and modularity. For instance, if you want to give the bare minimum permissions to the new users in your system, you can create a single "guest" role that governs the common permissions for these users and assign it to them on demand.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ease of auditing and management:&lt;/strong&gt; RBAC allows admins to easily add, remove and change the users' permissions on demand by adding or removing the corresponding role assignments. It also makes it easy to tackle any security related issues, since each role assignment governs a set of well-defined roles.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simple computational model:&lt;/strong&gt; RBAC is only concerned with the fact that if the requesting user has the sufficient permissions to access the target resource or not. This increases your system scalability and heavily cuts down on the computation costs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Role explosions:&lt;/strong&gt; since RBAC allows you to create as many roles as you want, you can end up creating an infinite number of roles, possibly duplicates, which will inevitably clutter your system and may introduce orphan roles.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;God role:&lt;/strong&gt; if you're not highly cautious about the granularity and modularity of your roles, you might end up creating multiple "god roles" which contain a lot of uneeded permissions, introducing more security issues to your system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;RBAC is static:&lt;/strong&gt; RBAC does not care about the actual content or meta data (ex: creation date, number of records, etc.) of your business resources, which makes it hard to map between your organizational needs and the implemented RBAC roles.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Attribute Based Access Control (ABAC)
&lt;/h3&gt;

&lt;p&gt;rather than evaluating roles like RBAC, Attribute Based Access Control (ABAC) is a model that assesses attributes to grant access to the system resources. Attributes may include resource creation date, file owner, file ID, etc. ABAC runs a series of evaluation statements on the user's request to determine whether to allow access or not. All evaluation statements in an ABAC logic must return true in order to grant access to the target resource. There are four main entities that are present in an ABAC system:&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%2Frhsg5mucayrqyuqo8ly1.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%2Frhsg5mucayrqyuqo8ly1.png" alt="ABAC entities"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User:&lt;/strong&gt; this is the entity that describes and governs a set of attributes for a unique user in a given system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Resource:&lt;/strong&gt; an entity that holds the asset that the user wants to access. Resources may contain certain attributes such as creation date, owner, size, modification date, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Request/Action:&lt;/strong&gt; an entity that describes the intention of the user in a given system. For instance, a user may want &lt;em&gt;read&lt;/em&gt; content from a specific resource, or &lt;em&gt;write&lt;/em&gt; some data to another resource.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Environment:&lt;/strong&gt; an entity that describes the setting at which the request/action is taking place. An environment may contain certain attributes such as current date/time, system uptime, current number of concurrent users, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ABAC gives more control over the authorization process, and confines the auth logic in one place.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic authorization:&lt;/strong&gt; ABAC allows you to evaluate the user's access level at runtime, giving you more control over the authorization process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Encapsulated authorization logic:&lt;/strong&gt; ABAC allows you to confine most of the authorization logic in one place, making it easier for developers to append and maintain security rules.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fine grained access control:&lt;/strong&gt; since ABAC operates over attributes &amp;amp; characteristics, it makes it easier to map the user's access level to suit the organizational needs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Complex computations:&lt;/strong&gt; ABAC logic can get complex at times, which might introduce some latency into your system, as well as increase the operational costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hard to audit:&lt;/strong&gt; since ABAC is dynamic and encapsulates the auth logic behind a black box, it is hard for IT &amp;amp; admins to audit the permissions of each user in the system.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Discretionary Access Control (DAC)
&lt;/h3&gt;

&lt;p&gt;Discretionary Access Control, or DAC for short, is a simple model that determines access control via policies set by the resource's owner. Essentially, each resource has an owner, usually the initial creator, that has the power to determine the access level of other users, and has the ability to transfer ownership to other users. DAC is fairly simple to implement, and does not have a lot of complexity in its core. There are four main entities that are present in a DAC system:&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%2Fqip62ka6i860gz0eog9t.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%2Fqip62ka6i860gz0eog9t.png" alt="DAC entities"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User:&lt;/strong&gt; is an entity that describes a unique user in a given system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Owner:&lt;/strong&gt; is an entity that describes an owner of a specific resource.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Resource:&lt;/strong&gt; an entity that holds the asset that the user wants to access.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;access policies:&lt;/strong&gt; an entity that describes the rules that determines which user has access (read, write, etc.) to the resource, which is set by the owner.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easy to implement:&lt;/strong&gt; compared to RBAC and ABAC, DAC is really easy to implement and does not imply any complex computations under the hood.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimizes administration activities:&lt;/strong&gt; since access control is purely managed by resource owners, the administration workload is divided upon multiple users, making it easier to maintain the system.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No centralized management:&lt;/strong&gt; since each resource is managed by an owner, DAC implies a decentralized management approach by design. This might make it hard to audit and monitor the system from a security perspective.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Less secure:&lt;/strong&gt; since resource owners have the ability to give other users access, and as more owners are introduced to the system, data can be easily mishandled and possibly leaked outside of the organization.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  IV- How to choose your Auth solution
&lt;/h2&gt;

&lt;p&gt;when it comes to choosing an Auth solution for your application, it might be tricky to determine the best Auth combination based on your system's requirements. The following series of questions should help you decide on an Auth solution that best fits your needs.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Do you expect alot of concurrent users in your system?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; stateless&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; stateful&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Does your system require low latency and high throughput?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; stateless&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; stateful&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Do you want more control over the current user's sessions?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; stateful&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; stateless&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Do you care about the scalability of your system?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; stateless&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; stateful&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Does your system operate in an hostile environment where you need to revoke user access frequently?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; stateful&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; stateless&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Do you want to easily integrate with third party identity providers?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; stateless&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; stateful&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Do you care about the complexity of logic in your server?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; stateless&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; stateful&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Authorization
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Do you want to give your users the ability to manage their own resources?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; DAC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; ABAC or RBAC&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Does your system have complex access control requirements?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; ABAC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; RBAC or DAC&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Should your access control system have a similar hierarchy to your organizational roles?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; RBAC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; ABAC or DAC&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Do you want to be able to control access to resources based on runtime attributes?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; ABAC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; RBAC or DAC&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Do you want a simple access control solution that does not contain complex logic?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; RBAC or DAC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; ABAC&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Do you care about access control auditing and monitoring?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; RBAC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; DAC&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Do you have fixed and well defined permissions for each group of users?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;YES:&lt;/strong&gt; RBAC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NO:&lt;/strong&gt; ABAC or DAC&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  V- Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, We've gone through the basics of Auth, and how would one go about choosing the right Auth framework for their application. Auth is a huge topic in the security world and its concepts extend way beyond what has been mentioned in this article. Treat this article as a guide rather than a set of rules, and I encourage you to read more about these topics to further extend your knowledge. Thank you for reading thus far 😄&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>backend</category>
      <category>web</category>
    </item>
    <item>
      <title>Using RxJS/BLoC as a state management solution for React</title>
      <dc:creator>Karim Elghamry</dc:creator>
      <pubDate>Fri, 25 Nov 2022 12:21:01 +0000</pubDate>
      <link>https://dev.to/karimelghamry/using-rxjsbloc-as-a-state-management-solution-for-react-3cla</link>
      <guid>https://dev.to/karimelghamry/using-rxjsbloc-as-a-state-management-solution-for-react-3cla</guid>
      <description>&lt;p&gt;Hello fellow programmers 👋. in this article, we will discuss how we would use the &lt;a href="https://rxjs.dev/" rel="noopener noreferrer"&gt;RxJS&lt;/a&gt; library to implement the BLoC pattern as a state management solution for a React project. &lt;/p&gt;

&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
Prerequisite knowledge &lt;/li&gt;
&lt;li&gt;What is BLoC?&lt;/li&gt;
&lt;li&gt;
Creating a Todo app using BLoC/RxJS

&lt;ul&gt;
&lt;li&gt;App initialization&lt;/li&gt;
&lt;li&gt;Folder structure&lt;/li&gt;
&lt;li&gt;Creating the models&lt;/li&gt;
&lt;li&gt;Creating the BLoC's&lt;/li&gt;
&lt;li&gt;Creating the hook&lt;/li&gt;
&lt;li&gt;Creating the view&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Considerations and closing remarks&lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  Prerequisite knowledge &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Before going through the content of this article, it is assumed that the reader has prior basic knowledge in Rx, at least with observables and observers. The article will try to briefly go over these concepts, if needed.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is BLoC? &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;BLoC&lt;/strong&gt;, which stands for &lt;em&gt;Business Logic Component&lt;/em&gt;, is a pattern created by &lt;em&gt;Paolo Soares&lt;/em&gt; and &lt;em&gt;Cong Hui&lt;/em&gt; from Google that aims to separate the business logic from the view in a given application. One of its primary goals is to make the business logic &lt;strong&gt;reusable&lt;/strong&gt; and independent from the corresponding view. Initially, this pattern was introduced within the &lt;a href="https://flutter.dev/" rel="noopener noreferrer"&gt;Flutter&lt;/a&gt; community, but throughout this article, we will take the concepts of BLoC and implement them in a React application. To start with, BLoC has these main rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Each BLoC should have a &lt;code&gt;dispose()&lt;/code&gt; method to cleanup any ongoing streams.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Any data coming in a BLoC or any change in state within a BLoC must be done through a method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Any data coming out of a BLoC should be done through a stream/observable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The following diagram illustrates how the view and a BLoC interacts with each other.&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%2F6ikqrf44emkocxyo9zo9.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%2F6ikqrf44emkocxyo9zo9.png" alt="BLoC view" width="730" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Creating a Todo app using BLoC/RxJS &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Now that we've gone briefly over the concepts of the BLoC pattern, we will attempt to implement it in an example todo react application.&lt;/p&gt;

&lt;h2&gt;
  
  
  App initialization &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;First, lets initialize a react typescript project using &lt;code&gt;vite&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm create vite@latest todo-bloc &lt;span class="nt"&gt;--template&lt;/span&gt; react-ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this should create a folder called &lt;code&gt;todo-bloc&lt;/code&gt;. Navigate to that folder and install the dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;todo-bloc &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;once done, we'll proceed by installing the &lt;a href="https://www.npmjs.com/package/rxjs" rel="noopener noreferrer"&gt;RxJS&lt;/a&gt; package and the &lt;a href="https://www.npmjs.com/package/uuid" rel="noopener noreferrer"&gt;uuid&lt;/a&gt; package (we'll need it later):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;rxjs uuid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can now run the app in dev mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Folder structure &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In our simple todo app, we will create the following sub directories inside the &lt;code&gt;src&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;src/
 ├───blocs/
 ├───components/
 ├───hooks/
 └───models/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;blocs:&lt;/strong&gt; will encapsulate all of the reusable business logic components in our app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;components:&lt;/strong&gt; will govern the view (UI) react components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;hooks:&lt;/strong&gt; will hold all of our custom hooks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;models:&lt;/strong&gt; will contains the interfaces and abstract classes that are shared across the app.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating the models &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;To simulate a real world application, we will start first by creating a &lt;code&gt;Todo&lt;/code&gt; interface, which all of the todo objects in our app will conform to. We'll create a &lt;code&gt;Todo.ts&lt;/code&gt; file under the models directory and add the following interface:&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%2F9dzz30ehs81gjsgqvfjy.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%2F9dzz30ehs81gjsgqvfjy.png" alt="code" width="748" height="676"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt;: to distinguish between different todo objects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;text&lt;/code&gt;: the actual content of the todo object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isDone&lt;/code&gt;: to keep track of whether this specific todo is marked as done or not.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Secondly, we'll create the base &lt;code&gt;bloc&lt;/code&gt; interface which will include all common methods/properties shared between the BLoC's in our app. Ultimately, we'll only need a &lt;code&gt;dispose()&lt;/code&gt; method on the &lt;code&gt;bloc&lt;/code&gt; interface as previously mentioned. We'll proceed by creating a &lt;code&gt;Bloc.ts&lt;/code&gt; file under the models directory and adding the following interface:&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%2Fn9kei0ntty48ne8y8noy.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%2Fn9kei0ntty48ne8y8noy.png" alt="bloc" width="748" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dispose()&lt;/code&gt;: a method invoked to close any opened streams and complete all observables within our BLoC.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating the BLoC's &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we have the interfaces in place, lets start by defining our business requirements for our app. Essentially, our app should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;display todo items chronologically.&lt;/li&gt;
&lt;li&gt;provide a way to add a todo item.&lt;/li&gt;
&lt;li&gt;provide a way to mark todo items as finished.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First off, we'll create a &lt;code&gt;TodoBloc&lt;/code&gt; class that will implement the &lt;code&gt;Bloc&lt;/code&gt; interface that was previously imposed. Ultimately, this class will live in a file called &lt;code&gt;TodoBloc.ts&lt;/code&gt; under the blocs directory. To implement the first business requirement, we'll have to find a way to keep track of the current list of todo items. This is where &lt;code&gt;Subject&lt;/code&gt; from &lt;code&gt;RxJS&lt;/code&gt; comes into play. To recall, any data coming out of a BLoC should be through a stream/observable. To achieve that, we will create a &lt;code&gt;BehaviourSubject&lt;/code&gt; that will hold the most recent list of todos, and emit new items whenever the state changes:&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%2Foq0h6pqg7zgsw8pq3swu.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%2Foq0h6pqg7zgsw8pq3swu.png" alt="todo" width="800" height="637"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;_todos$&lt;/code&gt;: is a private property of type &lt;code&gt;BehaviorSubject&amp;lt;Todo[]&amp;gt;&lt;/code&gt; that will be responsible for holding our &lt;code&gt;Todo&lt;/code&gt; objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;dispose()&lt;/code&gt;: we close/cleanup the &lt;code&gt;_todos$&lt;/code&gt; subject in the previously defined dispose method.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;you might now be wondering, how are we going to expose the &lt;code&gt;_todos$&lt;/code&gt; subject to the view if the field is marked as private? good question. This is a bit tricky, because we don't want to expose the whole &lt;code&gt;Subject&lt;/code&gt; to the view as we don't want the view to be emitting data directly to the Subject (we have to do it through a method as previously mentioned). Instead, we will create a getter for the &lt;code&gt;_todos$&lt;/code&gt; field that will &lt;strong&gt;only&lt;/strong&gt; expose the &lt;code&gt;Observable&lt;/code&gt; side of our subject:&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%2Ffx8haliwhrlrcv6q92i5.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%2Ffx8haliwhrlrcv6q92i5.png" alt="getter" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To cover the second business requirement, we will create a method called &lt;code&gt;addTodo&lt;/code&gt;, which will be responsible for taking in a &lt;code&gt;Todo&lt;/code&gt; object and adding it to the &lt;code&gt;_todos$&lt;/code&gt; subject:&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%2F0gto6eanuy29aquii45m.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%2F0gto6eanuy29aquii45m.png" alt="addTodo" width="800" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; the previous method accepts a &lt;code&gt;Todo&lt;/code&gt; object as a param, gets the current list of todos from the &lt;code&gt;_todos$&lt;/code&gt; subject, concatenates the given todo to the list of todos, and finally pushes the new list of todos to the &lt;code&gt;_todos$&lt;/code&gt; subject.&lt;/p&gt;

&lt;p&gt;To implement the final business requirement, we will add a &lt;code&gt;toggleTodo&lt;/code&gt; method, which will be responsible for marking the given &lt;code&gt;Todo&lt;/code&gt; item as done/undone:&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%2Fs1juxus7lxgdp04il6sd.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%2Fs1juxus7lxgdp04il6sd.png" alt="toggle" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; &lt;code&gt;toggleTodo&lt;/code&gt; accepts a &lt;code&gt;Todo&lt;/code&gt; object as a param, gets the current list of todos from the &lt;code&gt;_todos$&lt;/code&gt; subject, loops over the retrieved list and toggles the given &lt;code&gt;Todo&lt;/code&gt; item by id, and finally pushes the new todo list to the &lt;code&gt;_todo$&lt;/code&gt; subject.&lt;/p&gt;

&lt;p&gt;The final &lt;code&gt;TodoBloc&lt;/code&gt; class should look like this:&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%2F072i3ijt730q3tt5pd8e.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%2F072i3ijt730q3tt5pd8e.png" alt="Todo Bloc" width="800" height="737"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the hook &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we're done implementing our business logic, we need to start thinking of a way to connect our BLoC to the anticipated view in our app. We can facilitate this process by creating a custom hook that will have the following responsibilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;accept an &lt;code&gt;Observable&lt;/code&gt; of a generic type as a param.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;consume the values coming from the &lt;code&gt;Observable&lt;/code&gt; by subscribing to it when the components mounts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;preserve the state of the most recent value from the &lt;code&gt;Observable&lt;/code&gt; and update the state each time a new value comes in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cleanup the subscription when the component unmounts.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Having these points in mind, we'll create a &lt;code&gt;useSubscribe&lt;/code&gt; hook under the hooks directory to fulfill these responsibilities:&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%2Frk27458a1o313qdxjt4p.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%2Frk27458a1o313qdxjt4p.png" alt="hook" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; &lt;code&gt;useSubscribe&lt;/code&gt; accepts an &lt;code&gt;Observable&lt;/code&gt; of a generic type as a param. A state is created to store the most recent value emitted from the given observable using the &lt;code&gt;useState&lt;/code&gt; hook. We then use the &lt;code&gt;useEffect&lt;/code&gt; hook to subscribe to the given observable on component mount and update the state with the most recent value. We also add a cleanup callback that will unsubscribe from the observable once the components unmounts. Finally, we return a copy of the state to the view to be able to effectively use it in display.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the view &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we have everything in place, let us create the view to demonstrate how we can tie these all pieces together. First, we create a simple &lt;code&gt;TodoItem&lt;/code&gt; component under our components directory to display a single todo item:&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%2Fnub86fe4q5bad6tmsncc.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%2Fnub86fe4q5bad6tmsncc.png" alt="todo view" width="800" height="671"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; the &lt;code&gt;TodoItem&lt;/code&gt; component accepts two params - &lt;code&gt;todo&lt;/code&gt;, which is the given &lt;code&gt;Todo&lt;/code&gt; object, and &lt;code&gt;onToggle&lt;/code&gt; callback, which will be triggered when the user marks the items as done/undone. The component displays the text property of the given todo in a &lt;code&gt;li&lt;/code&gt; tag, and style is rendered according to the &lt;code&gt;isDone&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;Finally, in our &lt;code&gt;App.tsx&lt;/code&gt; file, we will tie all the pieces together:&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%2Fgphqyqd7o5hrnv41tb6s.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%2Fgphqyqd7o5hrnv41tb6s.png" alt="view" width="800" height="1025"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; we create an instance of the &lt;code&gt;TodoBloc&lt;/code&gt; outside of our &lt;code&gt;App&lt;/code&gt; component. We then consume the most recent value from the the &lt;code&gt;TodoBloc&lt;/code&gt; by using the previously defined &lt;code&gt;useSubscribe&lt;/code&gt; hook and passing the &lt;code&gt;todos$&lt;/code&gt; observable as a param. Now, the &lt;code&gt;todo&lt;/code&gt; variable will always hold the most recent list of todos, and will update accordingly. To give the user the ability to add new todo items, we create a simple form with a single controlled &lt;code&gt;input&lt;/code&gt; element and a submit button. On submit, the &lt;code&gt;addTodo&lt;/code&gt; method on the &lt;code&gt;TodoBloc&lt;/code&gt; will be invoked, and a new &lt;code&gt;Todo&lt;/code&gt; object will be pushed to the list of todos. Finally, to display the list of todo items, we map over the todos list and render a &lt;code&gt;TodoItem&lt;/code&gt; component for each given todo. Each &lt;code&gt;TodoItem&lt;/code&gt; will be provided an &lt;code&gt;onToggle&lt;/code&gt; callback that will invoke the &lt;code&gt;toggleTodo&lt;/code&gt; method on the &lt;code&gt;TodoBloc&lt;/code&gt; for each item, respectively.&lt;/p&gt;

&lt;p&gt;The final result will look/behave as follows:&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%2Fly32ayd8khrwi2m7n0ol.gif" 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%2Fly32ayd8khrwi2m7n0ol.gif" alt="demo" width="489" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Considerations and closing remarks &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;As your application grows, your number of BLoC's will grow respectively and you'll need to find a way to manage them cleanly. Multiple view components may consume a single BLoC and you'll need to find a way to provide it to these components. One way to go about that is to use the &lt;a href="https://beta.reactjs.org/apis/react/useContext" rel="noopener noreferrer"&gt;context api&lt;/a&gt; to provide a single bloc to multiple components. You may also want to look into the &lt;a href="https://refactoring.guru/design-patterns/singleton" rel="noopener noreferrer"&gt;Singleton design pattern&lt;/a&gt; to preserve a single copy of a given BLoC within your app. You would also want to manage the disposal of each BLoC when it is no longer needed in your app (in our todo app, we did not call &lt;code&gt;dispose()&lt;/code&gt; anywhere because the &lt;code&gt;TodoBloc&lt;/code&gt; has the same lifespan as our application). Nevertheless, be flexible and reasonable with your design and do not over engineer your solutions. Happy coding 🎉!&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Bash Basics: useful commands and tools</title>
      <dc:creator>Karim Elghamry</dc:creator>
      <pubDate>Fri, 18 Nov 2022 10:38:38 +0000</pubDate>
      <link>https://dev.to/karimelghamry/bash-basics-useful-commands-and-tools-2po4</link>
      <guid>https://dev.to/karimelghamry/bash-basics-useful-commands-and-tools-2po4</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Hello fellow programmers 👋 In this article, I will shed the light on some useful/basic &lt;strong&gt;bash&lt;/strong&gt; commands and tools that might help you save time while scripting. To make it easy to follow, the article will adhere to the following format:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;command&lt;/li&gt;
&lt;li&gt;description&lt;/li&gt;
&lt;li&gt;format&lt;/li&gt;
&lt;li&gt;examples&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Commands&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. &lt;code&gt;man&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; &lt;code&gt;man&lt;/code&gt; is a useful command that displays the documentation/user manual to any command that you pass as an argument.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ man [COMMAND]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets try to display the manual for the &lt;code&gt;echo&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;man &lt;span class="nb"&gt;echo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the output of the previous line is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ECHO&lt;span class="o"&gt;(&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;                          User Commands                         ECHO&lt;span class="o"&gt;(&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;                                                                                                                

NAME                                                                                                                                                                                          
       &lt;span class="nb"&gt;echo&lt;/span&gt; - display a line of text                                                                                                                                                          

SYNOPSIS                                                                                                                                                                                      
       &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;SHORT-OPTION]... &lt;span class="o"&gt;[&lt;/span&gt;STRING]...                                                                                                                                                     
       &lt;span class="nb"&gt;echo &lt;/span&gt;LONG-OPTION                                                                                                                                                                       

DESCRIPTION                                                                                                                                                                                   
       Echo the STRING&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; to standard output.                                                                                                                                                 

       &lt;span class="nt"&gt;-n&lt;/span&gt;     &lt;span class="k"&gt;do &lt;/span&gt;not output the trailing newline                                                                                                                                              

       &lt;span class="nt"&gt;-e&lt;/span&gt;     &lt;span class="nb"&gt;enable &lt;/span&gt;interpretation of backslash escapes                                                                                                                                      

       &lt;span class="nt"&gt;-E&lt;/span&gt;     disable interpretation of backslash escapes &lt;span class="o"&gt;(&lt;/span&gt;default&lt;span class="o"&gt;)&lt;/span&gt;                                                                                                                           

       &lt;span class="nt"&gt;--help&lt;/span&gt; display this &lt;span class="nb"&gt;help &lt;/span&gt;and &lt;span class="nb"&gt;exit&lt;/span&gt;                                                                                                                                                      

       &lt;span class="nt"&gt;--version&lt;/span&gt;                                                                                                                                                                              
              output version information and &lt;span class="nb"&gt;exit&lt;/span&gt;        
...                                                                                                                                     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the &lt;code&gt;man&lt;/code&gt; command displays the output in a formatted manner. The output usually contains the name, description and the available options/flags for the given command.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fun fact:&lt;/em&gt; you can use the &lt;code&gt;man&lt;/code&gt; command to display the manual for the &lt;code&gt;man&lt;/code&gt; command.&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%2Fi.redd.it%2F9fppkjvr6oq51.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%2Fi.redd.it%2F9fppkjvr6oq51.png" alt="man man"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. &lt;code&gt;tldr&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; &lt;code&gt;tldr&lt;/code&gt; is a community driven tool that enhances the output of the &lt;code&gt;man pages&lt;/code&gt; and displays brief and meaningful examples for the given command. This tool is not available by default, so you will have to install it first. You can visit the tool's &lt;a href="https://tldr.sh/" rel="noopener noreferrer"&gt;official page&lt;/a&gt; to easily add it to your environment.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tldr [COMMAND]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets try to display the &lt;code&gt;tldr&lt;/code&gt; formatted docs for the &lt;code&gt;echo&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;tldr &lt;span class="nb"&gt;echo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the output of the previous line is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo

&lt;/span&gt;Print given arguments.

Print a text message. Note: quotes are optional:

    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"{{Hello World}}"&lt;/span&gt;

Print a message with environment variables:

    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"{{My path is &lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="s2"&gt;}}"&lt;/span&gt;

Print a message without the trailing newline:

    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="s2"&gt;"{{Hello World}}"&lt;/span&gt;

Append a message to the file:

    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"{{Hello World}}"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{{&lt;/span&gt;file.txt&lt;span class="o"&gt;}}&lt;/span&gt;

Enable interpretation of backslash escapes &lt;span class="o"&gt;(&lt;/span&gt;special characters&lt;span class="o"&gt;)&lt;/span&gt;:

    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"{{Column 1&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;Column 2}}"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Note how it gives concise examples to illustrate the usage of the given command, unlike &lt;code&gt;man&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similar to &lt;code&gt;man&lt;/code&gt;, you can use the &lt;code&gt;tldr&lt;/code&gt; command to display the formatted docs for &lt;code&gt;tldr&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. &lt;code&gt;cat&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; &lt;code&gt;cat&lt;/code&gt; is an awesome command that lets you display files in the standard output, concatenate files together or create new files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat [OPTION]... [FILE]...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suppose that we want to print out the content of a file named &lt;em&gt;example&lt;/em&gt; to the standard output. It can be done using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to append two files, named &lt;em&gt;foo&lt;/em&gt; and &lt;em&gt;bar&lt;/em&gt; respectively, and save the output in a file called &lt;em&gt;output&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;foo bar &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create a new file called &lt;em&gt;new_file&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; new_file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;who doesn't like cats?&lt;/em&gt;&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%2Fwww.virtmann.com%2Fcontent%2Fimages%2Fsize%2Fw2000%2F2017%2F11%2Fbash1.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%2Fwww.virtmann.com%2Fcontent%2Fimages%2Fsize%2Fw2000%2F2017%2F11%2Fbash1.png" alt="bash cat"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;4. &lt;code&gt;grep&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; &lt;code&gt;grep&lt;/code&gt; is a powerful tool that lets you match patterns (&lt;strong&gt;using regex&lt;/strong&gt;) or exact strings in a given text. It prints out the lines that contain the given pattern/string.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ grep [OPTION...] PATTERNS [FILE...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suppose that I have a python file called &lt;em&gt;stock_analysis.py&lt;/em&gt; and it contains the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;matplotlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;

&lt;span class="n"&gt;eminem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://www.google.com/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;my_file.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and I want to display all the &lt;code&gt;imports&lt;/code&gt; included in that file. We can simply use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt; &lt;span class="s2"&gt;"import"&lt;/span&gt; stock_analysis.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the output of the previous command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;matplotlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose that instead of displaying all the &lt;em&gt;imports&lt;/em&gt;, We want to display the &lt;em&gt;aliased imports&lt;/em&gt; &lt;strong&gt;only&lt;/strong&gt;. We can use the &lt;code&gt;grep&lt;/code&gt; command along with regex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;grep&lt;/span&gt;  &lt;span class="s2"&gt;"import.*as.*"&lt;/span&gt; stock_analysis.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the output of the previous command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;matplotlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;5. &lt;code&gt;curl&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; &lt;code&gt;curl&lt;/code&gt; is a cli tool that lets you transfer data from/to a server over various protocols such as HTTP and FTP. &lt;code&gt;curl&lt;/code&gt; is a massive tool that has lots of powerful features and is open source, so I encourage you to read more about it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl [OPTION...] [URL...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suppose that I want to retrieve a random dad joke from the &lt;a href="https://icanhazdadjoke.com/api" rel="noopener noreferrer"&gt;&lt;em&gt;icanhazdadjoke&lt;/em&gt;&lt;/a&gt; API using &lt;code&gt;curl&lt;/code&gt;. I would send a &lt;code&gt;GET&lt;/code&gt; request and specify that I want to only accept &lt;em&gt;text/plain&lt;/em&gt; data in the header:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; GET https://icanhazdadjoke.com/ &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Accept: text/plain"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this was the joke that I've received:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Is the pool safe for diving? It deep ends.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this API is a lot of fun and I encourage you to try your luck with it!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6. &lt;code&gt;|&lt;/code&gt; - pipes&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; &lt;code&gt;|&lt;/code&gt;, or shall I say, pipes let you use the output of a program as an input to another program. A series of bash commands connected together using pipes is usually called a &lt;strong&gt;pipeline&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ [COMMAND1] | [COMMAND2] | ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suppose that we want to retrieve all Eminem songs that contains the word "love". We would first use &lt;code&gt;curl&lt;/code&gt; to retrieve the list of songs from the internet, &lt;strong&gt;then&lt;/strong&gt; use the output from the request as an input to the &lt;code&gt;grep&lt;/code&gt; command to search for exact matches of the word "love":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; GET https://pastebin.com/raw/6nfK1QRb | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s2"&gt;"love"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the final filtered list of songs is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;14. Never Love Again$
14 M-bM-^@M-^T Love Game (feat. Kendrick Lamar)$
Rihanna M-bM-^@M-^S Love The Way You Lie Part II (feat. Eminem)$
09 M-bM-^@M-^S No Love (feat. Lil Wayne)$
15 M-bM-^@M-^S Love the Way You Lie (feat. Rihanna)$
17 M-bM-^@M-^S Crazy in Love$
02 M-bM-^@M-^S I Love You More$
03 M-bM-^@M-^S I Love You More (Original)$
02 M-bM-^@M-^S Love Me M-bM-^@M-^S Obie Trice, Eminem &amp;amp; 50 Cent$
Eminem M-bM-^@M-^S Thug Love (feat. 50 Cent &amp;amp; Beyonce) (Remix)$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;7. &lt;code&gt;!&lt;/code&gt; - bang&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; &lt;code&gt;!&lt;/code&gt;, or shall I say, bang is a useful command that lets you execute and play around with bash commands history. Combined with other characters and keywords, It can be extremely time efficient to use it instead of retyping commands from the bash history.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt; &lt;span class="c"&gt;# execute the last executed command&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="c"&gt;# execute the last command except for the last argument&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# get the last argument of the last executed command&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="o"&gt;![&lt;/span&gt;PATTERN] &lt;span class="c"&gt;# execute the the first command that matches the given pattern from the bash history&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;8. &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;tail&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;tail&lt;/code&gt; are two useful bash commands that lets you display a portion of a given file. The former is used to display the first &lt;code&gt;n&lt;/code&gt; lines of a file, and the latter is used to display the last &lt;code&gt;n&lt;/code&gt; lines of a file. By default, &lt;code&gt;n&lt;/code&gt; is equal to &lt;code&gt;10&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ head [OPTION]... [FILE]...
$ tail [OPTION]... [FILE]...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dump the first and last 20 lines of a file called &lt;code&gt;main.js&lt;/code&gt; in a file called &lt;code&gt;example&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;--lines&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;20 main.js &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; example
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;--lines&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;20 main.js &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;9. &lt;code&gt;fzf&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; &lt;code&gt;fzf&lt;/code&gt; is an awesome cli tool for performing fuzzy search on your files or stdout through the command line. Honestly, &lt;code&gt;fzf&lt;/code&gt; is a really powerful tool that can do alot, but in this article, we will focus on two main usages for this tool: searching for files and search for content from the stdout. You can read more about &lt;code&gt;fzf&lt;/code&gt; through their &lt;a href="https://github.com/junegunn/fzf#using-the-finder" rel="noopener noreferrer"&gt;github page&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ fzf [OPTION]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By default, running the &lt;code&gt;fzf&lt;/code&gt; command without any options will open an interactive searching session within the current directory. The following gif shows how we could fuzzy search for files in the current directory:&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%2F5jdpxugy3nnr1ra79pi9.gif" 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%2F5jdpxugy3nnr1ra79pi9.gif" alt="fzf"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also pipe the stdout to &lt;code&gt;fzf&lt;/code&gt; and search for content within the provided context:&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%2Ffhghisq5jn83m84zgrrz.gif" 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%2Ffhghisq5jn83m84zgrrz.gif" alt="fzf"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing Remarks
&lt;/h2&gt;

&lt;p&gt;Knowing your way around bash commands and tools can accelerate your productivity immensely. This article has covered some of the useful ones the I use on a daily basis. Feel free to mention your favorite bash commands/tools in the comments. Nevertheless, Have a great day🎉.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>linux</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Generators in Javascript: How to use them</title>
      <dc:creator>Karim Elghamry</dc:creator>
      <pubDate>Fri, 05 Feb 2021 14:04:54 +0000</pubDate>
      <link>https://dev.to/karimelghamry/generators-in-javascript-how-to-use-them-372d</link>
      <guid>https://dev.to/karimelghamry/generators-in-javascript-how-to-use-them-372d</guid>
      <description>&lt;h1&gt;
  
  
  Opening Note
&lt;/h1&gt;

&lt;p&gt;Hello fellow programmers 👋 In this article, we will walk-through the basics of generators in Javascript, which was introduced in ES6, and get across some real use cases.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a generator?
&lt;/h1&gt;

&lt;p&gt;From its name, a generator is a &lt;strong&gt;function&lt;/strong&gt; that allows you to &lt;strong&gt;generate&lt;/strong&gt; one or more values by exiting and re-entering the execution procedure whilst saving its state (context) across multiple calls. To put it in simpler words, a generator is similar to normal functions, but has the ability to continue execution on demand at the point at which it was previously terminated, simply by saving its previous state. The following flowchart illustrates the difference between a normal function and a generator function.&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%2Fi%2Fwmj54httcqn0v71kpe7l.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%2Fi%2Fwmj54httcqn0v71kpe7l.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;As you've already guessed, there are some syntactic differences between a normal function and a generator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Normal Function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;normalFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// your logic goes here&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* --------------------------------- */&lt;/span&gt;

&lt;span class="c1"&gt;// Generator Function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;generatorFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// your logic&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;value1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// your logic&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;value2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="cm"&gt;/*
    .
    .
    .
  */&lt;/span&gt;

  &lt;span class="c1"&gt;// your logic&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;valueN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The first noticeable difference in syntax is that a generator is declared using the &lt;code&gt;function*&lt;/code&gt; keyword instead of &lt;code&gt;function&lt;/code&gt;. Also, notice how we use the &lt;code&gt;return&lt;/code&gt; keyword in a normal function, while we use the &lt;code&gt;yield&lt;/code&gt; keyword in a generator function instead, respectively. The &lt;code&gt;yield&lt;/code&gt; keyword inside the generator allows us to 'return' a value, terminate execution, save the state (context) of the current lexical scope and waits for the next invocation to resume execution at the last termination point. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; In a normal function, you can only execute the &lt;code&gt;return&lt;/code&gt; keyword once, which will return a value and terminate the function completely. In a generator, you can use the &lt;code&gt;yield&lt;/code&gt; keyword multiple times as much as you want to 'return' values on consecutive calls. You can also use the &lt;code&gt;return&lt;/code&gt; keyword inside a generator, but leave this discussion for a different day.&lt;/p&gt;

&lt;h2&gt;
  
  
  Invocation
&lt;/h2&gt;

&lt;p&gt;Now that we've covered the differences in syntax between both functions, let us see how does one invoke a generator and yield its values. First, consider the following piece of code which illustrates the invocation of a normal function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;normalFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I have been invoked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// invocation&lt;/span&gt;
&lt;span class="nf"&gt;normalFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In general, you can invoke a normal function by typing the function's signature followed by a pair of parentheses &lt;code&gt;()&lt;/code&gt;. The previous code will output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;have&lt;/span&gt; &lt;span class="nx"&gt;been&lt;/span&gt; &lt;span class="nx"&gt;invoked&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's try to use the same procedure to invoke a generator. Inspect the following piece of code closely:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;generatorFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I have been invoked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resuming execution&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// does this invoke the generator?&lt;/span&gt;
&lt;span class="nf"&gt;generatorFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;What do you expect from such program? Technically, we would expect that the function is to be executed until it hits the first yield keyword. However, the output of the previous program was empty:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;that is because the normal invocation syntax &lt;strong&gt;does not&lt;/strong&gt; actually execute the body of the generator function. Instead, it creates a &lt;code&gt;Generator&lt;/code&gt; Object that holds multiple properties and methods. To prove this, we can try to print out &lt;code&gt;console.log(generatorFunction())&lt;/code&gt; and the output should be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Generator&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the question is; &lt;strong&gt;how do we actually yield our values from a generator?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;well, there are some important methods that belong to the &lt;code&gt;Generator&lt;/code&gt; Object that we can utilize. The first and the most important method is called &lt;code&gt;next()&lt;/code&gt;, which, from its name, yields the next value from the defined generator. Now lets modify our previous code to actually yield our values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;generatorFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I have been invoked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resuming execution&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// store the Generator Object in a variable&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generatorFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// execute until we yield the first value&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// resume execution until we yield the second value&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// execute until the function ends&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;the output of the previous code is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;have&lt;/span&gt; &lt;span class="nx"&gt;been&lt;/span&gt; &lt;span class="nx"&gt;invoked&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;resuming&lt;/span&gt; &lt;span class="nx"&gt;execution&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Let's inspect the output line by line. When calling the first &lt;code&gt;foo.next()&lt;/code&gt; method, the generator began to execute until it hit the first yield keyword and stops the execution. This is reflected in the first two lines of the output. Notice how the &lt;code&gt;foo.next()&lt;/code&gt; returned an &lt;code&gt;Object&lt;/code&gt; instead of the actual yielded value. This Object should always contain the following properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;'value': which holds the current yielded value from the generator.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;'done': a boolean flag which indicates whether the generator execution has reached the end or not.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets move on to the second &lt;code&gt;foo.next()&lt;/code&gt; call. As expected, the generator resumes the execution from the last termination step and executes until it hits the second yield keyword, which is reflected in the third and fourth lines of the output. Notice how the &lt;code&gt;done&lt;/code&gt; flag is still set by &lt;code&gt;false&lt;/code&gt;, as it did not yet reach the end of the function.&lt;/p&gt;

&lt;p&gt;On the last &lt;code&gt;foo.next()&lt;/code&gt; call, the function resumes execution after the second yield keyword and finds nothing to execute, which indicates that we've reached the end of the function. At this point, there are no more values to yield and the &lt;code&gt;done&lt;/code&gt; flag is set to &lt;code&gt;true&lt;/code&gt; as reflected in the last line of the output.&lt;/p&gt;

&lt;p&gt;Now that we've covered the basic concepts of generators in Javascript, lets take a look at some of its useful use cases.&lt;/p&gt;

&lt;h1&gt;
  
  
  Use Cases
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Use case 1: Mimic the &lt;code&gt;range()&lt;/code&gt; function from Python
&lt;/h2&gt;

&lt;p&gt;According to the Python docs, "the &lt;code&gt;range&lt;/code&gt; type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops." The &lt;code&gt;range()&lt;/code&gt; function in Python usually contains the following parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;start&lt;/code&gt; (optional, default = 0): the first number in the sequence, inclusive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;end&lt;/code&gt; (required): the last number of the sequence, exclusive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;step&lt;/code&gt; (optional, default = 1): the difference between any two given numbers in the sequence.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basically, the usage of the &lt;code&gt;range()&lt;/code&gt; function in Python is shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Python code
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="nf"&gt;range&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# output:
# 0
# 1
# 2
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;what we need to do is to mimic this functionality in Javascript using generators. Inspect the following piece of code closely:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
range function implemented in Javascript
*/&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets take it step by step. Firstly, the function signature defines a generator that takes three params: &lt;code&gt;start&lt;/code&gt;, &lt;code&gt;end&lt;/code&gt; and &lt;code&gt;step&lt;/code&gt;, in which &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;step&lt;/code&gt; are defaulted to &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; respectively. Moving onto the function body, it contains a basic for loop that starts iterating from &lt;code&gt;start&lt;/code&gt; inclusive till &lt;code&gt;end&lt;/code&gt; exclusive. Inside the loop's scope, we yield the value &lt;code&gt;i&lt;/code&gt; of the current number in the sequence.&lt;/p&gt;

&lt;p&gt;Lets see it in action. The following piece of code illustrates different examples of the implemented &lt;code&gt;range&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// first example&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/*
output:
0
1
2
3
*/&lt;/span&gt;

&lt;span class="c1"&gt;// second example&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;start&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="na"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/*
output:
2
3
*/&lt;/span&gt;

&lt;span class="c1"&gt;// third example&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;step&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/*
output:
1
3
5
7
*/&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use case 2: Visualize the Bubble Sort algorithm
&lt;/h2&gt;

&lt;p&gt;In this use case, we will attempt to output a step-by-step execution of the Bubble Sort algorithm on a given array to easily visualize it. Briefly, bubble sort works as follows; given an array of length &lt;code&gt;n&lt;/code&gt; and &lt;code&gt;i&lt;/code&gt; as the current iteration, propagate the &lt;code&gt;max(array[0:n - i])&lt;/code&gt; to the index &lt;code&gt;n - i&lt;/code&gt; repeatedly until the array is sorted. The default implementation is shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
Bubble Sort implementation in javascript
*/&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bubbleSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&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="c1"&gt;// if the current value is larger than its adjacent&lt;/span&gt;
      &lt;span class="c1"&gt;// swap them together&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&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="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our job is to visualize the step-by-step comparisons and swaps that are carried out throughout this algorithm. This can be easily done using generators. We simply yield the current array after each iteration in the inner loop. The new function will be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
visualize Bubble Sort implementation in javascript
*/&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;visualizeBubbleSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;arr&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will yield the array for each iteration in the inner loop, showing us the current state of the array. Consider the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;inputArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&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;20&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentStep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;visualizeBubbleSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputArray&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`step #&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;currentStep&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;: [&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;]`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;currentStep&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the previous program will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;40&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;20&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;30&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;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;30&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;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&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;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&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;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&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;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can clearly see what is happening throughout the algorithm thanks to the implemented generator:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;step 1 -&amp;gt; swap &lt;code&gt;40&lt;/code&gt; with &lt;code&gt;30&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;step 2 -&amp;gt; swap &lt;code&gt;40&lt;/code&gt; with &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;step 3 -&amp;gt; swap &lt;code&gt;40&lt;/code&gt; with &lt;code&gt;20&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;step 4 -&amp;gt; swap &lt;code&gt;30&lt;/code&gt; with &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;step 5 -&amp;gt; swap &lt;code&gt;30&lt;/code&gt; with &lt;code&gt;20&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;step 6 -&amp;gt; do not swap anything, array is sorted&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; this technique can be used to visualize any given algorithm easily. It can be very helpful sometimes. &lt;/p&gt;

&lt;h2&gt;
  
  
  Use case 3: Generate distinct random numbers on demand
&lt;/h2&gt;

&lt;p&gt;In this use case, we will try to generate a series of &lt;strong&gt;distinct&lt;/strong&gt; random numbers using generators. First, we would put some constraints on the inputs and outputs as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The function should only generate &lt;strong&gt;positive integers.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The function should take a parameter &lt;code&gt;limit&lt;/code&gt;, which determines the maximum number of generated integers as well as the largest possible generated integer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The function should have a way to store the valid pool of integers to choose from.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following the previous constrains carefully, we can implement this functionality using generators easily:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
distinctRandom implementation in js 
*/&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;distinctRandom&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// we create an array that contains all numbers in range [0:limit)&lt;/span&gt;
  &lt;span class="c1"&gt;// this is our initial pool of numbers to choose from&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;availableValues&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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;index&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// we repeatedly loop until the available pool of numbers is empty&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;availableValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// generate a random index in range [0: availableValues.length)&lt;/span&gt;
    &lt;span class="c1"&gt;// then, yield the number that is present at the chosen index&lt;/span&gt;
    &lt;span class="c1"&gt;// Finally, remove the picked item from the pool of available numbers&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentRandom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;availableValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;availableValues&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentRandom&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;availableValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentRandom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Briefly, the previous generator tries to maintain a pool of available integers to choose from. In each iteration, we randomly choose a number from this pool, then yield it and remove it from the valid pool. Theoretically, the maximum number of generated integers should be equal to &lt;code&gt;limit&lt;/code&gt; and all generated integers must be distinct. We can easily proof this by exhausting the implemented generator until the end of execution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// we set the limit to 8&lt;/span&gt;
&lt;span class="k"&gt;for &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;val&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;distinctRandom&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/*
sample output:
3
7
5
2
4
0
1
6
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Closing Note
&lt;/h1&gt;

&lt;p&gt;Generators are a great addition to ES6 which provides a solution for multiple problems and use cases. You can use them anywhere for sure, but I would suggest looking into alternative solutions for the problem in-hand before settling for a generator, as they can introduce more complexity to your code as well as they can be hard to debug at times. Nevertheless, happy coding 🎉  &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>GraphAV - A graph algorithms visualizer built using React and Typescript</title>
      <dc:creator>Karim Elghamry</dc:creator>
      <pubDate>Tue, 28 Jul 2020 12:51:46 +0000</pubDate>
      <link>https://dev.to/karimelghamry/graphav-a-graph-algorithms-visualizer-built-using-react-and-typescript-4g0m</link>
      <guid>https://dev.to/karimelghamry/graphav-a-graph-algorithms-visualizer-built-using-react-and-typescript-4g0m</guid>
      <description>&lt;h1&gt;
  
  
  Aim of the project
&lt;/h1&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%2Fraw.githubusercontent.com%2FKarimElghamry%2FGraphAV%2Fmaster%2Fscreenshots%2FGraphAV.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%2Fraw.githubusercontent.com%2FKarimElghamry%2FGraphAV%2Fmaster%2Fscreenshots%2FGraphAV.png" alt="GraphAV Logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GraphAV is an &lt;strong&gt;open source&lt;/strong&gt; graph algorithms visualizer built 💯 &lt;strong&gt;from scratch&lt;/strong&gt; using React and Typescript. It strives to facilitate the learning process of anyone who's struggling to grasp the concept of a certain graph algorithm. It can also be used as a tool by teachers to easily introduce a new algorithm to their students 👨‍🎓👩‍🎓.&lt;/p&gt;

&lt;h3&gt;
  
  
  links
&lt;/h3&gt;

&lt;p&gt;website: &lt;a href="https://karimelghamry.github.io/GraphAV/" rel="noopener noreferrer"&gt;https://karimelghamry.github.io/GraphAV/&lt;/a&gt;&lt;br&gt;
Github repo: &lt;a href="https://github.com/KarimElghamry/GraphAV" rel="noopener noreferrer"&gt;https://github.com/KarimElghamry/GraphAV&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GraphAV does not include any UI dependencies in its code other than styled-components. 🎉🎉&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: touch screen support is yet to be implemented. Use a PC/Laptop for a better experience.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Show some ❤️ and ⭐ the repo to support the project 😁&lt;/p&gt;
&lt;h1&gt;
  
  
  Features
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; Light/Dark themes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; Add, move and delete nodes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; Directed/Undirected edges&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; Unweighted edges&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; Control visualization speed&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; Control canvas zoom percentage&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; Shortest paths and predecessor node information&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; Visited nodes/edges animations&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚧&lt;/strong&gt; Weighted edges (to be implemented)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚧&lt;/strong&gt; Touch screen support (to be implemented)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚧&lt;/strong&gt; Save/share graphs (to be implemented)&lt;/p&gt;
&lt;h1&gt;
  
  
  Available Algorithms
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; DFS - Depth First Search&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; DLS - Depth Limited Search&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; IDDFS - Iterative Deepening Depth First Search&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; BFS - Breadth First Search (with shortest paths calculations)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔️&lt;/strong&gt; Dijkstra (with shortest paths calculations)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚧&lt;/strong&gt; Bellman-Ford (to be implemented)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚧&lt;/strong&gt; Floyd-Warshall (to be implemented)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚧&lt;/strong&gt; A* (to be implemented)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚧&lt;/strong&gt; Kosaraju (to be implemented)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚧&lt;/strong&gt; Tarjan's Strongly Connected Components* (to be implemented)&lt;/p&gt;
&lt;h1&gt;
  
  
  Demo
&lt;/h1&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/3hMRy2jO9Cw"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
