<?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>Forem: Nathanael Demacon</title>
    <description>The latest articles on Forem by Nathanael Demacon (@quantumsheep).</description>
    <link>https://forem.com/quantumsheep</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%2F106751%2F5b8cfab3-99fe-4f16-9213-441a30d45cd6.jpg</url>
      <title>Forem: Nathanael Demacon</title>
      <link>https://forem.com/quantumsheep</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/quantumsheep"/>
    <language>en</language>
    <item>
      <title>How calculators read mathematical expression with operator precedence</title>
      <dc:creator>Nathanael Demacon</dc:creator>
      <pubDate>Tue, 28 May 2019 15:10:46 +0000</pubDate>
      <link>https://forem.com/quantumsheep/how-calculators-read-mathematical-expression-with-operator-precedence-4n9h</link>
      <guid>https://forem.com/quantumsheep/how-calculators-read-mathematical-expression-with-operator-precedence-4n9h</guid>
      <description>&lt;p&gt;In my journey of creating a programming language, I faced an issue: how to handle mathematical expressions with operator precedence properly without having to write a big ass algorithm? So after long long researches, I found the Reverse Polish Notation, a mathematical notation made exactly for that and used in all modern calculators!&lt;/p&gt;

&lt;p&gt;Reverse Polish Notation (or RPN) is a &lt;code&gt;Postfix&lt;/code&gt; notation (operators after operands) while our mosly used notation is an &lt;code&gt;Infix&lt;/code&gt; notation (operators between a left and right operands).&lt;/p&gt;

&lt;p&gt;But how to easily convert an Infix mathematical expression to a Postfix one? Well, the &lt;a href="https://en.wikipedia.org/wiki/Shunting-yard_algorithm" rel="noopener noreferrer"&gt;Shunting-yard algorithm&lt;/a&gt; is here for us! And thanks to Wikipedia, we have &lt;a href="https://en.wikipedia.org/wiki/Shunting-yard_algorithm#The_algorithm_in_detail" rel="noopener noreferrer"&gt;a pseudo-code representation of the algorithm&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Perfect, now that I knew where to start, there was another issue: how to read that thing? I will give you an example:&lt;/p&gt;

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

&lt;span class="nx"&gt;infix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&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;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;rpn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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;5&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Yeah I know - it looks like a big mess at first sight but I will tell you how to convert and read it by hand and you'll see, it's pretty easy.&lt;/p&gt;




&lt;h1&gt;
  
  
  Conversion
&lt;/h1&gt;

&lt;p&gt;To convert from infix to RPN, we'll need 2 stacks: one is the output, the other one is the operator stack. We'll symbolise everything in array-style:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&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="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;We have 2 rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;literals always goes to the output stack&lt;/li&gt;
&lt;li&gt;when an operator with lower or equal &lt;a href="https://en.wikipedia.org/wiki/Order_of_operations" rel="noopener noreferrer"&gt;precedence&lt;/a&gt; than the last element of the operators stack is comming, pop the last element of the operators stack to the output stack&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's convert it!
&lt;/h2&gt;

&lt;p&gt;First we have &lt;code&gt;1&lt;/code&gt;, a literal, like said in the rules above, it goes to the output stack:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&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="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="p"&gt;,&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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&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="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Then we encounter an operator so, in the operators stack it goes!&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&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="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Again, we face a literal so we put it in the output stack:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&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="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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&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="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After that, we have an operator that have the same precedence than the last operator in the operators stack which means that the actual &lt;code&gt;+&lt;/code&gt; will be poped into the output stack and we will push the &lt;code&gt;-&lt;/code&gt; to the operators stack:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&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="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Parentheses doesn't have precedence in Infix notation but we put it in the operators stack to separate the enclosed expression until we find a closing parenthese:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&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="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;(]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Again, &lt;code&gt;5&lt;/code&gt; is a literal:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&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="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="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="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="o"&gt;+&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="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;(]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;+&lt;/code&gt; is an operator, since the last operator in the operators stack is an opening parenthese, we don't care and just push it in the stack:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&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="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="o"&gt;+&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="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;(,&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;2&lt;/code&gt; is a literal:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="o"&gt;+&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;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;(,&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;*&lt;/code&gt; is an operator with higher precedence than the last operator in the stack (&lt;code&gt;+&lt;/code&gt;) so we just push it:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="o"&gt;+&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;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;(,&lt;/span&gt; &lt;span class="o"&gt;+&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;4&lt;/code&gt; is a literal, at this point I think that you must have understand that all literals goes in the output stack:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[)]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="o"&gt;+&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;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="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;(,&lt;/span&gt; &lt;span class="o"&gt;+&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then we find a closing parenthese which means that we'll pop every operators into the output stack until the opening parenthese (we completly delete the opening parenthese, we don't need it anymore):&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="o"&gt;+&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;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="o"&gt;*&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;operators&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Since we doen's have any more elements in our input stack, we pop all the operators into the output stack:&lt;/p&gt;

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

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="nx"&gt;output&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="o"&gt;+&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;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="o"&gt;*&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;operators&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;You did it! You completly converted an Infix notation into a RPN! &lt;br&gt;
Here's a graphical representation of what we did:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzijqiakqmi4lb6dzsz2.jpg" 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%2Fjzijqiakqmi4lb6dzsz2.jpg" alt="Shunting-yard algorithm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if you're not already bored by the length of this post, we'll see how to calculate it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Calculation
&lt;/h1&gt;

&lt;p&gt;We have our beautiful RPN: &lt;code&gt;[1, 2, +, 5, 2, 4, *, +, -]&lt;/code&gt; but now we want to calculate the mathematical expression to get its value. It's a lot simpler than it seems:&lt;/p&gt;

&lt;p&gt;We'll base on a single variable, doesn't need more than that:&lt;/p&gt;

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

&lt;span class="nx"&gt;rpn&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="o"&gt;+&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;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="o"&gt;*&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Then we need to find the first operator in the stack (starting to the first element which is &lt;code&gt;1&lt;/code&gt; here). Once we found it, we'll fetch the literal before and the one before the one before:&lt;/p&gt;

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

&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rpn&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rpn&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;operator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rpn&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In our case, &lt;code&gt;left&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt; is &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;operator&lt;/code&gt; is &lt;code&gt;+&lt;/code&gt;. Seems like we got all we need! Simply doing &lt;code&gt;left + right&lt;/code&gt; will give us the result (&lt;code&gt;3&lt;/code&gt;). Then we have our number and just put it in replacement for the left, right and operator:&lt;/p&gt;

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

&lt;span class="nx"&gt;rpn&lt;/span&gt; &lt;span class="o"&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;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="o"&gt;*&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;And we repeat. First operator is &lt;code&gt;*&lt;/code&gt;, left is &lt;code&gt;2&lt;/code&gt;, right is &lt;code&gt;4&lt;/code&gt;, calculate and replace (&lt;code&gt;2 * 4 = 8&lt;/code&gt;):&lt;/p&gt;

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

&lt;span class="nx"&gt;rpn&lt;/span&gt; &lt;span class="o"&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;8&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Again with the &lt;code&gt;+&lt;/code&gt;, left is &lt;code&gt;5&lt;/code&gt; and right is &lt;code&gt;8&lt;/code&gt;, &lt;code&gt;5 + 8 = 13&lt;/code&gt;:&lt;/p&gt;

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

&lt;span class="nx"&gt;rpn&lt;/span&gt; &lt;span class="o"&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;13&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next first operator is &lt;code&gt;-&lt;/code&gt;, right is &lt;code&gt;3&lt;/code&gt; and left is &lt;code&gt;13&lt;/code&gt;, &lt;code&gt;3 - 13 = -10&lt;/code&gt;:&lt;/p&gt;

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

&lt;span class="nx"&gt;rpn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Since we doesn't have any other operators, the last number in the stack is the result, otherwise you did it wrong at some point.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;rpn[0]&lt;/code&gt; is our result, which is the result of &lt;code&gt;1 + 2 - (5 + 2 * 4)&lt;/code&gt; as we wanted. Yay!&lt;/p&gt;




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

&lt;p&gt;Now that you learned a new mathematical notation, you can have fun with and build your own calculator or programming language that can properly resolve operator precedence.&lt;/p&gt;

&lt;p&gt;What other mathematical expression do you know and why do you use them?&lt;/p&gt;

&lt;p&gt;Twitter: &lt;a href="https://twitter.com/qtmsheep" rel="noopener noreferrer"&gt;@qtmsheep&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/quantumsheep" rel="noopener noreferrer"&gt;Nathanael Demacon&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Basics of Multithreading in C</title>
      <dc:creator>Nathanael Demacon</dc:creator>
      <pubDate>Wed, 09 Jan 2019 17:20:11 +0000</pubDate>
      <link>https://forem.com/quantumsheep/basics-of-multithreading-in-c-4pam</link>
      <guid>https://forem.com/quantumsheep/basics-of-multithreading-in-c-4pam</guid>
      <description>&lt;p&gt;C programs run on a single thread by default - meaning only one instruction is executed at a time. But what if you need to perform multiple tasks simultaneously? For example, a graphical interface must remain responsive even while performing time-consuming operations in the background. This is where &lt;strong&gt;multithreading&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Multithreading is different from asynchronous programming, which allows a single thread to handle multiple tasks without blocking.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧵 What Is a Thread?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;thread&lt;/strong&gt; is a single sequence of instructions within a process. Each process starts with one thread, but it can create additional threads that run concurrently. These threads share the same memory space, making communication between them efficient, but also introducing risks like race conditions.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔧 Creating a Thread in C
&lt;/h2&gt;

&lt;p&gt;Let’s dive into how to use threads in C.&lt;/p&gt;

&lt;p&gt;On POSIX systems (like Linux), the &lt;code&gt;pthread&lt;/code&gt; library provides thread functionality. To use it, you must link your program with &lt;code&gt;-lpthread&lt;/code&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc &lt;span class="nt"&gt;-o&lt;/span&gt; main main.c &lt;span class="nt"&gt;-lpthread&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s a simple program that creates and runs a thread:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;pthread.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;wait_fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;sleep&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;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Done.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pthread_t&lt;/span&gt; &lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pthread_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wait_fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&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;err&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="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"An error occurred: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&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="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Waiting for the thread to end...&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;pthread_join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread ended.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Expected Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Waiting for the thread to end...
Done.
Thread ended.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pthread_create&lt;/code&gt; starts a new thread and runs the &lt;code&gt;wait_fn&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pthread_join&lt;/code&gt; blocks the main thread until the new thread finishes.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;sleep()&lt;/code&gt; call pauses the thread for 2 seconds to simulate work.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pretty straightforward, right?&lt;/p&gt;




&lt;h2&gt;
  
  
  🔐 Using Mutexes to Prevent Race Conditions
&lt;/h2&gt;

&lt;p&gt;Multithreading introduces the risk of &lt;strong&gt;race conditions&lt;/strong&gt;, especially when multiple threads access shared resources (e.g., a file or a variable). To prevent this, we use a &lt;strong&gt;mutex&lt;/strong&gt; (short for &lt;em&gt;mutual exclusion&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Here’s an example that shows how to safely increment a shared variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;pthread.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;pthread_mutex_t&lt;/span&gt; &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;do_process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pthread_mutex_lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;int&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;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&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="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="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;sleep&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;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="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"...Done&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;pthread_mutex_unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;lock&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;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pthread_t&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t2&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;pthread_mutex_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&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="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mutex initialization failed.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&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="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;pthread_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;do_process&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;pthread_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;do_process&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;pthread_join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;pthread_join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;pthread_mutex_destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;11111...Done
22222...Done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The threads run &lt;strong&gt;sequentially&lt;/strong&gt; because the mutex ensures only one thread can access the shared section at a time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: Even if you switch the order of &lt;code&gt;pthread_join&lt;/code&gt;, the output will be the same because the threads themselves are synchronized by the mutex.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🚦 Semaphores: Flexible Synchronization
&lt;/h2&gt;

&lt;p&gt;Semaphores are another synchronization mechanism, similar to mutexes, but more flexible. Unlike mutexes, &lt;strong&gt;semaphores don’t have ownership&lt;/strong&gt; - any thread can lock or unlock them.&lt;/p&gt;

&lt;p&gt;This makes semaphores suitable for managing access to a limited set of resources (like a pool of connections or permits).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We won't dive into full semaphore examples here, but if you're interested, check out the &lt;code&gt;semaphore.h&lt;/code&gt; library and functions like &lt;code&gt;sem_init&lt;/code&gt;, &lt;code&gt;sem_wait&lt;/code&gt;, and &lt;code&gt;sem_post&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  ✅ Wrapping Up
&lt;/h2&gt;

&lt;p&gt;As you’ve seen, multithreading in C is powerful yet surprisingly approachable. With just a few function calls, you can start writing concurrent programs - but always keep safety in mind with tools like mutexes and semaphores.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>c</category>
      <category>threading</category>
      <category>multithreading</category>
    </item>
    <item>
      <title>How did I started learning a new technology</title>
      <dc:creator>Nathanael Demacon</dc:creator>
      <pubDate>Sun, 02 Dec 2018 16:39:28 +0000</pubDate>
      <link>https://forem.com/quantumsheep/how-i-started-learning-a-new-technology-10aa</link>
      <guid>https://forem.com/quantumsheep/how-i-started-learning-a-new-technology-10aa</guid>
      <description>&lt;p&gt;There are a lot of technologies, out there and for every needs: programming languages, softwares, operating systems, etc... But what if you wanted to learn a new technology? Because of any reason? In this post, we will see how I learned a new technology to give you an idea of how to start.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why
&lt;/h1&gt;

&lt;p&gt;It's a fact: some technologies are better for doing things that other ones can't do better and vice versa. While some technologies can be the same, there are differences that can lead to change to another one.&lt;/p&gt;

&lt;p&gt;Programming languages for example are a matter of syntax, performance and community while softwares are used for their usefulness or design; and operating systems are for what they can do (Windows has Active directory, Linux has full customisation, etc...).&lt;/p&gt;

&lt;p&gt;Jobs offers can also be a great reason of why to learn a technology.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsiza41fo99m37r3h8p98.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsiza41fo99m37r3h8p98.gif" width="500" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, what makes me learn a technology is mostly the hype. I know that most peoples thinks that following the trend isn't a great thing but who know? Go was just a trend and becomes one of the most used programming languages and the same thing applied for the BlockChain. It's just a matter of wanting to learn this technology because it's popular, if it's popular it's certainly a good one.&lt;/p&gt;

&lt;h1&gt;
  
  
  How
&lt;/h1&gt;

&lt;p&gt;The tech's documentation will become your life, tutorials and forums too. There is not really a good way to learn something, it depends on your determination and how you learn better.&lt;/p&gt;

&lt;p&gt;Books are the first choice for most peoples. Personnally, doing little projects for fun is how I learn a technology but it keeps me away from the theorical side.&lt;/p&gt;

&lt;h1&gt;
  
  
  What did I learned?
&lt;/h1&gt;

&lt;p&gt;The technology I chosen to learn was the &lt;a href="https://crystal-lang.org/" rel="noopener noreferrer"&gt;Crystal programming language&lt;/a&gt;. Why? Because it's a trend and have the Ruby's syntax. Also, I recently started to learn how to use Arch Linux, totally for fun.&lt;/p&gt;

&lt;p&gt;The fact is that I don't want to stay in one technology, which is boring in the long term. I want to discover things everyday because of my curiosity.&lt;/p&gt;

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

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

&lt;p&gt;Maybe that after this post you will start learning a new technology, for any good reason. Or maybe that you will create something that other people will learn because it's a fantastic idea.&lt;/p&gt;

&lt;p&gt;Discovering new things and making the world evolve is something that moves us and needs to continue like that.&lt;/p&gt;

&lt;p&gt;And you: how and why do you started learning something?&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How to properly use passwords</title>
      <dc:creator>Nathanael Demacon</dc:creator>
      <pubDate>Mon, 12 Nov 2018 00:05:25 +0000</pubDate>
      <link>https://forem.com/quantumsheep/how-to-use-passwords-properly-7j0</link>
      <guid>https://forem.com/quantumsheep/how-to-use-passwords-properly-7j0</guid>
      <description>&lt;p&gt;There are different kind of passwords uses: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the "my memory isn't that good" (also call the "I don't care anymore" method) with passwords like &lt;code&gt;qwerty&lt;/code&gt; or &lt;code&gt;123456&lt;/code&gt; or literally &lt;code&gt;password&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the one that uses social information, ex: &lt;code&gt;199105Bob&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the good ones&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's be real, the first two aren't secure at all, not even a little. But what about "the good ones"? Well, they are what we are talking about.&lt;/p&gt;

&lt;p&gt;Maybe sometimes you wonder if your password is safe enough, and you hope that your account will not be taken by someone else by any way. First, we need to take a look at what attacks can be done to find your password.&lt;/p&gt;

&lt;h1&gt;
  
  
  Brute-force attack
&lt;/h1&gt;

&lt;p&gt;This method is used to try every possibility in a way find your password. It's not used a lot because it requires massive amounts of time and power to be achieved, depending on the target's password length.&lt;/p&gt;

&lt;p&gt;The longer a password is, the longer it will take to try every possibility.&lt;/p&gt;

&lt;h1&gt;
  
  
  Dictionary attack
&lt;/h1&gt;

&lt;p&gt;While brute-force attack tries every possible characters, security researchers found a way to escape the useless trials and focus on commonly used passwords. &lt;/p&gt;

&lt;p&gt;A dictionary attack use pre-defined passwords list instead of random characters. Rules can even be added to match the target's information and remove passwords that may not work, like its birthdate, its name, etc... That's social engineering.&lt;/p&gt;

&lt;p&gt;To prevent this method, don't use obvious personnal information in your password and &lt;a href="https://haveibeenpwned.com/passwords" rel="noopener noreferrer"&gt;check if it's not already in a database&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Your web browser's saved passwords
&lt;/h1&gt;

&lt;p&gt;Maybe that you use the "Remember my password" option while using the internet. While this option can be very comfortable to use, it's not secure at all.&lt;/p&gt;

&lt;p&gt;Web browsers cipher your passwords, but they also decipher them to fill password inputs, to make this happen, they use a stored key. So why can't we decipher them too? Well, we can, tools like &lt;a href="https://github.com/AlessandroZ/LaZagne" rel="noopener noreferrer"&gt;LaZagne&lt;/a&gt; are the proof, that's why you should never use this option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxwwfqutxq8xmsn2hhrw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxwwfqutxq8xmsn2hhrw.png" alt="Brute force time/password length" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Solutions
&lt;/h1&gt;

&lt;p&gt;Now that I told you how your password can be hacked, I must tell you how to create strong passwords that are not a plague to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Password managers
&lt;/h2&gt;

&lt;p&gt;Those are softwares that helps you to manage your passwords. The best use of them is to export your password with a file (that can be transferred in a secure way using &lt;a href="https://dev.to/quantumsheep/protect-your-data-with-warshield-3n6i"&gt;WarShield&lt;/a&gt; or something else).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.dashlane.com/en" rel="noopener noreferrer"&gt;Dashlane&lt;/a&gt; - Very popular and self-hosted &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.vaultproject.io/" rel="noopener noreferrer"&gt;Vault&lt;/a&gt; - Open-source one&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find a lot of password managers on the internet, maybe you will find the one who match your needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Easy to remember strong passwords
&lt;/h2&gt;

&lt;p&gt;A dream for your accounts, a nightmare for hackers, the sure thing is that you can remember sentences. What about using our brain's capacity in order to create passwords?&lt;/p&gt;

&lt;p&gt;This method doesn't need a random sentence, just try to make logic. I will create John Lemmon, a man that live in London at 41 Abbey Street, his password will be &lt;code&gt;MniJL@Il41ab&amp;amp;&lt;/code&gt;. I see you, behind your screen, with a face saying "wtf is that?", it can be really hard the first time you use a password like this but it's really easy to remember.&lt;/p&gt;

&lt;p&gt;The sentence was &lt;code&gt;My name is John Lemmon and I live at 41 Abbey Street&lt;/code&gt;, the ampersand (&lt;code&gt;&amp;amp;&lt;/code&gt;) is a random character with no logic behind it.&lt;/p&gt;

&lt;p&gt;Like you see, it's pretty easy to create a very strong password without breaking your mind every time you need to remember it.&lt;/p&gt;

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

&lt;p&gt;Today, there is no excuse for not having a secure password.&lt;/p&gt;

&lt;p&gt;I think that there should be computer security awareness in schools to teach children to protect their data in the digital world. In the next years, it will evolve, more and more peoples are learning security and the security war will never end.&lt;/p&gt;

&lt;p&gt;Stay safe with your data.&lt;/p&gt;

</description>
      <category>security</category>
      <category>meta</category>
    </item>
    <item>
      <title>What I learned about cryptography in 3 weeks</title>
      <dc:creator>Nathanael Demacon</dc:creator>
      <pubDate>Sun, 04 Nov 2018 23:31:52 +0000</pubDate>
      <link>https://forem.com/quantumsheep/what-i-learned-about-cryptography-in-3-weeks-57ok</link>
      <guid>https://forem.com/quantumsheep/what-i-learned-about-cryptography-in-3-weeks-57ok</guid>
      <description>&lt;p&gt;It's been 3 weeks that I started developing &lt;a href="https://dev.to/quantumsheep/protect-your-data-with-warshield-3n6i"&gt;WarShield&lt;/a&gt;, a file encryption CLI. So I wanted to tell you everything important I know today about cryptography.&lt;/p&gt;




&lt;h1&gt;
  
  
  Keys (passwords) in cryptography
&lt;/h1&gt;

&lt;p&gt;In case you want to encrypt your data, it's good to be able to decrypt it too: that's why you use a key, more commonly named as password.&lt;/p&gt;

&lt;p&gt;Keys in cryptography are grouped in two categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Symmetric-key cryptography&lt;/li&gt;
&lt;li&gt;Public-key cryptography, also called as asymmetric-key cryptography&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Symmetric-key cryptography algorithms uses only one key to encrypt and decrypt a value, unlike public-key cryptography algorithms that uses a "public key" to encrypt and a "private key" to decrypt.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F148l3ksy4k610qvifgbj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F148l3ksy4k610qvifgbj.png" alt="Encryption types (from https://ssl2buy.com)" width="" height=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;sup&gt;&lt;sup&gt;Image credits: &lt;a href="https://www.ssl2buy.com/wiki/symmetric-vs-asymmetric-encryption-what-are-differences" rel="noopener noreferrer"&gt;SSL2Buy&lt;/a&gt;&lt;/sup&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Those keys must be exactly the same length as required by the encryption algorithm. For instance, an algorithm like AES-256-GCM require a 128 bit key (32 characters), GCM being the "mode of operation" that define how the algorithm works to provide additional informations like authenticity.&lt;/p&gt;

&lt;p&gt;But don't worry, you don't need to have 32 characters passwords, event though it would be secure. You can (and must) hash your keys to provide a secure and well sized key.&lt;/p&gt;




&lt;h1&gt;
  
  
  Random and authenticity
&lt;/h1&gt;

&lt;p&gt;If you encrypt multiple values with the same key, the outputs would leak some informations about your key. Which you don't want for security reasons. A great way to secure your encrypted values is to make them fully random. And here you have: the Initialization Vector.&lt;/p&gt;

&lt;h2&gt;
  
  
  Initialization Vector (IV)
&lt;/h2&gt;

&lt;p&gt;This technique is basically a way to mix your key with another value, best being a random value.&lt;/p&gt;

&lt;p&gt;It's size depends on the defined encrypting algorithm mode of operation and must be given manually. It's not generated by the algorithm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Authentication and integrity
&lt;/h2&gt;

&lt;p&gt;Maybe you heard about MD5 algorithm to check if your downloaded file contain all the wanted data? Well, it's called integrity. It ensure that your data is exactly the one you want.&lt;/p&gt;

&lt;p&gt;Authentication involve integrity and make sure that your data has been encrypted by a specific entity (for instance, a computer). It's a way to ensure the non-repudiation of data, in clear, it's mean to be sure that the data isn't falsified. An authentification value is called a Tag or a MAC (Message Authentication Code). &lt;/p&gt;

&lt;p&gt;The most used integrity algorithms are SHA1 and MD5. For authentication algorithms it's HMAC, checksum and CMAC. There is bunch of others algorithms but here's the main ones.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff5idsknnom3bhyjoewt1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff5idsknnom3bhyjoewt1.png" alt="MAC" width="661" height="409"&gt;&lt;/a&gt;&lt;br&gt;
&lt;sup&gt;&lt;sup&gt;Image credits: &lt;a href="https://en.wikipedia.org/wiki/Message_authentication_code" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;&lt;/sup&gt;&lt;/sup&gt;&lt;/p&gt;




&lt;p&gt;And... That's it! There is nothing more to say about the basics of cryptography. Sure I could talk about different encryption algorithms but I find it useless in this case.&lt;/p&gt;

&lt;p&gt;I will learn more and more about cryptography, improve my projects and maybe my own security. Hope that you learned too about cryptography 😄&lt;/p&gt;

</description>
      <category>security</category>
      <category>cryptography</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Why you should reinvent the wheel</title>
      <dc:creator>Nathanael Demacon</dc:creator>
      <pubDate>Sat, 27 Oct 2018 23:27:23 +0000</pubDate>
      <link>https://forem.com/quantumsheep/why-you-should-reinvent-the-wheel-4ha2</link>
      <guid>https://forem.com/quantumsheep/why-you-should-reinvent-the-wheel-4ha2</guid>
      <description>&lt;p&gt;Sometimes some people tell to not "reinvent the wheel" by creating things that already exists. It's been time to clarify why we should reinvent the wheel and why they are wrong!&lt;/p&gt;

&lt;h1&gt;
  
  
  Learning
&lt;/h1&gt;

&lt;p&gt;The best argument you can say in this situation is that you want to learn. Revolutionary ideas don't come that easy, if you want to learn just do something you would like to do, don't focus on creating new things especially.&lt;/p&gt;

&lt;p&gt;You need skills to create your ideas. Get those skills by doing anything that comes in your mind!&lt;/p&gt;

&lt;h1&gt;
  
  
  Understanding how something works
&lt;/h1&gt;

&lt;p&gt;Doing something that already exists can make yourself understand how it works and how can it be improved. Things are not perfects but are perfectibles, let's see the smartphone industry for instance.&lt;/p&gt;

&lt;h1&gt;
  
  
  You want to do it
&lt;/h1&gt;

&lt;p&gt;You reinvent the wheel? So what? Peoples should not lead you in unwanted situations. Just have fun making things. Doesn't matter if those already exists, at least you'll be happy and that's what matters the most.&lt;/p&gt;

&lt;h1&gt;
  
  
  The "wheel" has been improved
&lt;/h1&gt;

&lt;p&gt;The metaphor "don't reinvent the wheel" is so wrong! The wheel, strictly speaking, has been reinvented tons of times. We didn't come with a full rubber wheel in high technology cars in the first try. Things evolves because peoples "reinvent the wheel", and it should continue like that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0via5p7zddhmscalzfvw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0via5p7zddhmscalzfvw.jpg" alt="Reinventing the wheel" width="800" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, just improve yourself without thinking about making a brand new idea. It's ain't easy to find something that haven't been created yet so don't think too much about it if your only goal is to discover and explore new things.&lt;/p&gt;

&lt;p&gt;Ideas comes with discovery. &lt;br&gt;
Discoveries comes with time.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>career</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Protect your data with WarShield</title>
      <dc:creator>Nathanael Demacon</dc:creator>
      <pubDate>Sat, 20 Oct 2018 22:57:01 +0000</pubDate>
      <link>https://forem.com/quantumsheep/protect-your-data-with-warshield-3n6i</link>
      <guid>https://forem.com/quantumsheep/protect-your-data-with-warshield-3n6i</guid>
      <description>&lt;p&gt;Security has been evolving with information technology, peoples find new ways to protect your data and privacy because we need to feel secure.&lt;br&gt;
Sometimes you want to go deep into security and you start to put passwords everywhere. This is why WarShield was created.&lt;/p&gt;
&lt;h1&gt;
  
  
  What is WarShield?
&lt;/h1&gt;

&lt;p&gt;WarShield is a CLI tool made to cipher and decipher your files with a password using AES-256. It was made to ensure a full files protection inside a USB flash drive or an external hard drive. If those are lost, malicious peoples can take over your files and their data.&lt;/p&gt;

&lt;p&gt;This is when WarShield comes with his fast and reliable solution.&lt;/p&gt;
&lt;h1&gt;
  
  
  How to use WarShield?
&lt;/h1&gt;

&lt;p&gt;WarShield can be installed with NPM:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g warshield
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After the (very) short installation, you can use the &lt;code&gt;warshield&lt;/code&gt; CLI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;warshield (encrypt|decrypt) (file)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first argument is &lt;code&gt;encrypt&lt;/code&gt; or &lt;code&gt;decrypt&lt;/code&gt;, describing the wanted action. Don't worry if you try to decrypt something that isn't encrypted, it will not do it.&lt;/p&gt;

&lt;p&gt;Next is the file (or folder) you want to encrypt/decrypt. If it's a folder, every files in it will be processed recursively.&lt;/p&gt;

&lt;p&gt;Launching that command will make the program asking you the password you want or need to use to encrypt or decrypt the file.&lt;/p&gt;

&lt;p&gt;When you launch the encryption/decryption, WarShield will scan all the wanted files first. If the selected file a folder, it can take a while depending on the files weight.&lt;/p&gt;

&lt;p&gt;WarShield on NPM: &lt;a href="https://www.npmjs.com/package/warshield/"&gt;https://www.npmjs.com/package/warshield/&lt;/a&gt;&lt;br&gt;
Github repository: &lt;a href="https://github.com/QuantumSheep/warshield/"&gt;https://github.com/QuantumSheep/warshield/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks to 💖 this article if you enjoyed it :)&lt;/p&gt;

</description>
      <category>security</category>
      <category>ciphering</category>
      <category>aes</category>
    </item>
    <item>
      <title>All you need to know about destructuring in JavaScript</title>
      <dc:creator>Nathanael Demacon</dc:creator>
      <pubDate>Mon, 08 Oct 2018 13:30:43 +0000</pubDate>
      <link>https://forem.com/quantumsheep/all-you-need-to-know-about-destructuring-in-javascript-1hla</link>
      <guid>https://forem.com/quantumsheep/all-you-need-to-know-about-destructuring-in-javascript-1hla</guid>
      <description>&lt;p&gt;There is one fact that nobody can deny- everything , once built, can be destroyed. This concept also applies to programming. In this article, I will be discussing destructuring in JavaScript.&lt;/p&gt;

&lt;p&gt;In JavaScript, destructuring is when you decompose the properties of an object or the indexes of an array to separate them to create specific variables. This does not mean that these separated objects or arrays can never be used again in the program.&lt;/p&gt;

&lt;p&gt;Before we begin, however, I will quickly outline some important differences between arrays and objects. Arrays have number indexes and objects have string indexes. In addition, arrays and objects use different syntax.&lt;/p&gt;

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

&lt;p&gt;In JavaScript, we can put objects in arrays and arrays in objects too, the syntax let us put everything where we want.&lt;/p&gt;




&lt;p&gt;Now let's talk about the main subject, the destructuration. Destructing an object or an array doesn't mean that you will erase it from the program and can't ever use it again, it means that you'll get a specific part of it. Let's use examples, with Axios, a famous library. We can do an HTTP request (like with Fetch API or XmlHttpRequest) which returns us the following object's schema:&lt;/p&gt;

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

&lt;p&gt;We'll stick to the data property! It's an object that contains all the server's response data. Let's say that we want to get all our users from our NodeJS API with MongoDB database, we may do something similar to the following code.&lt;/p&gt;

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

&lt;p&gt;In this case, the &lt;code&gt;req&lt;/code&gt; constant will be an object with the same schema as seen before. So to get the users data, we has the &lt;code&gt;req.data&lt;/code&gt; containing our array of users.&lt;/p&gt;

&lt;p&gt;Now that we saw how to perform an Axios request, let's say that we only want one user that we can get from the route &lt;code&gt;/api/users/:userid&lt;/code&gt;. For instance if the wanted user's id is &lt;code&gt;7&lt;/code&gt;, we do a request to &lt;code&gt;/api/users/7&lt;/code&gt;. But what if the API returns an array? Then we can do &lt;code&gt;req.data[0]&lt;/code&gt; which can be a good way to do it but not as practical as if we used destructors…&lt;/p&gt;

&lt;p&gt;First, let's get the &lt;code&gt;data&lt;/code&gt; object. We can do &lt;code&gt;req.data&lt;/code&gt; but we don't care about others req's properties so let's only get data. What we will do is an object destructuring assignment. (Finally, some excitment)&lt;/p&gt;

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

&lt;p&gt;Yes, we only got the &lt;code&gt;data&lt;/code&gt; property and it created an object named &lt;code&gt;data&lt;/code&gt;! And you've done a "destructuring assignment" in JavaScript, great!&lt;/p&gt;

&lt;p&gt;For instance we can destruct multiple properties in the same destructuring assignment, Axios provide a &lt;code&gt;status&lt;/code&gt; property so let's get it by destructuration!&lt;/p&gt;

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

&lt;p&gt;We can also give default values to any destructed property like bellow.&lt;/p&gt;

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

&lt;p&gt;Default value to a destructed propertyBut our object's name isn't what we really wanted, we want a &lt;code&gt;users&lt;/code&gt; object to become easier to understand. So let's do a name's assignation without creating any more new variables.&lt;/p&gt;

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

&lt;p&gt;It's cool right? You can assign destructed properties to new variable's name in the same line and it doesn't look bad at all! So now we have our well named &lt;code&gt;users&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Still, &lt;code&gt;users&lt;/code&gt; keep being an array, what could we do with it? I present you, the array's destructing assignment:&lt;/p&gt;

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

&lt;p&gt;Destructing assignment with an arrayIn this case, &lt;code&gt;a&lt;/code&gt; is a new constant which will receive the index &lt;code&gt;0&lt;/code&gt; of the array &lt;code&gt;[1, 2, 3]&lt;/code&gt; (Which has the value &lt;code&gt;1&lt;/code&gt;). The constant declaration's position in the destructing assignment define the selected index that will have it's value taken.&lt;/p&gt;

&lt;p&gt;In this case, a is a new constant which will receive the index &lt;code&gt;0&lt;/code&gt; of the array &lt;code&gt;[1, 2, 3]&lt;/code&gt; (Which has the value &lt;code&gt;1&lt;/code&gt;). The constant declaration's position in the destructing assignment define the selected index that will have its value taken.&lt;/p&gt;

&lt;p&gt;Like for objects, we can have default values and multiple declarations. Their position always match the selected array's indexes.&lt;/p&gt;

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

&lt;p&gt;We can also bypass any indexes we want by putting a &lt;code&gt;,&lt;/code&gt; without any variable declaration. In the following case we will bypass the indexes &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; (2 comas). &lt;code&gt;c&lt;/code&gt; will be equal to the third index which have a value of &lt;code&gt;6&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;Bypassing indexes in an array's destructing assignmentNow that we know how to use array's and object's destructing assignment, we finally could resolve our case of the variable &lt;code&gt;users&lt;/code&gt; being an array with only one index.&lt;/p&gt;

&lt;p&gt;We can use destructing assignments inside one another and it will works. So put an array's destructuring assignment in an object's destructuring assignment and you'll get the same result on the selected property:&lt;/p&gt;

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

&lt;p&gt;To sum up, we've destructed the property &lt;code&gt;data&lt;/code&gt; of the &lt;code&gt;axios.get('/api/users/7')&lt;/code&gt; instruction's. Next we assigned data to a proper variable's name, &lt;code&gt;users&lt;/code&gt;. After that we used the array's destructing assignment to define the variable &lt;code&gt;users&lt;/code&gt; as the first index of the &lt;code&gt;data&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;Every destructuring assignment can be used recursivly like seen previously, in any order you want. Object's destructuring assignment can be used in others object's destructuring assignment and same goes for array's destructuring assignment.&lt;/p&gt;




&lt;p&gt;Now we perfectly know how to destructs objects and arrays in JavaScript. But there's another trick you can do with destructive assignments, the " rest pattern" or "spreading". This give you the possibility to take the remaining undestructed properties/indexes to a new variable. Here's a demonstration with arrays but you can do the same with objects :&lt;/p&gt;

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




&lt;p&gt;Thanks you for reading, hope that now you know everything about destructuring in JavaScript!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>syntax</category>
      <category>tips</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
