<?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: nuel ikwuoma</title>
    <description>The latest articles on DEV Community by nuel ikwuoma (@nuel_ikwuoma).</description>
    <link>https://dev.to/nuel_ikwuoma</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%2F357617%2F2389d0e1-e0bf-496d-b1e9-afc6bbd2b65a.jpg</url>
      <title>DEV Community: nuel ikwuoma</title>
      <link>https://dev.to/nuel_ikwuoma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nuel_ikwuoma"/>
    <language>en</language>
    <item>
      <title>Unlocking Ethereum Scalability with Scroll and Wormhole Integration</title>
      <dc:creator>nuel ikwuoma</dc:creator>
      <pubDate>Thu, 06 Jun 2024 13:43:34 +0000</pubDate>
      <link>https://dev.to/nuel_ikwuoma/unlocking-ethereum-scalability-with-scroll-and-wormhole-integration-44ci</link>
      <guid>https://dev.to/nuel_ikwuoma/unlocking-ethereum-scalability-with-scroll-and-wormhole-integration-44ci</guid>
      <description>&lt;h2&gt;
  
  
  Unlocking Ethereum Scalability with Scroll and Wormhole Integration
&lt;/h2&gt;

&lt;p&gt;The Ethereum blockchain, popular for its decentralized applications (dApps) and Smart Contracts, experiences serious scalability challenges. To address these, various Layer 2 (L2) solutions have surfaced, rendering enhanced throughput and minimizing costs while maintaining Ethereum’s security and decentralization. Among these solutions, Scroll remains one of the most innovative zkEVM (zero-knowledge Ethereum Virtual Machine) validity rollups. &lt;/p&gt;

&lt;p&gt;This article explains how Scroll enhances Ethereum’s scalability and the impact of its integration with Wormhole, facilitating the deployment of Ambient Finance, c3, and Synonym on the Scroll network. &lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Scroll’s zkEVM Rollup
&lt;/h3&gt;

&lt;p&gt;Scroll is an enhanced Layer 2 scaling solution that leverages EVM, a zero-knowledge rollup designed to scale Ethereum applications without needing specialized circuit designs.&lt;/p&gt;

&lt;p&gt;Zero-knowledge proofs (ZKPs) are known for verifying how credible a statement is without the statement itself.  When used on blockchains, ZKPs can verify a large number of transactions off-chain and then batch them into a single proof submitted on-chain. This process significantly helps to minimize computational load and gas fees.&lt;/p&gt;

&lt;p&gt;Scroll’s zkEVM is particularly intriguing because it maintains Ethereum Virtual Machine (EVM) congruence, meaning developers can dispatch existing Ethereum smart contracts on Scroll with minimal changes. This congruence ensures that developers can maximize the existing tools, languages, and frameworks built for Ethereum, facilitating a seamless transition and broader adoption.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Scroll Works
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Transaction Batching
&lt;/h4&gt;

&lt;p&gt;Transactions are initially processed off-chain in a Layer 2 environment. &lt;/p&gt;

&lt;h4&gt;
  
  
  2. Proof Generation
&lt;/h4&gt;

&lt;p&gt;These transactions are then bundled together, and a succinct proof (zk-SNARK) is generated to attest to their validity.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. On-Chain Verification
&lt;/h4&gt;

&lt;p&gt;The proof is passed on to the Ethereum mainnet, where Smart Contracts verifies it. Through this process, computational resources and amount of data required are drastically reduced, ultimately, leading to lower gas fees and faster transactions. &lt;/p&gt;

&lt;p&gt;By using this method, Scroll achieves high throughput and cost efficiency without compromising on security. The validity rollup ensures that all off-chain transactions are cryptographically secure and verifiable on-chain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bridging Ecosystem with the aid of Wormhole Ecosystems
&lt;/h3&gt;

&lt;p&gt;Wormhole is a cross-chain transmission protocol that eases interchangeability between different blockchain networks. Through the integration of  Wormhole with Scroll, several prominent dApps—Ambient Finance, c3, and Synonym—are now set to be deployed on Scroll, bringing with them new functionalities and user experiences.&lt;/p&gt;

&lt;h4&gt;
  
  
  Ambient Finance
&lt;/h4&gt;

&lt;p&gt;Ambient Finance leverages on the services rendered by decentralized finance to provide users with various financial products, including lending, borrowing, and asset management. Deploying on Scroll via Wormhole allows Ambient Finance to offer these services with lower transaction costs and higher efficiency, attracting a broader user base and enabling more complex financial products.&lt;/p&gt;

&lt;h4&gt;
  
  
  c3
&lt;/h4&gt;

&lt;p&gt;c3 focuses on decentralized data storage and management. By integrating with Scroll, c3 benefits from reduced data storage costs and enhanced speed, crucial for applications handling large volumes of data. This integration also enhances c3’s ability to offer scalable solutions to enterprises requiring decentralized storage.&lt;/p&gt;

&lt;h4&gt;
  
  
  Synonym
&lt;/h4&gt;

&lt;p&gt;Synonym aims to streamline cross-chain asset transfers and decentralized exchanges. With Wormhole and Scroll, Synonym can facilitate faster and cheaper transactions, improving the overall user experience in the decentralized finance (DeFi) ecosystem. This deployment ensures that users can move assets across different blockchains seamlessly, leveraging Scroll’s efficiency and Wormhole’s interoperability.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Future of Ethereum Scaling
&lt;/h3&gt;

&lt;p&gt;The integration of Scroll and Wormhole is one of the prominent moves towards addressing challenges associated with Ethereum’s scalability. This is because the integration fosters the maximization of the strengths of zKEVM roll ups when combined with cross-chain interoperability, rendering a robust framework for developing scalable, secure, and cost-effective networks.&lt;/p&gt;

&lt;p&gt;Scroll and Wormholes integration represents a significant step forward in addressing Ethereum’s scalability issues. By combining the strengths of zkEVM rollups and cross-chain interoperability, they offer a robust framework for developing scalable, secure, and cost-effective decentralized applications.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Benefits
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Enhanced Scalability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Scroll’s zkEVM rollup drastically increases transaction throughput and reduces costs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Seamless Interoperability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Wormhole enables cross-chain communication, allowing dApps to operate across multiple blockchains.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Broader Adoption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With EVM compatibility, existing Ethereum dApps can easily migrate to Scroll, benefiting from its scalability improvements.&lt;/p&gt;

&lt;h4&gt;
  
  
  Challenges Ahead
&lt;/h4&gt;

&lt;p&gt;This integration is promising, however, it also comes with challenges like ensuring robust security during cross-chain interactions and preserving all the ethos of decentralization in the Ethereum Ecosystem. In order to address these challenges, there is a need to embark on continuous development and rigorous testing. &lt;/p&gt;

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

&lt;p&gt;When it comes to Ethereum Scaling, merging Scroll’s zkEVM rollup with Wormhole is vital as it not only improves the performance and cost-efficiency of dApps but also facilitates a more interdependent blockchain ecosystem. With Ambient Finance, c3, and Synonym all deployed on Scroll, both users and developers can anticipate an Ethereum network with higher scalability and versatility, which could usher the web3 industry into the next generation of decentralized applications.&lt;/p&gt;

</description>
      <category>wormhole</category>
      <category>scroll</category>
      <category>defi</category>
    </item>
    <item>
      <title>Introducing Wormhole Gateway</title>
      <dc:creator>nuel ikwuoma</dc:creator>
      <pubDate>Wed, 09 Aug 2023 10:33:34 +0000</pubDate>
      <link>https://dev.to/nuel_ikwuoma/introducing-wormhole-gateway-48jh</link>
      <guid>https://dev.to/nuel_ikwuoma/introducing-wormhole-gateway-48jh</guid>
      <description>&lt;p&gt;Wormhole Gateway was recently &lt;a href="https://twitter.com/wormholecrypto/status/1686765306184695808"&gt;announced&lt;/a&gt; as an app-chain built on the industry tested Cosmos-SDK to enable bridging non-native assets into the Cosmos ecosystem. This is a huge milestone for cross-chain or chain-agnostic application, but before we explain what Gateways aims to achieve, lets briefly re-introduce Wormhole, for what it is, and what it tries to achieve in the web3 ecosystem.&lt;/p&gt;

&lt;p&gt;So as we know, ever since the existence of blockchains capable of executing arbitrary programs on-chain a.k.a Smart Contracts, different networks have sprung up, each with it own unique promise and competitive advantage, and although this has led to innovation in the web3 space but albeit not without its own challenges as well. Having blockchain networks which are isolated is synonymous living in world without any means of communication or even transportation from a source to a destination.&lt;br&gt;
Now we can begin to imagine the kinds of challenges we're poised to face in a world such as this, including missed out opportunities for economic co-operation, trade and skills or knowledge transfer. These problems are also synonymous in isolated blockchain networks of today, hence the need for a mechanism or a system by which say chainA can communicate and send message back and forth with say chainB.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Wormhole?
&lt;/h3&gt;

&lt;p&gt;At its core, Wormhole strives to be a generic message passing protocol, allowing for transfers of arbitrary data between different blockchains. This system is currently operated by a network of guardians, which are responsible for creating what is known as &lt;a href="https://docs.wormhole.com/wormhole/explore-wormhole/vaa"&gt;VAAs&lt;/a&gt;(verifiable Action Approvals) from messages observed in blockchain networks on which wormhole core-contract is deployed. These VAAs server are a security primitive or these cross-chain interactions, however a planned future upgrade to the Wormhole protocol would replace these with state proofs, effectively leading to a much more decentralized message passing system.&lt;br&gt;
Now that we've established the fundamentals of what Wormhole is about, let's meet Gateway😃.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enter the Gateway
&lt;/h3&gt;

&lt;p&gt;Gateway is an app-chain built with the Cosmos-SDK, to facilitate message transfer in the cosmos ecosystem and serve as a unified liquidity source in the Cosmos ecosystem.&lt;br&gt;
A big selling point for Gateway is that it leverages IBC(Inter-blockchain Communication) which is an industry standard protocol for authentication and transport of data between blockchains.&lt;br&gt;
It might be worth pointing out that Cosmos itself is a Multichain ecosystem, allowing for seamless interoperability between sovereign blockchains, all enabled by IBC(Inter-Blockchain Communication) module. Therefore cosmos chains that do implement this module can interact seamlessly with each other.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qbsqAYU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wq4xsg4g4x4ngs7b1z0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qbsqAYU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wq4xsg4g4x4ngs7b1z0.png" alt="Image description" width="800" height="542"&gt;&lt;/a&gt;&lt;br&gt;
(An overview of the cosmos ecosystem).&lt;/p&gt;

&lt;h3&gt;
  
  
  How Gateway work?
&lt;/h3&gt;

&lt;p&gt;For transfers of x-Assets between cosmos chains, these assets are sent from the source chain to the Gateway via IBC messages, and then forwarded to the destination chain on cosmos. By leveraging IBC and packet forwarding middleware, it becomes possible to bridge tokens in the Cosmos ecosystem without liquidity fragmentation, hence making Gateway the canonical chain in cosmos for bridging x-Assets through IBC transfer, and with no additional fees.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fun Fact: Originally Gateway was called Wormchain😉&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Why you should use Gateway
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability&lt;/strong&gt;:&lt;br&gt;
Gateway is highly scalable as only one full node is required to be operated by the Guardians to connect the entirety of the Cosmos ecosystem. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Security&lt;/strong&gt;:&lt;br&gt;
Gateway leverages light clients to facilitate assets transfers to and between cosmos chains, making it highly secure. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fungibility&lt;/strong&gt;:&lt;br&gt;
Gateway acts as the router for x-Assets transfer, hence making these assets 100% fungible.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Additional Information and resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="//docs.wormhole.com/wormhole/explore-%0Awormhole/gateway"&gt;Gateway docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/wormholecrypto/status/16823376643%0A79531266?s=20"&gt;Gateway twitter launch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=5OFjW_zmvbw"&gt;Gateway Osmocon presentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As part of a series of events lined up for the month of August, Gateway would be launched officially on 15th of August, with connection to Osmosis as the first supported chain, then other cosmos chains would follow suite. Some Interesting times ahead!🚀🚀&lt;/p&gt;

&lt;p&gt;To stay up-to-date with happenings in the wormhole ecosystem, be sure to check wormhole socials:&lt;br&gt;
&lt;a href="https://twitter.com/wormholecrypto?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"&gt;Wormhole twitter&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://linktr.ee/wormhole"&gt;Official Site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading, and see you all in web3, WAGMI!!!&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>wormhole</category>
      <category>ibc</category>
      <category>comos</category>
    </item>
    <item>
      <title>A guide to extending ERC2535 Diamonds.</title>
      <dc:creator>nuel ikwuoma</dc:creator>
      <pubDate>Wed, 22 Mar 2023 19:09:23 +0000</pubDate>
      <link>https://dev.to/nuel_ikwuoma/a-guide-to-extending-erc2535-diamonds-37a4</link>
      <guid>https://dev.to/nuel_ikwuoma/a-guide-to-extending-erc2535-diamonds-37a4</guid>
      <description>&lt;p&gt;&lt;em&gt;In this article, I introduce the Diamond standard for writing upgrade-able smart contracts, and give a walk through of how it can be extended,to add support for roll-back upgrades.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Pre-requisites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;understanding ethereum and smart contracts.&lt;/li&gt;
&lt;li&gt;experience with solidity.&lt;/li&gt;
&lt;li&gt;basic terminal skills, hardhat and foundry installation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For foundry installation, follow this &lt;a href="https://book.getfoundry.sh/getting-started/installation"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Brief introduction to Diamonds.
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://eips.ethereum.org/EIPS/eip-2535"&gt;EIP2535&lt;/a&gt; introduces the diamond standard as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A modular smart contract systems that can be upgraded/extended after deployment, and have virtually no size limit. More technically, a diamond is a contract with external functions that are supplied by contracts called facets. Facets are separate, independent contracts that can share internal functions, libraries, and state variables.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In my opinion, what makes diamond standard shines is the clear separation of concern(a.k.a logic) baked into it from the ground up. This is made possible through what is called &lt;code&gt;facets&lt;/code&gt;, which are smart contracts that groups logic that are related in one place. The default Diamond implementation used in this article includes three such facets described below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;DiamondCutFacet - This facet is mainly concerned with performing upgrade related logic on the Diamond.&lt;/li&gt;
&lt;li&gt;DiamondLoupeFacet - This facet is mainly concerned with introspection into the diamond.&lt;/li&gt;
&lt;li&gt;OwnershipFacet - Like the name implies, this facet is mainly concerned with matters related to ownership/admin/access-control on the diamond.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, onto the most important component, &lt;strong&gt;The Diamond&lt;/strong&gt; itself, which is the proxy smart contract that enshrines the state of the whole system, and exposes the methods of the facets using &lt;a href="https://docs.soliditylang.org/en/v0.8.19/introduction-to-smart-contracts.html#delegatecall-and-libraries"&gt;delegatecall&lt;/a&gt;.&lt;br&gt;
There are other components built into this default implementation, which includes libraries, interfaces and so on, but we'd discuss those later on as they're encountered.&lt;/p&gt;

&lt;p&gt;To follow along in your preferred IDE, you can clone the &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback"&gt;repository&lt;/a&gt; and follow the ReadME to install the necessary dependencies.&lt;/p&gt;
&lt;h2&gt;
  
  
  The adventure begins... 🚗
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Tracking the state of the diamond.
&lt;/h3&gt;

&lt;p&gt;The Diamond for this demonstration, utilizes the &lt;code&gt;diamond storage&lt;/code&gt; pattern, which declares the state of the Diamond as a &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/libraries/LibDiamond.sol#L44"&gt;storage struct&lt;/a&gt;, this is made possible via a feature of libraries in solidity, which allows for a library function to return a struct in storage, see &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/libraries/LibDiamond.sol#L61"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;diamondStorage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DiamondStorage&lt;/span&gt; &lt;span class="k"&gt;storage&lt;/span&gt; &lt;span class="n"&gt;ds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DIAMOND_STORAGE_POSITION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;ds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;position&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 struct is then set in storage using the hash of a &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/libraries/LibDiamond.sol#L24"&gt;unique string&lt;/a&gt;, effectively preventing storage slot collision.&lt;br&gt;
The &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/libraries/LibDiamond.sol"&gt;library&lt;/a&gt; defines important data structure and logic that are shared amongst facets, thereby upholding the justifications of this standard (i.e Facets can share internal functions, libraries).&lt;/p&gt;
&lt;h3&gt;
  
  
  How upgrades are accomplished?
&lt;/h3&gt;

&lt;p&gt;In diamond lingo, upgrade is synonymous to cut, the &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/facets/DiamondCutFacet.sol"&gt;DiamondCutFacet&lt;/a&gt; implements the &lt;code&gt;diamondCut&lt;/code&gt; to achieve that. However the struct &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/interfaces/IDiamondCut.sol#L13"&gt;FacetCut&lt;/a&gt; models the data unique to performing the said upgrade, i.e cutting the Diamond.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;FacetCut&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;facetAddress&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;FacetCutAction&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;bytes4&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;functionSelectors&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;h3&gt;
  
  
  How can we roll-back an upgrade? 😐
&lt;/h3&gt;

&lt;p&gt;First we define a custom &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/libraries/LibDiamond.sol#L38"&gt;RollBack&lt;/a&gt; struct, to model the data for performing a rollback&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;RollBackCuts&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;facetAddress&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FacetCutAction&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;bytes4&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;functionSelectors&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;Next we append an array of this struct to the storage struct of the diamond, and subsequently populate this array with every upgrade to the diamond, see &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/libraries/LibDiamond.sol#L132"&gt;here&lt;/a&gt;, &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/libraries/LibDiamond.sol#L168"&gt;here&lt;/a&gt; and &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/libraries/LibDiamond.sol#L197"&gt;here&lt;/a&gt;, these functions adds, replaces and removes functions from the Diamond as part of an upgrade.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;DiamondStorage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// maps function selector to the facet address and
&lt;/span&gt;        &lt;span class="c1"&gt;// the position of the selector in the facetFunctionSelectors.selectors array
&lt;/span&gt;        &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes4&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;FacetAddressAndPosition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;selectorToFacetAndPosition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// maps facet addresses to function selectors
&lt;/span&gt;        &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;FacetFunctionSelectors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;facetFunctionSelectors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// facet addresses
&lt;/span&gt;        &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;facetAddresses&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// Used to query if a contract implements an interface.
&lt;/span&gt;        &lt;span class="c1"&gt;// Used to implement ERC-165.
&lt;/span&gt;        &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes4&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;supportedInterfaces&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// owner of the contract
&lt;/span&gt;        &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;contractOwner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// history for rollback
&lt;/span&gt;        &lt;span class="n"&gt;RollBackCuts&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;rollbackCuts&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;A constant address, labeled as &lt;code&gt;ROLLBACK_ADDRESS&lt;/code&gt; is defined in the library this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;constant&lt;/span&gt; &lt;span class="n"&gt;ROLLBACK_ADDRESS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x000000000000000000000000000000000000bEEF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This constant address distinguishes a roll-back from an upgrade, hence preventing the duplication bug from multiple roll-backs.&lt;br&gt;
Further more, the &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/libraries/LibDiamond.sol#L259"&gt;ROLLBACK_ADDRESS&lt;/a&gt; also serves to deter an initialization call on roll-back.&lt;/p&gt;

&lt;p&gt;Then we proceed to define the logic for the &lt;code&gt;rollback&lt;/code&gt; as part of the DiamondCutFacet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rollback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// enforce is contract owner
&lt;/span&gt;        &lt;span class="n"&gt;LibDiamond&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enforceIsContractOwner&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="c1"&gt;// LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
&lt;/span&gt;        &lt;span class="n"&gt;LibDiamond&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RollBackCuts&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;storage&lt;/span&gt; &lt;span class="n"&gt;rollbackCuts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LibDiamond&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;diamondStorage&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;rollbackCuts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;rollbackCutsLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rollbackCuts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&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="n"&gt;rollbackCutsLength&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="nb"&gt;revert&lt;/span&gt; &lt;span class="n"&gt;NoRollBackAction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;LibDiamond&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RollBackCuts&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;rollbackCut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rollbackCuts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rollbackCutsLength&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="c1"&gt;// remove last rollback action
&lt;/span&gt;        &lt;span class="n"&gt;rollbackCuts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;cutLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rollbackCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;facetAddress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FacetCut&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;facetCut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FacetCut&lt;/span&gt;&lt;span class="p"&gt;[](&lt;/span&gt;
            &lt;span class="n"&gt;cutLength&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="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;i&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;cutLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;bytes4&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;rollbackSelector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;bytes4&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="n"&gt;rollbackSelector&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rollbackCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;functionSelectors&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="n"&gt;facetCut&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="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FacetCut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;rollbackCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;facetAddress&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="n"&gt;rollbackCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;rollbackSelector&lt;/span&gt;
            &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;LibDiamond&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;diamondCut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;facetCut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;LibDiamond&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ROLLBACK_ADDRESS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s"&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 logic for the rollback effectively reads the storage struct, checks if a rollback is valid, constructs the data for rollback and proceeds to cut the diamond with the said data, using our beloved &lt;code&gt;ROLLBACK_ADDRESS&lt;/code&gt; once again. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Diamond once again. 💎
&lt;/h3&gt;

&lt;p&gt;This time we dive into the logic of the Diamond constructor, and the main change here is registering the selector of our &lt;code&gt;rollback&lt;/code&gt; implementation as seen &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/contracts/Diamond.sol#L29"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_contractOwner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_diamondCutFacet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;LibDiamond&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setContractOwner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_contractOwner&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Add the diamondCut external function from the diamondCutFacet
&lt;/span&gt;        &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FacetCut&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;cut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FacetCut&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="kt"&gt;bytes4&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;functionSelectors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;bytes4&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="n"&gt;functionSelectors&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;diamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// Add the rollback extxernal function from the diamondCutFacet
&lt;/span&gt;        &lt;span class="n"&gt;functionSelectors&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="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rollback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;cut&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FacetCut&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="n"&gt;facetAddress&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;_diamondCutFacet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IDiamondCut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FacetCutAction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;functionSelectors&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;functionSelectors&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="n"&gt;LibDiamond&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;diamondCut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s"&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;With all major changes out of the way, we proceed to write test to validate our code:&lt;br&gt;
The tests can be found &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback/blob/main/test/testRollback.t.sol"&gt;here&lt;/a&gt;.&lt;br&gt;
Now in your terminal excute the commands to &lt;code&gt;compile&lt;/code&gt; and run the &lt;code&gt;tests&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;npx hardhat compile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





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

&lt;/div&gt;



&lt;p&gt;The test essentially deploys, upgrade, and execute a roll-back on the diamond.&lt;br&gt;
Verify that it passes, and viola!!! 🎉  We have basically extended an ERC2535 Diamond to add support for roll-back, which brings us to the end of this article.&lt;/p&gt;

&lt;p&gt;If you enjoy reading code, i encourage an in-depth look at the &lt;a href="https://github.com/nuel-ikwuoma/Diamond-With-Rollback"&gt;Repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once again, congrats on making it to the end, and I hope you found this article useful and can proceed to implementing Diamonds in the wild. Reach me on twitter @&lt;a href="https://twitter.com/nuel_ikwuoma"&gt;nuel&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>diamonserc2535</category>
      <category>upgradability</category>
      <category>smartcontrcats</category>
    </item>
    <item>
      <title>Arbitrary Message Signing in Dapps</title>
      <dc:creator>nuel ikwuoma</dc:creator>
      <pubDate>Tue, 17 Aug 2021 03:32:32 +0000</pubDate>
      <link>https://dev.to/nuel_ikwuoma/arbitrary-message-signing-in-dapps-5gie</link>
      <guid>https://dev.to/nuel_ikwuoma/arbitrary-message-signing-in-dapps-5gie</guid>
      <description>&lt;p&gt;Hi, I am nuel, a web and smart contract developer, i tweet frequently about web development and blockchains, find me &lt;a href="https://twitter.com/nuel_ikwuoma"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For the absolute beginners, i would like to briefly define what Dapps are...&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A Dapp is a decentralized application, that functions and executes based on the primitives of the blockchain, making it permissionless, trustless and censorship resistant&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In a quest to understand message signing and digital signatures, which is quite a relatively new concept for me, I had gone through the rabbit hole of reading tons of articles and even landed in several official Ethereum Improvement Proposals (&lt;code&gt;EIP-155&lt;/code&gt;, &lt;code&gt;EIP-191&lt;/code&gt;, &lt;code&gt;EIP-712&lt;/code&gt;), these proposals although well detailed out isnt neccasarily the best landing point for a beginner new to this concept as it can seem really abstract, as it was for my case initially as it took me a whole lot of time to figure out the basics of arbitratry message signing and what digital signatures really are and why we might need one. So I would approach this article starting by answering the question of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What digital signatures are?&lt;/li&gt;
&lt;li&gt;Why we need digital signatures? &lt;/li&gt;
&lt;li&gt;Use cases in Dapps.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What are digital signatures?
&lt;/h2&gt;

&lt;p&gt;Basically, digital signature is a way to prove identity, even in the absence of the entity who owns the signature.(still dosent make sense, relax). &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It works similar like this: Imagine you were issued a cheque by a account holder, the cheque is signed and all you have to do is go to the bank, once the bank verifies the signature on the cheque (and maybe other verifications), you become elligible to claim the said ammount.
In the context of Dapps, an externally owned account signs a message or transaction off-chain, and anyone can submit the transaction on-chain to verify the signature or perform its intended action in the case of a transaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why we need digital signatures?
&lt;/h2&gt;

&lt;p&gt;Aside the obvious benefits of authentication checks, digital signatures can improve the user experience in Dapps, and also reduce the number of transactions sent to the blockchain, see this awesome &lt;a href="https://soliditydeveloper.com/erc20-permit"&gt;article&lt;/a&gt; for a quick and well explained demonstration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases?
&lt;/h2&gt;

&lt;p&gt;Some trending uses cases for digital signatures are listed but not limited to...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Message signing and verification&lt;/li&gt;
&lt;li&gt;UX improvements&lt;/li&gt;
&lt;li&gt;Gasless transactions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where to go from here?
&lt;/h3&gt;

&lt;p&gt;If you are building smart contract on ethereum using solidity or you flesh out the frontends of Dapps, you might want to take look at &lt;a href="https://eips.ethereum.org/EIPS/eip-155"&gt;this&lt;/a&gt;, &lt;a href="https://eips.ethereum.org/EIPS/eip-191"&gt;this&lt;/a&gt; and &lt;a href="https://eips.ethereum.org/EIPS/eip-712"&gt;this&lt;/a&gt; proposals, to get a much deeper understanding on the topic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Message signing, although done with a private key, is not the same as encryption.&lt;/p&gt;

&lt;p&gt;I am still actively learning and researching about blockchain and web3/p2p technologies, and I would love to hear your suggestions, as well as recommendations.&lt;/p&gt;

&lt;p&gt;Thanks for reading, have any questions and/or interests related to Ethereum smart contract development or solidity? Reach out on Twitter&amp;gt;&amp;gt;&amp;gt; &lt;a href="https://twitter.com/nuel_ikwuoma"&gt;nuel&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ethereum</category>
      <category>digitalsignatures</category>
      <category>dapps</category>
    </item>
    <item>
      <title>Higher Order Functions - A pragmatic approach</title>
      <dc:creator>nuel ikwuoma</dc:creator>
      <pubDate>Sat, 18 Jul 2020 12:17:23 +0000</pubDate>
      <link>https://dev.to/nuel_ikwuoma/higher-order-functions-a-pragmatic-approach-51fb</link>
      <guid>https://dev.to/nuel_ikwuoma/higher-order-functions-a-pragmatic-approach-51fb</guid>
      <description>&lt;h2&gt;
  
  
  Intoducting HoF
&lt;/h2&gt;

&lt;p&gt;Its a common saying that functions are the bread and butter of programming, and the basic unit for building reusable logic in many programming languages, but &lt;strong&gt;What makes a function become higher-order?&lt;/strong&gt; &lt;br&gt;
In simple terms, a HoF is just a kind of function that can accept other function(s) as argument or/and return a function. &lt;br&gt;
Still Not clear? ,Its fine, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;a line of code says more than a thousand words. Lets proceed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are many different scenarios for approaching HoF but i would list some of the mot common as we continue&lt;/p&gt;
&lt;h3&gt;
  
  
  Filtering Collections
&lt;/h3&gt;

&lt;p&gt;To demonstrate a simple example, we consider a basic attempt to get only even numbers from a collection, we do the following:&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;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&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="mi"&gt;11&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;result&lt;/span&gt; &lt;span class="o"&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="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;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&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="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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&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="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&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="p"&gt;}&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;result&lt;/span&gt;     &lt;span class="c1"&gt;// [2, 6, 8]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach seems to work, but if the criteria for selecting the result become a little complicated, things can easily start to look messy, also leaving no room for reusability. A better approach would be to write a custom filtering logic as we do 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;)&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;result&lt;/span&gt; &lt;span class="o"&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="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;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;nums&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="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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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="p"&gt;{&lt;/span&gt;
              &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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="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;result&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 function we have just written would expect a collection as its first argument and another function as its second argument, which would be used to perform the selection criteria, now we can easily demonstrate the previous example again.&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&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;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// [2, 6, 8]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should noted that the custom filter function defined above is only a naive attempt to implement the more robust and efficient inbuilt &lt;code&gt;Array.prototype.filter&lt;/code&gt; built-in method, for filtering Array collections.&lt;/p&gt;

&lt;h3&gt;
  
  
  Grouping
&lt;/h3&gt;

&lt;p&gt;An even more useful application for HoF would be to group collection by say some arbitrary tag, and presenting them in a nicer arrangement.&lt;br&gt;
This is one in many scenarios where higher order function begins to shine. Lets implement the logic to group items&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="nx"&gt;group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;groupBy&lt;/span&gt;&lt;span class="p"&gt;)&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;grouped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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="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;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;items&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="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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;groupBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;grouped&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;grouped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&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="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="nx"&gt;grouped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tag&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;items&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="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;grouped&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;For this example we would utilize the group function we just defined to re-arrange a collection, using a arbitrary tag.&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;const&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
     &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;car&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tesla&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Y&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="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;smartphone&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;yr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2019&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="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;car&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mercedes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;classic&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="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gaming&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PS5&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="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;smartphone&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Iphone&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;yr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2019&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tagged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tag&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nx"&gt;tagged&lt;/span&gt;   
&lt;span class="cm"&gt;/*
  {
     car: [
        { tag: 'car', name: 'tesla',model: "Y"},
        { tag: 'car', name: 'mercedes', model: "classic" }
     ],
     smartphone: [
        { tag:'smartphone', name: 'Samsung s9', yr: "2018" },
        { tag:'smartphone', name: 'Iphone 11', yr: "2019" }
     ],
     gaming: [ { tag: 'gaming', name: 'PS5' } ]
    }
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool right? 😊 With HoF we can easily express this logic and still maintaining readability of our code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flattening Arrays
&lt;/h3&gt;

&lt;p&gt;Ill leave you with this attempt to flatten a nested array, of an arbitrary depth. The first attempt would make use of the built-in Array.prototype.reduce. Lets do that.&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="nx"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nested&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;nested&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&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;flat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;next&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;flat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nest&lt;/span&gt; &lt;span class="o"&gt;=&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="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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;0&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;deeper&lt;/span&gt; &lt;span class="o"&gt;=&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="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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nx"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deep&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// [1, 2, 3, 5, 0]&lt;/span&gt;
&lt;span class="nx"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deeper&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// [1, 2, 3, 5, [0, 9, 1], 0]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that trying to flatten a deeply nested array, seemed not to yield the expected output 😦. However, we can do better, and we try a second approach but this time using the good old recursion technique in combination with &lt;code&gt;Array.prototype.reduce&lt;/code&gt;&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="nx"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nested&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;nested&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&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="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&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="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="nx"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deeper&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// [1, 2, 3, 5, 0, 9, 1, 0]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Viola, we get the result we expected. It works!!!  😆&lt;/p&gt;

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

&lt;p&gt;In essence higher order functions are not really difficult to understand, although they could look somewhat intimidating at first. Many popular javascript libraries including Redux, use them behind the scenes to expose simple interface for implementing even very complex logic.&lt;/p&gt;

&lt;p&gt;I hope you do enjoy this article, as much as i did putting it up. Please leave your review below.&lt;/p&gt;

&lt;p&gt;Say hi on &lt;a href="https://twitter.com/nuel_ikwuoma"&gt;twitter&lt;/a&gt;  💙&lt;br&gt;
Lovely weekend to you!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>higherorderfunctiions</category>
      <category>es6</category>
      <category>pragmatic</category>
    </item>
    <item>
      <title>JavaScript concepts to write better React code</title>
      <dc:creator>nuel ikwuoma</dc:creator>
      <pubDate>Sat, 11 Jul 2020 09:53:51 +0000</pubDate>
      <link>https://dev.to/nuel_ikwuoma/javascript-concepts-to-write-better-react-code-45f0</link>
      <guid>https://dev.to/nuel_ikwuoma/javascript-concepts-to-write-better-react-code-45f0</guid>
      <description>&lt;p&gt;Beginning development in React can be fun and trivial, however your codebase could quuickly get messy, verbose and unreadable unless some careful toughts are put into the development process. Writing clean (quality) code, many would agree comes with experience and i also belong to that school of thought, however there are simple and often overlooked javascript concepts that i believe if harnessed properly can greatly improve our React code quality. I would list these concepts in no particular order, Lets begin...&lt;/p&gt;

&lt;h1&gt;
  
  
  Object destructing
&lt;/h1&gt;

&lt;p&gt;To be honest, ive never seen a professional React codebase without this sugar. Its a simple concept that can greatly improve code conciseness. The idea is simple - write variables whose names matches those of the object to be destructured and hold the same value as the name it mirrors.&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="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="nx"&gt;baz&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="na"&gt;foo&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;bar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Leave me out&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;baz&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="nx"&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="nx"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; 1, 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This property is used in React mostly to assign variables from a component prop or state, hence avoiding repeated calls to props[dot]this, state[dot]that. I hope you get this idea. 😆&lt;br&gt;
The same also applies for destructuring array, however the index of array elements determines how the operation executes.&lt;/p&gt;
&lt;h1&gt;
  
  
  Conditional Execution
&lt;/h1&gt;

&lt;p&gt;More often we have logic we want to excute based on some given criteria, using simple if-else statement would suffice, however React developers favor the &amp;amp;&amp;amp; operator instead. The code below logs 'congrats' to console if an expression returns 'true'&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;const&lt;/span&gt; &lt;span class="nx"&gt;isAdult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&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;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;

&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;Congrats!!!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;// 'congrats'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Spread Operators
&lt;/h1&gt;

&lt;p&gt;Often in React codebases you see the spread operator been used to pass props to components, as this is often convinient.&lt;br&gt;
A simple use case for spread operators are passing arbitrary arguments to a function. A simple is how we would calculate the 'mean' from a series of numbers, we could leverage spread operator to make this easy.&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;const&lt;/span&gt; &lt;span class="nx"&gt;calc_mean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;series&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;series&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&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;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;series&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;calc_mean&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;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using spread operator we can evaluate mean from an arbitrary length of arguments.&lt;/p&gt;

&lt;h1&gt;
  
  
  Flattening with Array.prototype.reduce()
&lt;/h1&gt;

&lt;p&gt;I chose the word 'flattening' out of recklessness, as i could not come up with something better, do forgive me 😱&lt;br&gt;
However the intent is here is trying to reduce a collection to a single output. Say for instance you have been tasked at your new job to find the percent reviews of a newly shipped product from a given 'customers' sample.&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;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;happy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;204&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;indifferent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; 
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// compute total from customers list&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&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;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// optional: proceed to compute percent for all categories&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;customer_reviews&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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;percent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;percent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;percent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toFixed&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="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;%&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer_reviews&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have a list of customers with their average percentage reviews. &lt;/p&gt;

&lt;h1&gt;
  
  
  Higher Order Functions
&lt;/h1&gt;

&lt;p&gt;This category of higher-order functions discussed here involve those that return a function, as they can greatly result to code conciseness and make abstracting reusable logic a breeze. Say for instance, we want to filter a collection based on another collection&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;invite_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&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;friends&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;snow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Fun Fact: The array, &lt;strong&gt;invite_ids&lt;/strong&gt; is actually a fibonacci series 😉&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we only want those friends whose id can be found in the invite_ids list to attend the party, how do we go about this?&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;const&lt;/span&gt; &lt;span class="nx"&gt;filter_invites&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;id&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;ids&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// now we can do this...&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;my_invites&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;friends&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filter_invites&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;invite_ids&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;my_invites&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [{name: 'sam', id: 1}, {name: 'jon', id: 13}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now notice how concise and brief we have implemented this logic while still maintaining readability. The function that performs the filtering can even be abstracted for purpose of reusing and/or testing.&lt;/p&gt;

&lt;h1&gt;
  
  
  Template Literals
&lt;/h1&gt;

&lt;p&gt;Template literals are cool, in-essence they are just like strings, but with more capabilities. Very often, they are used in places where we intend to embed expressions and other code-related logic.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A simple greeting procedure
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mike&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;// Hello mike&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;conditional logic in templates
&lt;/li&gt;
&lt;/ul&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;friends&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ola&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tea&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mike&lt;/span&gt;&lt;span class="dl"&gt;"&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;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`box &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;friends&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;show&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hidden&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;// 'box show'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;By utilizing template literals we can write inline and more concise conditional expressions. &lt;br&gt;
There are many other concepts to uncover as you write more React code, but i do not intend this post an ultimate authority. Thanks for reading, and i love your feedback.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/nuel_ikwuoma"&gt;twitter&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>patterns</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>Using Iterators and Generators in JavaScript</title>
      <dc:creator>nuel ikwuoma</dc:creator>
      <pubDate>Thu, 02 Apr 2020 02:48:41 +0000</pubDate>
      <link>https://dev.to/nuel_ikwuoma/using-iterators-and-generators-in-javascript-121n</link>
      <guid>https://dev.to/nuel_ikwuoma/using-iterators-and-generators-in-javascript-121n</guid>
      <description>&lt;p&gt;The word 'iterable' appears in many programming paradigm, it simply can be assumed as any data-structure that can be passed to a loop, to extract its contents. Many types in javascript are &lt;em&gt;iterable&lt;/em&gt;, the likes of which includes &lt;strong&gt;string&lt;/strong&gt;, &lt;strong&gt;array&lt;/strong&gt;, &lt;strong&gt;set&lt;/strong&gt; e.t.c&lt;br&gt;
A quick example would be iterating over the Array type, so we can safely call the Array type an &lt;em&gt;iterable&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let weekends = ["fri", "sat", "sun"];   // Array type
for(let day of weekends) {
    console.log("its " + day)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we begin to implement our own custom &lt;em&gt;iterable&lt;/em&gt;, lets quickly take a look at generators. A generator in javascript is an function expression with a yield statement, its quite different from a normal function and the yield statement should be inside the function block that defines the generator and not in an enclosing function. A quick demo of generator for yielding fibonacci sequence looks thus:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; function * fibonacci (rng) {
     let a = 0, b = 1, nxt;
     for(let i=2; i &amp;lt; rng; i++) {
        nxt = a + b;
        a = b;
        b = nxt
        yield nxt;    // 'yield' the next number in the fibonacci sequence
     }
 }

// using the fibinacci generator above to yield first 10 sequence
for(let val of fibonacci(10)) {
    if(val &amp;gt; 100) break;    // Note 'break' to prevent an infinite loop
    console.log(val)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I apologize if the generator expression above is a little complicated, but the most important to note is how we define the expression with an asterik and how we output value using the yield statement.&lt;br&gt;
One more thing to briefly introduce is the Symbol constructor, in javascript, Symbol defines a unique symbol(constant) and ensures it does not coerce with other Symbols of similar construct. For instance,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let bar = Symbol("bar")
let bar2 = Symbol("bar")
bar == bar2    // returns "false"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the two Symbol definition above does not coerce.&lt;br&gt;
Now, lets assume we creating a custom type we would call Matrix, to store a series of numbers, we would define a custom javascript class thus:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Matrix {
    constructor(width, height, element = (x, y) =&amp;gt; undefined) {
        this.width = width
        this.height = height
        this._content = []

        for(let y=0; y &amp;lt; height; y++) {
            for(let x=0; x &amp;lt; width; x++) {
                this._content[y*width + x] = element(x, y)
            }
        }
    }

    get(x, y) {
        return this._content[y*this.width + x]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can instantiate an 3 by 3 matrix object and pass some arbitrary values thus:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let matrix = new Matrix(3, 3, (x, y) =&amp;gt; `x: ${x}, y: ${y}`)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get through the values defined in the matrix type, a naive approach would look thus;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(let val of matrix._content) {
    console.log(val)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This seems to work, but the underscore which precedes the &lt;strong&gt;content&lt;/strong&gt; instance property should remind us not to access that property directly from outside the class in which it is defined, so how do we make the &lt;strong&gt;Matrix&lt;/strong&gt; type &lt;em&gt;iterable&lt;/em&gt;, there are quite a few ways to implement this, but i claim that the generator approach is quite easy to implement and reason about, it goes this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Matrix.prototype[Symbol.iterator] = function* () {
    for(let y=0; y&amp;lt; this.height; y++) {
        for(let x=0; x &amp;lt; this.width; x++) {
            yield {x, y, value: this._content[y * this.width + x]}
        }
    }
}

// now we can create the object and iterate directly without directly accessing its internals

let matrix2 = new Matrix(3, 3, (x, y) =&amp;gt; `x: ${x}, y: ${y}`)
for(let {x, y, value} of matrix2) {
    console.log(x, y, value)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What just happened?&lt;br&gt;
First, we defined a property in &lt;strong&gt;Matrix&lt;/strong&gt; prototype named &lt;strong&gt;Symbol.iterator&lt;/strong&gt;, this is what is called when we try to get values from an iterable type inside a loop.&lt;/p&gt;

&lt;p&gt;Second, since generator maintains its state everytime we yield from it, we use that to ensure we return appropriate values upon each iteration.&lt;/p&gt;

&lt;p&gt;Now, it should be clear how we can use iterators and the less-favoured generator expression to write more robust custom types.&lt;/p&gt;

&lt;p&gt;Thanks for reading, would appreciate your feedbacks&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>iterators</category>
      <category>generators</category>
      <category>symbol</category>
    </item>
  </channel>
</rss>
