<?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: George Gkasdrogkas</title>
    <description>The latest articles on DEV Community by George Gkasdrogkas (@georgegkas).</description>
    <link>https://dev.to/georgegkas</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%2F323815%2Ffd79049c-5f16-4840-a417-6a7827ac934e.png</url>
      <title>DEV Community: George Gkasdrogkas</title>
      <link>https://dev.to/georgegkas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/georgegkas"/>
    <language>en</language>
    <item>
      <title>The definitive guide on compiling and linking Boost C++ libraries for Visual Studio projects</title>
      <dc:creator>George Gkasdrogkas</dc:creator>
      <pubDate>Mon, 19 Apr 2021 21:06:18 +0000</pubDate>
      <link>https://dev.to/georgegkas/the-definite-guide-on-compiling-and-linking-boost-c-libraries-for-visual-studio-projects-1kan</link>
      <guid>https://dev.to/georgegkas/the-definite-guide-on-compiling-and-linking-boost-c-libraries-for-visual-studio-projects-1kan</guid>
      <description>&lt;p&gt;Recently, a requirement came up to migrate a legacy C++ codebase, containing quite a few of custom implemented functionalities, into a more robust architecture. After spending some time at searching, we decided to include the Boost libraries, due to the large set of utilities they offer. &lt;/p&gt;

&lt;p&gt;Most of the C++ Boost libraries are header-only; they consist entirely of header files, and require no separately-compiled library binaries. However, there are some libraries that need to be built separately. The "Getting Started" guide on Boost website is quite informative, but do not provide clear guidance on how to  build for multiple architectures and targets (debug/release). We're using Visual Studio as our IDE and finding the best way to build those binaries was quite tiresome. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For that reason, we dedicated this guide on how to build and link the C++ Boost libraries in Visual Studio projects.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Downloading the Boost libraries
&lt;/h1&gt;

&lt;p&gt;To get the latest version of Boost (or any other version for that matter), go to &lt;a href="https://www.boost.org/users/download/" rel="noopener noreferrer"&gt;the official download&lt;/a&gt; page. We are targeting Windows platforms, so we need  to choose the respective version. The "Getting Started" guide recommends  downloading the &lt;a href="https://www.7-zip.org/" rel="noopener noreferrer"&gt;7-Zip&lt;/a&gt; archive.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We no longer recommend .zip files for Boost because they are twice as large as the equivalent .7z files. We don't recommend using Windows' built-in decompression as it can be painfully slow for large archives. - "&lt;a href="https://www.boost.org/doc/libs/1_76_0/more/getting_started/windows.html#zip" rel="noopener noreferrer"&gt;Getting Started on Windows&lt;/a&gt;", &lt;a href="https://www.boost.org/" rel="noopener noreferrer"&gt;www.boost.org&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The current release, as of this day,  is version 1.76.0, but the methodology we are going to follow works (and will continue to work in the future) for any release.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4esmylznd1k8xj3i26e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4esmylznd1k8xj3i26e.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After we successfully downloaded the archive, we need to extract the containing folder into the desired location. &lt;/p&gt;

&lt;p&gt;The documentation assumes that the path for any library version is inside &lt;code&gt;C:\Program Files\boost&lt;/code&gt;, so we're going to respect that. Create a directory called &lt;code&gt;boost&lt;/code&gt; inside &lt;code&gt;C:\Program Files\&lt;/code&gt; and extract the archive there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuvqjdoagxjfrwq6it14g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuvqjdoagxjfrwq6it14g.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Building the binaries
&lt;/h1&gt;

&lt;p&gt;The Boost libraries includes a really nice build system, which we are definitely going to use it. The build system is triggered from the command line. First we have to open the &lt;code&gt;cmd&lt;/code&gt; window and navigate into the root folder of the Boost library.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxt6t7k7nkige7bez2e8q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxt6t7k7nkige7bez2e8q.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we have to initialize the build system by running the &lt;code&gt;bootstrap.bat&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe9oiiauri9brh5rv40en.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe9oiiauri9brh5rv40en.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After bootstraping is done, it's time to build the actual binaries. Remember what we said earlier: We have to build&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;artifacts for x64 architecture&lt;/li&gt;
&lt;li&gt;artifacts for x86 architecture&lt;/li&gt;
&lt;li&gt;artifacts for static linking (i.e. &lt;code&gt;.lib&lt;/code&gt; files)&lt;/li&gt;
&lt;li&gt;artifacts for dynamic linking (i.e. &lt;code&gt;.dll&lt;/code&gt; files)&lt;/li&gt;
&lt;li&gt;release artifacts&lt;/li&gt;
&lt;li&gt;debug artifacts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In general, we need to trigger 8 builds. Fortunately for us, the build command allow us to specify multiple targets in one build. In the end, we'll only trigger two builds, one for each architecture type.&lt;/p&gt;

&lt;h1&gt;
  
  
  Building for x86 architecture
&lt;/h1&gt;

&lt;p&gt;Run the following command in you terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;b2 --build-dir=build\x86 address-model=32 threading=multi --stagedir=.\bin\x86 --toolset=msvc -j 16 link=static,shared runtime-link=static,shared --variant=debug,release
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fckarjghgwkdw68kduoox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fckarjghgwkdw68kduoox.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This command will build and place the binaries into &lt;code&gt;C:\Program Files\boost\boost_1_76_0\bin\x86\lib\&lt;/code&gt;. The output will contain all the required files we might need for most of our projects.&lt;/p&gt;
&lt;h1&gt;
  
  
  Building Debug for x64 architecture
&lt;/h1&gt;

&lt;p&gt;Execute the following command on you terminal:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;b2 --build-dir=build\x64 address-model=64 threading=multi --stagedir=.\bin\x64 --toolset=msvc -j 8 link=static,shared runtime-link=static,shared --variant=debug,release
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frddgprvq4l7q2j7372zp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frddgprvq4l7q2j7372zp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This command will build and place the binaries into &lt;code&gt;C:\Program Files\boost\boost_1_76_0\bin\x64\lib\&lt;/code&gt;.  As before, the output will contain all the required files we might need for most of our projects.&lt;/p&gt;
&lt;h1&gt;
  
  
  Explaining the build command arguments
&lt;/h1&gt;

&lt;p&gt;Let's take a quick look on the arguments used in the previous two commands.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--build-dir&lt;/code&gt;: Specify the directory to place all the intermediate files while building.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;address-model&lt;/code&gt;: Specify the targeting address model.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;threading&lt;/code&gt;: Compile Boost to be thread aware. (see this question on stackoverflow for more info.)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--stage-dir&lt;/code&gt;: the directory where the binaries will be placed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--toolset&lt;/code&gt;: The compiler and linker that will be used to build the artifacts. We chose &lt;code&gt;msvc&lt;/code&gt;; this should be the default on Windows, but in case we have more compilers installed on the system, it's better to explicitly define it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-j&lt;/code&gt;: how many threads to use for building. This can drastically improve the build times on multi-threading environments, i.e. most modern machines.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;link&lt;/code&gt;: declare a library target.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;runtime-link&lt;/code&gt;: linking statically/dynamically to the C++ standard library and compiler runtime support libraries.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--variant&lt;/code&gt;: Build debug or release version of the binaries.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Including the Boost libraries on Visual Studio
&lt;/h1&gt;

&lt;p&gt;To be able to work with the Boost libraries in Visual Studio, we have to define the root path, that is &lt;code&gt;C:\Program Files\boost\boost_1_76_0&lt;/code&gt;, into each individual project properties. In this guide, we use Visual Studio 2019, but this process can be applied to older versions as well. To include the libraries, do the following:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On a Visual Studio solution → Select the project you want to add the library into → Right Click → "Properties"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The properties window should now be open. An example is attached bellow:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffh8b72jks5qrwt6iehra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffh8b72jks5qrwt6iehra.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we proceed take a moment to ensure that both "&lt;em&gt;Configuration&lt;/em&gt;" and "&lt;em&gt;Platform&lt;/em&gt;" values are selected as shown in the following screenshot. This will apply the changes we're going to make into all the platforms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc70drn105qjb0ewv79iu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc70drn105qjb0ewv79iu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We're going to modify the "&lt;strong&gt;Additional Include Directories&lt;/strong&gt;" option of &lt;strong&gt;"C/C++" → "General"&lt;/strong&gt; property.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpy4h3t7d4dk76q2mbv1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpy4h3t7d4dk76q2mbv1i.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the drop-down button and then click &lt;strong&gt;&amp;lt; Edit.. &amp;gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsq6q5nbwic568gcy7ktd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsq6q5nbwic568gcy7ktd.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have to add the library path and hit OK.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fymz9ja2moc9rznkozfr5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fymz9ja2moc9rznkozfr5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The option value should be updated as shown in the following screenshot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo4wid8ahkzmqkeiylrp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo4wid8ahkzmqkeiylrp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that &lt;code&gt;%(AdditionalIncludeDirectories)&lt;/code&gt; macro was automatically included in the value. This is expected and includes some default values specified by the system.&lt;/p&gt;

&lt;p&gt;We are ready to work with most of  the Boost libraries. We can include them in our source code without any issues. However, if we use libraries that require linking (like &lt;code&gt;boost/chrono&lt;/code&gt;, &lt;code&gt;boost/thread&lt;/code&gt;, etc) we have to link the binaries we built earlier to the linker.&lt;/p&gt;
&lt;h1&gt;
  
  
  Linking the Boost libraries on Visual Studio
&lt;/h1&gt;

&lt;p&gt;This is the final step to our journey. The only think we have to do, is to tell the linker to include our binaries. Like before, open the properties window:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On a Visual Studio solution → Select the project you want to add the library → Right Click → "Properties"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffh8b72jks5qrwt6iehra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffh8b72jks5qrwt6iehra.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we proceed take a moment to ensure that the "&lt;em&gt;Configuration&lt;/em&gt;" and "&lt;em&gt;Platform&lt;/em&gt;" values are selected as shown in the following screenshot. This will apply the changes we're going to make into all the platforms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc70drn105qjb0ewv79iu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc70drn105qjb0ewv79iu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We're going to modify the "&lt;strong&gt;Additional Library Directories&lt;/strong&gt;" option of &lt;strong&gt;"Linker" → "General"&lt;/strong&gt; property.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Note: In case your project builds a static library, the "&lt;/em&gt;&lt;em&gt;Linker&lt;/em&gt;&lt;em&gt;" menu will be shown as "&lt;/em&gt;&lt;em&gt;Librarian&lt;/em&gt;&lt;em&gt;".)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpgxmqazhf8sweb3geau3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpgxmqazhf8sweb3geau3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the drop-down button and then click &lt;strong&gt;&amp;lt; Edit.. &amp;gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxgq2h44uywha85m9n7xn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxgq2h44uywha85m9n7xn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the following value: &lt;code&gt;C:\Program Files\boost\boost_1_76_0\bin\$(PlatformTarget)\lib&lt;/code&gt;, and hit OK.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxywwpxjgbxcynab667ws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxywwpxjgbxcynab667ws.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The property value should be updated as shown in the following screenshot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgmyaowv78m1p8o0vmi2b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgmyaowv78m1p8o0vmi2b.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take a quick note on &lt;code&gt;$(PlatformTarget)&lt;/code&gt; macro. Recall when we built our targets and created two directories, one for each architecture. This macro defines the correct folder for use depending on the selected build platform.&lt;/p&gt;

&lt;p&gt;Note that &lt;code&gt;%(AdditionalLibraryDirectories)&lt;/code&gt; macro was automatically included in the value. This is expected and includes some default values specified by the system.&lt;/p&gt;
&lt;h1&gt;
  
  
  Testing the boost libraries
&lt;/h1&gt;

&lt;p&gt;We should now be able to build and run our projects in both x86 and x64 architectures, targeting both debug and release versions. In case you want a quick, test program, we provide the following example from &lt;code&gt;boost/chrono&lt;/code&gt; library.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Bellow is a running example screenshot using Debug and x64:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrpjdxsnawu8cvi0bhzq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrpjdxsnawu8cvi0bhzq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In this guide we described how we can set up Boost C++ libraries in Windows and link them into Visual Studio projects. We tried to be as detailed as possible in our explanation. We wanted to provide a unified reference for future readers who might encounter the same problems we did.&lt;/p&gt;

&lt;p&gt;As a final note, we inform the readers that we tested all the steps provided in this article, on multiple projects, without any issues. However, if you have a different set-up, that might or might not cause problems. If this is the case, feel free to comment your question or point any ambiguity you have encountered. &lt;/p&gt;

&lt;p&gt;I have plans to write more articles regarding project organization and software development in the future. Feel free to post suggestions and articles ideas you might want to read about.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>There is no such thing as the Spread Operator in JavaScript!</title>
      <dc:creator>George Gkasdrogkas</dc:creator>
      <pubDate>Fri, 10 Apr 2020 15:09:30 +0000</pubDate>
      <link>https://dev.to/georgegkas/there-is-no-such-thing-as-the-spread-operator-in-javascript-23a</link>
      <guid>https://dev.to/georgegkas/there-is-no-such-thing-as-the-spread-operator-in-javascript-23a</guid>
      <description>&lt;p&gt;Have you heard about the Spread Syntax? Introduced in ES2015, we love it due to its simple semantics and ubiquitous use cases. What about the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Spread_operator"&gt;Spread Operator&lt;/a&gt;? Yes, &lt;a href="https://guide.freecodecamp.org/javascript/es6/spread-operator/"&gt;it's the three dots&lt;/a&gt; (&lt;code&gt;...&lt;/code&gt;) used by the Spread Syntax!&lt;/p&gt;

&lt;p&gt;…and by saying such things, we start digging into the horrifying world of &lt;a href="https://www.youtube.com/watch?v=cXWRAb84TTE"&gt;a bug's life&lt;/a&gt;…&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick review of the Spread Syntax
&lt;/h2&gt;

&lt;p&gt;A simple use case where we can leverage the use of the Spread Syntax is when we want to concatenate multiple arrays. Check the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;clientErrors&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="s1"&gt;err1&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;err2&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;err3&lt;/span&gt;&lt;span class="dl"&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;serverErrors&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="s1"&gt;err4&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;err5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;numberOfErrors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientErrors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serverErrors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Assuming that both inputs are arrays to prevent TypeErrors.&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;clientErrors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;serverErrors&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="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;numberOfErrors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientErrors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serverErrors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;numberOfErrors&lt;/code&gt; concatenates two arrays and returns the length of the new array. But what if the parameters are falsy values, like &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;clientErrors&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="s1"&gt;err1&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;err2&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;err3&lt;/span&gt;&lt;span class="dl"&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;serverErrors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;numberOfErrors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientErrors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serverErrors&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;clientErrors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;serverErrors&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="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;numberOfErrors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientErrors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serverErrors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; TypeError&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We know that if we try to spread a &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; variable, this will make the interpreter nag. In real world scenarios, we want to guard ourselves from such edge cases. With a minor tweak, we end up writing something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;clientErrors&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="s1"&gt;err1&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;err2&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;err3&lt;/span&gt;&lt;span class="dl"&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;serverErrors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;numberOfErrors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientErrors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serverErrors&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;clientErrors&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="nx"&gt;serverErrors&lt;/span&gt; &lt;span class="o"&gt;||&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="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;numberOfErrors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientErrors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serverErrors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because &lt;code&gt;serverErrors&lt;/code&gt; is falsy, the logical OR operator will return an empty array, which then will be spread gracefully. The final result from calling &lt;code&gt;numberOfErrors&lt;/code&gt; is equal to the length of the &lt;code&gt;clientErrors&lt;/code&gt; array, which is &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Spread Operator Precedence
&lt;/h2&gt;

&lt;p&gt;Now that we covered a basic example, let's see something more interesting. For each of the following questions, mark the correct answer. The solutions will be presented immediately after. &lt;em&gt;(&lt;/em&gt;&lt;em&gt;Hint&lt;/em&gt;&lt;em&gt;: You can run the code snippets and see the results yourself!)&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Question A
&lt;/h3&gt;



&lt;div class="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;a1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;b1&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;c1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What is the value of &lt;code&gt;c1&lt;/code&gt;?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;c1&lt;/code&gt; has no value. The expression &lt;code&gt;...a1&lt;/code&gt; will throw &lt;code&gt;TypeError&lt;/code&gt;, because &lt;code&gt;a1&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;c1&lt;/code&gt; is &lt;code&gt;[1, 2]&lt;/code&gt;. The expression &lt;code&gt;a1 || b1&lt;/code&gt; will be evaluated first, which then will return &lt;code&gt;[1, 2]&lt;/code&gt;, which will be spread.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Question B
&lt;/h3&gt;



&lt;div class="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;a2&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;c2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;a2&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;c2&lt;/code&gt; is &lt;code&gt;[1, 2]&lt;/code&gt;. The expression &lt;code&gt;a2 || b2&lt;/code&gt; will be evaluated first, which will be spread.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;c2&lt;/code&gt; is &lt;code&gt;[1, 2]&lt;/code&gt;. The expression &lt;code&gt;…a2&lt;/code&gt; will be evaluated first, which will be spread.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Question C
&lt;/h2&gt;



&lt;div class="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;a3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;b3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;c3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;c3&lt;/code&gt; has no value. The expression &lt;code&gt;...a3&lt;/code&gt; will throw &lt;code&gt;TypeError&lt;/code&gt;, because &lt;code&gt;a3&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;c3&lt;/code&gt; has no value. The expression &lt;code&gt;a3 || b3&lt;/code&gt; will evaluate first which will return &lt;code&gt;null&lt;/code&gt; and then the Spread Syntax will throw &lt;code&gt;TypeError&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Answers
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A. 2
B. 1 
C. 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If it happens that you have not answered correctly to at least one of the above questions, then you might have fallen into the trap of the operator precedence. Does the dots punctuator &lt;code&gt;…&lt;/code&gt; have higher precedence over the logical OR &lt;code&gt;||&lt;/code&gt;, or is the other way around? What is the precedence of the Spread Operator? The correct answers is: &lt;strong&gt;It does not matter, because there is no such thing as Spread Operator in JavaScript!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Spread Operator does not exist!
&lt;/h2&gt;

&lt;p&gt;When we try to evaluate expressions like &lt;code&gt;[…array || []]&lt;/code&gt; it's logical to examine the precedence of our operators. There exist a common misconception in the Web regarding the Spread Syntax, which is presented as an operator.&lt;/p&gt;

&lt;p&gt;A &lt;a href="https://stackoverflow.com/questions/44934828/is-it-spread-syntax-or-the-spread-operator/44934830#44934830"&gt;great answer&lt;/a&gt; was posted in Stack Overflow by &lt;a href="https://stackoverflow.com/users/5647260/andrew-li"&gt;Andrew Li&lt;/a&gt;, which is worth mentioning and summarizes the nature of the Spread Syntax.&lt;/p&gt;

&lt;p&gt;One of the most memorable arguments can be retrieved directly by the ECMAScript 2015 specification itself:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The complete list of operators is listed in Clauses §12.5 through §12.15 in the  &lt;a href="http://www.ecma-international.org/ecma-262/6.0/"&gt;ECMAScript 2015 Language Specification&lt;/a&gt;, the specification in which &lt;code&gt;…&lt;/code&gt; is introduced, which doesn't mention &lt;code&gt;...&lt;/code&gt;. - &lt;a href="https://stackoverflow.com/questions/44934828/is-it-spread-syntax-or-the-spread-operator/44934830#44934830"&gt;Andrew Li answer&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another worth-mentioning point is that &lt;em&gt;"An operator is a builtin function [..] that **evaluates to exactly one value&lt;/em&gt;&lt;em&gt;."&lt;/em&gt;. If we try to run a statement like &lt;code&gt;const a = …b&lt;/code&gt; in our Web Console, where &lt;code&gt;b&lt;/code&gt; is an array, then we'll &lt;code&gt;SyntaxError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The way that the Spread Syntax works, is by evaluating its arguments first, and then spreading the result. Thus, &lt;code&gt;[…a || b]&lt;/code&gt; behaves exactly the same way as &lt;code&gt;[…(a || b)]&lt;/code&gt;. Putting a set of parentheses around &lt;code&gt;a || b&lt;/code&gt; expression helps to remove the ambiguity.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As a practical reference, Spread Syntax's arguments are evaluated first and then spread.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>An Example-Based Introduction to Finite-State Machines</title>
      <dc:creator>George Gkasdrogkas</dc:creator>
      <pubDate>Sat, 08 Feb 2020 16:01:33 +0000</pubDate>
      <link>https://dev.to/georgegkas/an-example-based-introduction-to-finite-state-machines-8bc</link>
      <guid>https://dev.to/georgegkas/an-example-based-introduction-to-finite-state-machines-8bc</guid>
      <description>&lt;p&gt;If you are enrolled in a CS program or happen to be fascinated by the aspects of theoretical Computer Science, you would most likely be introduced at some point to finite-state machines. It also might be the case that you were overwhelmed by their principles. But considering how ubiquitous this simple computational model is, it's time to unravel its mysteries once and for all!&lt;/p&gt;

&lt;h2&gt;
  
  
  An everyday example
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iDF5TSkj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AD08bFOjJDdmLtyb1KcBKqQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iDF5TSkj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AD08bFOjJDdmLtyb1KcBKqQ.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's Saturday morning. You'll wake up early today to enjoy the benefits of a morning walk, along the seaside. The clock rings at 8 am. Your eyes open, but your body feels quite relaxed under the sheets. For the next few moments, the motion of the second hand in the alarm clock is ticking rhythmically in your ears.&lt;/p&gt;

&lt;p&gt;You finally decide to leave the bed and get ready. It's time to go, you're inserting the key in the front door to lock it, but the key does not match. You're using the wrong key! It doesn't take more than a few seconds to find the correct one and with a gentle motion, you turn the key to the right.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Es6ep3cJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A1iMRXDi7Ka40_idk7LelYA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Es6ep3cJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A1iMRXDi7Ka40_idk7LelYA.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The beach is not far from home, but quite enough to make your head quickly to the driver's seat and start the engine. The alarm beeps reminding you to fasten the seat belt. You comply successfully with the demands of your vehicle.&lt;/p&gt;

&lt;p&gt;Heading to your favorite spot, where the sea ​​breeze leaves a gentle, liquid sense in the air, way off the crowded area you live in, the yellow color of the traffic lights suggests slowing your speed. After two seconds, the traffic lights turn red, commanding you to stop. On your left, two people are buying from the vending machine near the coffee shop, where a couple is playing a board game. The lights turn green and you continue the ride to the desired location.&lt;/p&gt;

&lt;p&gt;When you finally reach it, all the previous images start crossing your mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the alarm clock&lt;/li&gt;
&lt;li&gt;the key hole&lt;/li&gt;
&lt;li&gt;the car commanding you to fasten the seat belt&lt;/li&gt;
&lt;li&gt;the traffic lights&lt;/li&gt;
&lt;li&gt;the vending machine&lt;/li&gt;
&lt;li&gt;the board game&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;You suddenly make the connection! They all represent some kind of finite-state machine.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Finite-State Machines (FSMs)
&lt;/h2&gt;

&lt;p&gt;Think of a computer with extremely limited memory capacity. A device with such limited resources can seem inefficient at first glance but don't rush to a conclusion yet. What makes a machine like this special in a world where every computer tends to be big, complicated and fancy, is the simplicity of its design.&lt;/p&gt;

&lt;p&gt;You see, modern computers are extremely complex to allow us to create a feasible mathematical theory of them directly. Sometimes scientists need only the basics to work on a solution. For that reason, we created an idealized computer, a &lt;a href="https://en.wikipedia.org/wiki/Computational_model"&gt;computational model&lt;/a&gt;. The simplest model is called &lt;strong&gt;finite-state machine (FSM)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can visualize one using state diagrams. Look at the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u9J_3xTS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A4iMdmwZiR_8eexWTyFspYg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u9J_3xTS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A4iMdmwZiR_8eexWTyFspYg.png" alt="State diagram representing a finite-state machine. The circles represent states, where the arrows represent transitions. - Screenshot from graph IT editor."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For those who know the basics of graph theory, this is clearly a &lt;a href="https://en.wikipedia.org/wiki/Directed_graph"&gt;directed graph&lt;/a&gt;. We can see the nodes, represented by circles, and edges, represented by arrows. But unlike graphs, in state diagrams, we call them &lt;strong&gt;states&lt;/strong&gt; and &lt;strong&gt;transitions&lt;/strong&gt; respectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;States&lt;/strong&gt; represent the status of a system. For instance, automatic sliding doors in supermarkets have two states: either close or open. Traffic lights contain three states: red, yellow, green. More sophisticated examples can contain quite a lot of states, but never an infinite number. Having no states doesn't make sense either.&lt;/p&gt;

&lt;p&gt;When a finite-state machine is running, one or many states are active in each execution step. Those active states represent the current value of the system.&lt;/p&gt;

&lt;p&gt;In the first execution step, a default state, called the &lt;strong&gt;starting state&lt;/strong&gt;, is being activated. In state diagrams, those states are indicated by an arrow pointing to them. In the above diagram, state &lt;code&gt;s&lt;/code&gt; is the starting state.&lt;/p&gt;

&lt;p&gt;Finite-state machines may also contain one or multiple &lt;strong&gt;accepting states&lt;/strong&gt;. Accepting states are represented by a double circle, like states &lt;code&gt;p&lt;/code&gt; and &lt;code&gt;r&lt;/code&gt;. A finite-state machine &lt;strong&gt;is not required&lt;/strong&gt; to have accepting states; it might be designed to run indefinitely. The purpose of accepting states is quite straight forward: when the processing ends, depending on if we are on any accepting states or not, the output of the finite-state machine is either &lt;em&gt;accept&lt;/em&gt; or &lt;em&gt;reject&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;States are connected with each other using &lt;strong&gt;transitions&lt;/strong&gt;. Each transition contains a set of &lt;strong&gt;symbols&lt;/strong&gt;. The set of all symbols presented in the FSM is called the &lt;strong&gt;alphabet&lt;/strong&gt;. When the source state is active and the transition is executed, it changes the active state from the source state to the destination state only if the current input matches the symbol of the transition.&lt;/p&gt;

&lt;p&gt;It's like using a vending machine:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eXZ3HrIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A2B_sUJh72FXAXm05lop8OA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eXZ3HrIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A2B_sUJh72FXAXm05lop8OA.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The active state when you first approach the machine is &lt;code&gt;idle&lt;/code&gt;, which happens to also be the starting state. By inserting coins, you move to the next state &lt;code&gt;choose product&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To recap, finite-state machines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;have a set of &lt;strong&gt;states&lt;/strong&gt; and &lt;strong&gt;transitions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;have &lt;strong&gt;at least one&lt;/strong&gt; state&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;cannot have an infinite&lt;/strong&gt; number of states&lt;/li&gt;
&lt;li&gt;have one &lt;strong&gt;starting state&lt;/strong&gt;, which is the first state that is activated upon their execution&lt;/li&gt;
&lt;li&gt;may or may not have &lt;strong&gt;accepting states&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;have for each transition a set of &lt;strong&gt;symbols&lt;/strong&gt;,&lt;/li&gt;
&lt;li&gt;have an &lt;strong&gt;alphabet&lt;/strong&gt; which is the union of all the sets of transition symbols&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two types of finite-state machines: &lt;strong&gt;deterministic&lt;/strong&gt; and &lt;strong&gt;non-deterministic&lt;/strong&gt;. Based on those types, the execution is handled differently. Remember that finite-state machines are a computational model: they calculate an output based on the provided input. Below we discuss these two types and provide a complete running example for each one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deterministic Finite Automatons (DFAs)
&lt;/h2&gt;

&lt;p&gt;The first type of finite-state machine is the &lt;strong&gt;Deterministic Finite Automaton (DFA)&lt;/strong&gt;. The word deterministic has quite a value here. It means that you can transition from one state to the next while not being in multiple states concurrently. Think the example with the traffic signals: they can't be in green and red state at the same time.&lt;/p&gt;

&lt;p&gt;Let's take a look at the bellow automaton:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DZFQKgDK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AisIk7xWax5rXgTP6TCNSUQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DZFQKgDK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AisIk7xWax5rXgTP6TCNSUQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above DFA contains 4 states. Starting state is &lt;code&gt;s&lt;/code&gt; and accepting state is &lt;code&gt;r&lt;/code&gt;. The alphabet contains the symbols &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Observe that for each state, there is only one transition arrow for each symbol. In some cases, a transition arrow can contain multiple symbols, like the one from &lt;code&gt;q&lt;/code&gt; to &lt;code&gt;r&lt;/code&gt;, but this does not break the requirement as there is no other exiting transition from q with the same symbols (&lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;In deterministic finite automatons, the transition from one state to another can happen only if the input matches the symbol(s) of the transition. For instance, we can move from &lt;code&gt;s&lt;/code&gt; to &lt;code&gt;q&lt;/code&gt; only if the current symbol of the input is &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A running example
&lt;/h2&gt;

&lt;p&gt;But enough with the theory! Let us see what the above DFA outputs for the input &lt;strong&gt;baabb&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tVBc7liD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AwSNq4oGerm09w356sNyDVA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tVBc7liD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AwSNq4oGerm09w356sNyDVA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the execution begins, the starting state is being activated. To run the automaton, for each symbol in the input, from left-to-right, we're going to see if there is an exiting transition from the active state that match this symbol.&lt;/p&gt;

&lt;p&gt;The first symbol of the input &lt;strong&gt;baabb&lt;/strong&gt; is &lt;code&gt;b&lt;/code&gt;. The current active state is &lt;code&gt;s&lt;/code&gt;. Is there any exiting transition from &lt;code&gt;s&lt;/code&gt; that contains the symbol &lt;code&gt;b&lt;/code&gt;? Yes, the one that connects &lt;code&gt;s&lt;/code&gt; to &lt;code&gt;q&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DtRBANFj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AoxU7qyXB6Jcu-4tBEnzDYw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DtRBANFj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AoxU7qyXB6Jcu-4tBEnzDYw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The current active state is &lt;code&gt;q&lt;/code&gt;. The next symbol of the input &lt;strong&gt;baabb&lt;/strong&gt; is &lt;code&gt;a&lt;/code&gt;. Is there any exiting transition from &lt;code&gt;q&lt;/code&gt; that match the symbol &lt;code&gt;a&lt;/code&gt;? Yes, the one that connects &lt;code&gt;q&lt;/code&gt; with &lt;code&gt;p&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--85xiVSae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A5Yxzzc-OQBFtqqw_lk3GIg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--85xiVSae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A5Yxzzc-OQBFtqqw_lk3GIg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The current active state is &lt;code&gt;p&lt;/code&gt;. The next symbol of the input &lt;strong&gt;baabb&lt;/strong&gt; is &lt;code&gt;a&lt;/code&gt;. Is there any exiting transition from &lt;code&gt;p&lt;/code&gt; that match the symbol &lt;code&gt;a&lt;/code&gt;? Yes, the one that connects &lt;code&gt;p&lt;/code&gt; with itself.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RKnumqDb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A41L19NseQxJ2dK8DCNLQPg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RKnumqDb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A41L19NseQxJ2dK8DCNLQPg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The current active state is p. The next symbol of the input &lt;strong&gt;baabb&lt;/strong&gt; is &lt;code&gt;b&lt;/code&gt;. Is there any exiting transition from &lt;code&gt;p&lt;/code&gt; that match the symbol &lt;code&gt;b&lt;/code&gt;? Yes, the one that connects &lt;code&gt;p&lt;/code&gt; with &lt;code&gt;q&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2BAHXw79--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A9xkwJL94kDlWtUVDBHM2Sw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2BAHXw79--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A9xkwJL94kDlWtUVDBHM2Sw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The current active state is &lt;code&gt;q&lt;/code&gt;. The next symbol of the input &lt;strong&gt;baabb&lt;/strong&gt; is &lt;code&gt;b&lt;/code&gt;. Is there any exiting transition from &lt;code&gt;q&lt;/code&gt; that match the symbol &lt;code&gt;b&lt;/code&gt;? Yes, the one that connects &lt;code&gt;q&lt;/code&gt; with &lt;code&gt;r&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EeFa6xpX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AZNZAwQNXgS1yucqPz0d46w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EeFa6xpX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AZNZAwQNXgS1yucqPz0d46w.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point we have traverse all the symbols of the input. The final active state is &lt;code&gt;r&lt;/code&gt;. Is &lt;code&gt;r&lt;/code&gt; an accepting state? Yes it is! That means the output is &lt;em&gt;accept&lt;/em&gt;. In case that after traversing the input the final state was not an accepting state, the output would be &lt;em&gt;reject&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Formal definition
&lt;/h3&gt;

&lt;p&gt;It would be a shame not to mention the formal definition of a DFA, now that we have covered all the required details to do so.&lt;/p&gt;




&lt;p&gt;A deterministic automaton is a 5-tuple ⟨&lt;strong&gt;Q&lt;/strong&gt;, &lt;strong&gt;Σ&lt;/strong&gt;, &lt;strong&gt;δ&lt;/strong&gt;, &lt;strong&gt;q₀&lt;/strong&gt;, &lt;strong&gt;F&lt;/strong&gt;⟩, where&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Q&lt;/strong&gt; is a finite set of all states&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Σ&lt;/strong&gt; is a finite set of all symbols, the alphabet&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;δ: Q × Σ → Q&lt;/strong&gt; is the transition function&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;q₀ ∈ Q&lt;/strong&gt; is the starting state&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;F ⊆ Q&lt;/strong&gt; is the set of accepting states&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;What may bother you in the above definition, is the transition function. Remember that we can move from one state to another only if the current input matches the symbol of the transition. For instance, consider that a finite automaton has a transition labeled &lt;code&gt;1&lt;/code&gt; from a state &lt;code&gt;p&lt;/code&gt; to a state &lt;code&gt;q&lt;/code&gt;, then we can indicate the same thing with a transition function &lt;em&gt;δ(p, 1) = q&lt;/em&gt;. This notion is just a kind of mathematical shorthand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-deterministic Finite Automatons (NFAs)
&lt;/h2&gt;

&lt;p&gt;The second type of finite-state machine is the &lt;strong&gt;Non-deterministic Finite Automaton (NFA)&lt;/strong&gt;. Compared to deterministic finite automatons, there are some major differences.&lt;/p&gt;

&lt;p&gt;Consider the following automaton:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5jdCX6sx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AtsKOgEANHDA2eJ-giwf0_Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5jdCX6sx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AtsKOgEANHDA2eJ-giwf0_Q.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above NFA contains 5 states. Starting state is &lt;code&gt;s&lt;/code&gt; and accepting states are &lt;code&gt;v&lt;/code&gt; and &lt;code&gt;d&lt;/code&gt;. The alphabet contains the symbols &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But wait a moment! What is this strange label &lt;code&gt;ε&lt;/code&gt;? It is a Greek character called &lt;strong&gt;epsilon&lt;/strong&gt; and is not part of the alphabet. In non-deterministic automatons, transitions can be labeled either with a symbol from the alphabet or &lt;code&gt;ε&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In non-deterministic automatons, there are cases where more than one transitions are applicable. In those cases, the automaton splits into multiple copies of itself and continues to run in non-determinism manner like before. We can see this behavior as a kind of parallel computation, where the machine is &lt;a href="https://en.wikipedia.org/wiki/Fork%E2%80%93join_model"&gt;forking&lt;/a&gt; into several children, each processing independently.&lt;/p&gt;

&lt;p&gt;Label &lt;code&gt;ε&lt;/code&gt; is the first difference between deterministic and non-deterministic automatons. The second difference is the use of multiple transition arrows that contain the same symbols for each state in the NFA. For instance, state &lt;code&gt;s&lt;/code&gt; has two exiting transitions labeled with the symbol &lt;code&gt;a&lt;/code&gt;. This is the opposite of a DFA which has only one symbol per exiting transition for each state.&lt;/p&gt;

&lt;h3&gt;
  
  
  A running example
&lt;/h3&gt;

&lt;p&gt;A running example will clear any misconceptions.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5jdCX6sx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AtsKOgEANHDA2eJ-giwf0_Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5jdCX6sx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AtsKOgEANHDA2eJ-giwf0_Q.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the execution begins, the starting state is being activated. We're going to check if there are any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;s&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is there any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;s&lt;/code&gt;? Yes, the one that connects &lt;code&gt;s&lt;/code&gt; to &lt;code&gt;w&lt;/code&gt;. Activate state &lt;code&gt;w&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the new state, we'll do the same procedure until no more &lt;code&gt;ε&lt;/code&gt; transitions exist.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is there any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;w&lt;/code&gt;? No, there isn't.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The initial active states are &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;w&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M0oU-Ixv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AIKo621QD8xYBLbAX_mku2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M0oU-Ixv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AIKo621QD8xYBLbAX_mku2g.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For each symbol of the input, from left-to-right, we'll check if there are exiting transitions from the active states that match that symbol. Then we'll follow the previous process and activate all the states that connect with &lt;em&gt;epsilon transitions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The first symbol of the input &lt;strong&gt;aabc&lt;/strong&gt; is &lt;code&gt;a&lt;/code&gt;. The current active states are &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;w&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;s&lt;/code&gt; that contains the symbol &lt;code&gt;a&lt;/code&gt;? Yes, the ones that connect &lt;code&gt;s&lt;/code&gt; to &lt;code&gt;w&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;w&lt;/code&gt; that contains the symbol &lt;code&gt;a&lt;/code&gt;? No, there isn't.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the new activated states &lt;code&gt;w&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt;, we're going to follow all the epsilon transitions and repeat until we run out of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;w&lt;/code&gt;? No, there isn't.&lt;/li&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;a&lt;/code&gt;? Yes, the one that connects &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;v&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;v&lt;/code&gt;? Yes, the one that connects &lt;code&gt;v&lt;/code&gt; to itself. Because this transition forms a self loop, we won't test &lt;code&gt;v&lt;/code&gt; again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The new active states are &lt;code&gt;w&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jtoDlgrw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A1CuSc74Yp2SwD4uk9lPzCw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jtoDlgrw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A1CuSc74Yp2SwD4uk9lPzCw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;The next symbol of the input &lt;strong&gt;aabc&lt;/strong&gt; is &lt;code&gt;a&lt;/code&gt;. The current active states are &lt;code&gt;w&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;w&lt;/code&gt; that contains the symbol &lt;code&gt;a&lt;/code&gt;? No.&lt;/li&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;a&lt;/code&gt; that contains the symbol &lt;code&gt;a&lt;/code&gt;? Yes, the ones that connect &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;d&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;v&lt;/code&gt; that contains the symbol &lt;code&gt;a&lt;/code&gt;? No.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the new activated states &lt;code&gt;d&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;, we're going to follow all the epsilon transitions and repeat until we run out of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;d&lt;/code&gt;? No.&lt;/li&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;v&lt;/code&gt;? Yes, the one that connects &lt;code&gt;v&lt;/code&gt; to itself. Because this transition forms a self loop, we won't test &lt;code&gt;v&lt;/code&gt; again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The new active states are &lt;code&gt;d&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JGcjESSd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AYR4bIPghPZM-RpuP96CcCg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JGcjESSd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AYR4bIPghPZM-RpuP96CcCg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;The next symbol of the input &lt;strong&gt;aabc&lt;/strong&gt; is &lt;code&gt;b&lt;/code&gt;. The current active states are &lt;code&gt;d&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;d&lt;/code&gt; that contains the symbol &lt;code&gt;b&lt;/code&gt;? Yes, the one that connect &lt;code&gt;d&lt;/code&gt; to &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;v&lt;/code&gt; that contains the symbol &lt;code&gt;b&lt;/code&gt;? Yes, the one that connect &lt;code&gt;v&lt;/code&gt; to &lt;code&gt;a&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the new activated states &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt;, we're going to follow all the epsilon transitions and repeat until we run out of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;s&lt;/code&gt;? Yes, the one that connects to &lt;code&gt;w&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;w&lt;/code&gt;? No.&lt;/li&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;v&lt;/code&gt;? Yes, the ones that connects &lt;code&gt;v&lt;/code&gt; to itself and &lt;code&gt;a&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;a&lt;/code&gt;? Yes, the one that connects to &lt;code&gt;v&lt;/code&gt;. Because we already tested &lt;code&gt;v&lt;/code&gt; we won't test it again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The new active states are &lt;code&gt;s&lt;/code&gt;, &lt;code&gt;w&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZvUfpHVA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AT9IiGgyQHeaehEe5YCd2Kw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZvUfpHVA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AT9IiGgyQHeaehEe5YCd2Kw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;The last symbol of the input &lt;strong&gt;aabc&lt;/strong&gt; is &lt;code&gt;c&lt;/code&gt;. The current active states are &lt;code&gt;s&lt;/code&gt;, &lt;code&gt;w&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;s&lt;/code&gt; that contains the symbol &lt;code&gt;c&lt;/code&gt;? No.&lt;/li&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;w&lt;/code&gt; that contains the symbol &lt;code&gt;c&lt;/code&gt;? No.&lt;/li&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;a&lt;/code&gt; that contains the symbol &lt;code&gt;c&lt;/code&gt;? No.&lt;/li&gt;
&lt;li&gt;Any exiting transition from &lt;code&gt;v&lt;/code&gt; that contains the symbol &lt;code&gt;c&lt;/code&gt;? Yes, the one that connects to &lt;code&gt;a&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the new activated state &lt;code&gt;a&lt;/code&gt;, we're going to follow all the epsilon transitions and repeat until we run out of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;a&lt;/code&gt;? Yes, the one that connects to &lt;code&gt;v&lt;/code&gt; .&lt;/li&gt;
&lt;li&gt;Any &lt;code&gt;ε&lt;/code&gt; transition from &lt;code&gt;v&lt;/code&gt;? Yes, the one that connects &lt;code&gt;v&lt;/code&gt; to itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---Z5ABuun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A6QzpjOAKO_WdIxlN4BvbbA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---Z5ABuun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A6QzpjOAKO_WdIxlN4BvbbA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;The final active states are &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;. Is any of them an accepting state? Yes, state &lt;code&gt;v&lt;/code&gt; is an accepting state. That means the output is &lt;em&gt;accept&lt;/em&gt;. In case that after traversing the input the final state was not an accepting state, the output would be &lt;em&gt;reject&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Formal definition
&lt;/h3&gt;

&lt;p&gt;The formal definition of a non-deterministic automaton is similar to the definition of a deterministic one. It consists of states, transitions, a transition function, an alphabet, and some accepting states. The only difference is the kind of transition function.&lt;/p&gt;




&lt;p&gt;A non-deterministic automaton is a 5-tuple ⟨&lt;strong&gt;Q&lt;/strong&gt;, &lt;strong&gt;Σ&lt;/strong&gt;, &lt;strong&gt;δ&lt;/strong&gt;, &lt;strong&gt;q₀&lt;/strong&gt;, &lt;strong&gt;F&lt;/strong&gt;⟩, where&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Q&lt;/strong&gt; is a finite set of all states&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Σ&lt;/strong&gt; is a finite set of all symbols, the alphabet&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;δ: Q × Σε → P(Q)&lt;/strong&gt; is the transition function&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;q₀ ∈ Q&lt;/strong&gt; is the starting state&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;F ⊆ Q&lt;/strong&gt; is the set of accepting states&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;In deterministic automatons, the transition function takes as input a state, a symbol and returns the next state, as output. In non-deterministic automatons, the transition function takes as input a state, a symbol or &lt;code&gt;ε&lt;/code&gt; and returns the set of all possible next states, as output.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u8_3KdVx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A7H9ETkvfOwIdnebl-aZn3g.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u8_3KdVx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A7H9ETkvfOwIdnebl-aZn3g.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article was intended to provide you with all the required details and examples to get a better understanding of Finite-state Machines. Thank you for taking the time to read it. 🙏&lt;/p&gt;

</description>
      <category>computerscience</category>
    </item>
    <item>
      <title>Anonymous Web Scraping with Node.js, Tor, Puppeteer and cheerio</title>
      <dc:creator>George Gkasdrogkas</dc:creator>
      <pubDate>Sat, 25 Jan 2020 20:47:27 +0000</pubDate>
      <link>https://dev.to/georgegkas/anonymous-web-scraping-with-node-js-tor-puppeteer-and-cheerio-bge</link>
      <guid>https://dev.to/georgegkas/anonymous-web-scraping-with-node-js-tor-puppeteer-and-cheerio-bge</guid>
      <description>&lt;p&gt;Web Scraping is the technique of extracting data from websites. The term is used typically for automated data extraction. Today, I am going to show you how to crawl websites anonymously. The reason why you want to hide your identity is due to the fact that many web servers apply rules to websites which ban IPs after a certain amount of continuous requests. We are going to use &lt;a href="https://github.com/puppeteer/puppeteer"&gt;Puppeteer&lt;/a&gt; for accessing web pages, &lt;a href="https://github.com/cheeriojs/cheerio"&gt;cheerio&lt;/a&gt; for HTML parsing, and &lt;a href="https://www.torproject.org/"&gt;Tor&lt;/a&gt; to run each request from a different IP address.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;While the legal aspects of Web Scraping vary, with many grey zones, &lt;br&gt;
remember to always respect the &lt;a href="https://en.wikipedia.org/wiki/Terms_of_service"&gt;Terms of Service&lt;/a&gt; of each web page you &lt;br&gt;
scrape. &lt;a href="https://twitter.com/mbenbernard"&gt;Ben Bernard&lt;/a&gt; has wrote a &lt;a href="https://benbernardblog.com/web-scraping-and-crawling-are-perfectly-legal-right/"&gt;nice article&lt;/a&gt; about those legal issues.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Setting Up Tor
&lt;/h2&gt;

&lt;p&gt;First things first, we have to install our Tor client by using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;tor
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Configure Tor
&lt;/h2&gt;

&lt;p&gt;Next, we are going to configure our Tor client. The default Tor configuration uses a &lt;a href="https://en.wikipedia.org/wiki/SOCKS"&gt;SOCKS&lt;/a&gt; port to provide us with one circuit to a single exit node (i.e. one IP address). This is handy for everyday use, like browsing, but for our specific scenario we need multiple IP addresses, so that we can switch between them while scraping.&lt;/p&gt;

&lt;p&gt;To do this, we'll simply open additional ports to listen for &lt;a href="https://en.wikipedia.org/wiki/SOCKS"&gt;SOCKS&lt;/a&gt; connections. This is done by adding multiple &lt;code&gt;SocksPort&lt;/code&gt; options to main configuration file under &lt;code&gt;/etc/tor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Open &lt;code&gt;/etc/tor/torrc&lt;/code&gt; file with your preferred editor and add the next lines in the end of the file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;There a couple of things to notice here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The value of each &lt;code&gt;SocksPort&lt;/code&gt; is a number, the port that Tor will listen for connections from SOCKS-speaking applications, like browsers.&lt;/li&gt;
&lt;li&gt;Because &lt;code&gt;SocksPort&lt;/code&gt; value is a port to be open, the port must not already be used by another process.&lt;/li&gt;
&lt;li&gt;The initial port starts with value &lt;code&gt;9050&lt;/code&gt;. This is the default SOCKS of the Tor client.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;We bypass value &lt;code&gt;9051&lt;/code&gt;&lt;/strong&gt;. This port is used by Tor to allow external applications who are connected to this port to control Tor process.&lt;/li&gt;
&lt;li&gt;As a simple convention, to open more ports, we increment each value after &lt;code&gt;9051&lt;/code&gt; by one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Restart the tor client to apply the new changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; /etc/init.d/tor restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Create a new Node project
&lt;/h2&gt;

&lt;p&gt;Create a new directory for your project, I’ll call it &lt;code&gt;superWebScraping&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;superWebScraping
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Navigate to &lt;code&gt;superWebScraping&lt;/code&gt; and initialize an empty Node project.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;superWebScraping &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Install the required dependencies.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;--save&lt;/span&gt; puppeteer cheerio
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Browse with Puppeteer
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/puppeteer/puppeteer"&gt;Puppeteer&lt;/a&gt; is a &lt;a href="https://developers.google.com/web/updates/2017/04/headless-chrome"&gt;headless browser&lt;/a&gt; that uses &lt;a href="https://chromedevtools.github.io/devtools-protocol/"&gt;DevTools Protocol&lt;/a&gt; to communicate with &lt;a href="https://www.google.com/chrome/"&gt;Chrome&lt;/a&gt; or &lt;a href="https://www.chromium.org/"&gt;Chromium&lt;/a&gt;. The reason why we don’t use a request library, like &lt;a href="https://www.npmjs.com/package/tor-request"&gt;tor-request&lt;/a&gt;, is due to the fact that request libraries cannot process SPA websites that load their content dynamically.&lt;/p&gt;

&lt;p&gt;Create an &lt;code&gt;index.js&lt;/code&gt; file and add the below script. The statements are documented inline.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Run the script with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;node index.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You should see the Chromium browser navigating to &lt;a href="https://api.ipify.org"&gt;https://api.ipify.org&lt;/a&gt; like the following screenshot&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y6e6a6GZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1365/1%2AYTPeS730jihtRUQJ28ThZw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y6e6a6GZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1365/1%2AYTPeS730jihtRUQJ28ThZw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is a reason why I chose the specific web page in my example. &lt;a href="https://api.ipify.org"&gt;https://api.ipify.org&lt;/a&gt; is able to send us our public IP address. This is the IP you are browsing the web, without using Tor.&lt;/p&gt;

&lt;p&gt;Change the above code by adding the following key in &lt;code&gt;puppeteer.launch&lt;/code&gt; statement:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We provide the &lt;code&gt;--proxy-server&lt;/code&gt; argument to our browser. The value of this argument tells the browser to use a socks5 proxy in our local machine on top of port &lt;code&gt;9050&lt;/code&gt;. The value of the port is one of the values we provided earlier in &lt;code&gt;torrc&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Now run again the script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;node index.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This time you should see a different IP address. This is the IP that comes with Tor circuit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AynlicJp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1365/1%2AVudGr6lBoAlP4Djc_VX9Pw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AynlicJp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1365/1%2AVudGr6lBoAlP4Djc_VX9Pw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mine is &lt;code&gt;144.217.7.33&lt;/code&gt;, but you might have a different one. Note that if you run the script again on the same port &lt;code&gt;9050&lt;/code&gt; you'll get the same IP address as before.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AynlicJp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1365/1%2AVudGr6lBoAlP4Djc_VX9Pw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AynlicJp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1365/1%2AVudGr6lBoAlP4Djc_VX9Pw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the reason why we opened many ports in our Tor configuration. Try to use another port, like &lt;code&gt;9051&lt;/code&gt;. The IP will not be the same.&lt;/p&gt;
&lt;h2&gt;
  
  
  Scrape content with Cheerio
&lt;/h2&gt;

&lt;p&gt;Now that we have a nice way to get our pages, it's time to scrape them. We are going to use the &lt;a href="https://github.com/cheeriojs/cheerio"&gt;cheerio&lt;/a&gt; library. Cheerio is an HTML parser designed to use the same API as &lt;a href="https://jquery.com/"&gt;jQuery&lt;/a&gt;. Our task is to scrape the last 5 post titles of &lt;a href="https://news.ycombinator.com/"&gt;Hacker News&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's navigate to &lt;a href="https://news.ycombinator.com/"&gt;Hacker News&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rMl5F2pm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1366/1%2ACbpk6VOBeRabUQaLMXLhjQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rMl5F2pm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1366/1%2ACbpk6VOBeRabUQaLMXLhjQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We want to scrape the first 5 titles ("HAKMEM (1972), "Larry Roberts has died", etc). Inspecting the title of an article using my browser's DevTools I can see that each article is being wrapped with an HTML link element which has &lt;code&gt;storylink&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WiggaGMF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1365/1%2ACB4LvugprTfW5QOV6iOXiQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WiggaGMF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1365/1%2ACB4LvugprTfW5QOV6iOXiQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The procedure we are going to follow can be denoted by the bellow list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Launch a browser instance in headless mode with Tor proxy&lt;/li&gt;
&lt;li&gt;Create a new page&lt;/li&gt;
&lt;li&gt;Navigate to &lt;a href="https://news.ycombinator.com/"&gt;https://news.ycombinator.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Get page HTML content&lt;/li&gt;
&lt;li&gt;Load the HTML content in Cheerio&lt;/li&gt;
&lt;li&gt;Create an array to save the article titles.&lt;/li&gt;
&lt;li&gt;Access all the elements that have &lt;code&gt;storylink&lt;/code&gt; class&lt;/li&gt;
&lt;li&gt;Get only the first 5 such elements, using Cherrio's &lt;a href="https://cheerio.js.org/#slice-start-end-"&gt;slice()&lt;/a&gt; method.&lt;/li&gt;
&lt;li&gt;Iterate through those 5 elements using Cherrio's &lt;a href="https://cheerio.js.org/#each-functionindex-element-"&gt;each()&lt;/a&gt; method.&lt;/li&gt;
&lt;li&gt;Append each article title in the array.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;And here is the output of the above code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8PJPsr9E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1366/1%2AK2U6x5KVJD8bxZyTH-89eA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8PJPsr9E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1366/1%2AK2U6x5KVJD8bxZyTH-89eA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuous scraping with different IP
&lt;/h2&gt;

&lt;p&gt;The last thing to consider is to take advantage of all the SOCKS ports we defined in torrc file. This is quite easy. We’ll define an array with each item to be a different port number. Then we’ll rename &lt;code&gt;main()&lt;/code&gt; to &lt;code&gt;scrape()&lt;/code&gt; and we will define a new &lt;code&gt;main()&lt;/code&gt; function that will call &lt;code&gt;scrape()&lt;/code&gt; each time with a different port.&lt;/p&gt;

&lt;p&gt;Here is the complete code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


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

&lt;p&gt;This article was intended to provide you with a gentle introduction to Web Scraping. Thank you for taking time to read it. 🙏&lt;/p&gt;

</description>
      <category>node</category>
      <category>tor</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
