<?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: Pan Seba</title>
    <description>The latest articles on Forem by Pan Seba (@catchmareck).</description>
    <link>https://forem.com/catchmareck</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%2F629068%2F853e9da2-5c66-4f94-9f16-b6b5d83213a6.png</url>
      <title>Forem: Pan Seba</title>
      <link>https://forem.com/catchmareck</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/catchmareck"/>
    <language>en</language>
    <item>
      <title>Stop abusing .map()!</title>
      <dc:creator>Pan Seba</dc:creator>
      <pubDate>Sun, 26 Sep 2021 19:41:41 +0000</pubDate>
      <link>https://forem.com/catchmareck/stop-abusing-map-51mj</link>
      <guid>https://forem.com/catchmareck/stop-abusing-map-51mj</guid>
      <description>&lt;p&gt;Every once in a while when I do code review or visit StackOverflow I stumble upon code snippets that look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruitIds&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;apple&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;oragne&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;banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;fruitIds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`fruit-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So as you can see it's just a simple iteration where for every element in the &lt;code&gt;fruitIds&lt;/code&gt; array we add &lt;code&gt;active&lt;/code&gt; class to a certain HTML element in a DOM.&lt;/p&gt;

&lt;p&gt;Many programmers (especially new) wouldn't notice anything wrong with the code above. &lt;strong&gt;However&lt;/strong&gt;, there is a one major issue here - the usage of &lt;code&gt;.map()&lt;/code&gt;. Let me explain.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is wrong with &lt;code&gt;.map()&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Well, there is completely nothing wrong with this particular array method. In fact, I think it is very handy and beautifully wraps one of the iteration patterns - &lt;em&gt;mapping&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In simple words, mapping is an operation which applies a function to every element of a collection and returns a new collection with elements changed by the mentioned function. For example, if we have an array of numbers &lt;code&gt;const nums = [1, 2, 3, 4];&lt;/code&gt; and would like to receive a new array of doubled numbers, we could &lt;em&gt;map&lt;/em&gt; the original array to a new one like this (in JavaScript):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;biggerNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&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="c1"&gt;// &amp;gt;&amp;gt; [2, 4, 6, 8];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;biggerNums&lt;/code&gt; array would consist of numbers from the original &lt;code&gt;nums&lt;/code&gt; array multiplied by &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Notice how &lt;code&gt;.map()&lt;/code&gt; is used - we assigned the result of this method to a new variable called &lt;code&gt;biggerNums&lt;/code&gt;. I have also mentioned earlier that mapping is an operation that &lt;strong&gt;returns a new collection&lt;/strong&gt; of elements. And this is the very reason the code snippet showed at the beginning of this article is wrong. The &lt;code&gt;.map()&lt;/code&gt; returns a new array - &lt;strong&gt;always&lt;/strong&gt; - and if we don't need that array, we shouldn't use &lt;code&gt;.map()&lt;/code&gt; in the first place. In this particular case (simple iteration) a different array method should be used - &lt;code&gt;.forEach()&lt;/code&gt; - which is specifically designed for such cases. It doesn't return a new collection, it simply walks through an array and invokes a callback function for every element allowing you to do something for each of them.&lt;/p&gt;

&lt;p&gt;So the correct version of the mentioned snippet should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// good way&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruitIds&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;apple&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;oragne&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;banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;fruitIds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`fruit-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We don't need a new array so we simply iterate over the &lt;code&gt;fruitIds&lt;/code&gt; array and add the &lt;code&gt;active&lt;/code&gt; class to an HTML element for each of the array items.&lt;/p&gt;

&lt;p&gt;Okay, but why should I care? &lt;code&gt;.map()&lt;/code&gt; is shorter and easier to write than &lt;code&gt;.forEach()&lt;/code&gt;. What could possibly go wrong?&lt;/p&gt;

&lt;h2&gt;
  
  
  Consequences of abusing &lt;code&gt;.map()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;One of the worst consequences of abusing &lt;code&gt;.map()&lt;/code&gt; is the fact that it returns a new redundant array. To be more specific - it returns a new array of the same size as the one this method was called on. It means that if we have an array of 1000 elements, &lt;code&gt;.map()&lt;/code&gt; will return a new array of 1000 elements - &lt;strong&gt;every time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In JavaScript, all functions return a value. Even if we don't use the &lt;code&gt;return&lt;/code&gt; keyword, the function will return &lt;code&gt;undefined&lt;/code&gt; implicitly. That's how the language has been designed. This rule also applies to callbacks - they are functions too.&lt;/p&gt;

&lt;p&gt;Having said that, let's get back to the original example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// wrong way&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruitIds&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;apple&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;oragne&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;banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;fruitIds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`fruit-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens here? An array of fruit IDs is created and then it's &lt;strong&gt;mapped&lt;/strong&gt; to another array of the same size. Even though the array returned by &lt;code&gt;.map()&lt;/code&gt; is not used, it does take place in memory. This new (unused) array looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's because the callback passed to the &lt;code&gt;.map()&lt;/code&gt; method does not have the &lt;code&gt;return&lt;/code&gt; keyword and as we know, if there is no &lt;code&gt;return&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt; is returned implicitly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How bad is it? Very bad.&lt;/strong&gt; In this particular example it won't bring any serious consequences - there are only three items in the array so creating another three-element array won't cause any problems. However, the problem arises when we deal with big arrays of complex data. If we want to iterate over an array of five thousand objects and we abuse &lt;code&gt;.map()&lt;/code&gt;, we create another array of five thousand elements - &lt;code&gt;undefined&lt;/code&gt;s. So we end up storing 10 000 elements in memory from which a whole half is redundant. It is a very non-optimal practice and in some scenarios it may even lead to the application overload. This is why we should pick right methods to the right tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;There are many practices that are essentialy bad, but the negative consequences will start to be visible only when dealing with bigger datasets. One of such practices is abuse of &lt;code&gt;.map()&lt;/code&gt;. When operating on small arrays, it won't cause any hurt. But when we make this mistake with a bigger array it will start overloading our application and it may be quite hard to debug.&lt;/p&gt;

&lt;p&gt;This is why we should never let it pass by and whenever we see this abuse, we should take care of it. I hope now you understand why.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>functional</category>
      <category>codereview</category>
    </item>
    <item>
      <title>Do you remember Bosque from Microsoft? I wrote a book about it</title>
      <dc:creator>Pan Seba</dc:creator>
      <pubDate>Thu, 16 Sep 2021 19:28:47 +0000</pubDate>
      <link>https://forem.com/catchmareck/dou-you-remember-bosque-from-microsoft-i-wrote-a-book-about-it-53gj</link>
      <guid>https://forem.com/catchmareck/dou-you-remember-bosque-from-microsoft-i-wrote-a-book-about-it-53gj</guid>
      <description>&lt;p&gt;Let's start with a quick explanation what is Bosque, because I believe it's not so well-known topic.&lt;/p&gt;

&lt;p&gt;Bosque is a programming language created by Microsoft in 2019. Initially, it was an experiment that was supposed to show what would happen if we removed the sources of accidental complexity from the language. Currently, Bosque is on its road to the first stable version. This project's mission is to start a new paradigm called Regularized Programming.&lt;/p&gt;

&lt;p&gt;At this point you may be wondering why on Earth would I write a book about a language which is hardly recognizable? Let me explain how it happend.&lt;/p&gt;

&lt;h2&gt;
  
  
  How did I become an author?
&lt;/h2&gt;

&lt;p&gt;It was a bit unexpected. Back in 2019, when Bosque had been initially released to the public, I was curious what this new language looks like and what it can be used for. At the time, I was also very interested in the internal design of various programming languages and was simply curious how to implement a new one from scratch. So, I downloaded the Bosque codebase and started reading it and playing with the language itself. There was a ton of bugs really. But I couldn’t leave that as-is and started fixing them. After my first PR got merged, I was motivated to open more. I was really enjoying it – I was both helping to develop Bosque and learning about how programming languages are designed and implemented. I was thinking that regardless of whether Bosque will be a success or not, it’s still worth doing it – you never know what the future brings, right? And then, a year later, I got an email in my inbox with a proposition to author a book about Bosque. I couldn’t say no and without hesitation agreed. That’s how I become an author and this is the cover of my first ever technical book:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q7u6UCju--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gnpr33ku6junkjcn1iy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q7u6UCju--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gnpr33ku6junkjcn1iy.jpeg" alt="Cover"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's get back to Bosque itself. I think it's a good idea to make a little introduction to the language since I decided to write a book about it, right? In the next few sections I describe in more detail what is Bosque and regularized programming as well as cite a few fragments from my book. Let's jump in!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is regularized programming?
&lt;/h2&gt;

&lt;p&gt;Let's make a step back to the term "&lt;em&gt;sources of accidental complexity&lt;/em&gt;". It covers things like loops, mutable state, unexpected behavior or invariants. All of that lead to the whole variety of problems, often quite easy to solve but hard to find. You can imagine a built-in method called &lt;code&gt;.sort()&lt;/code&gt;. As a programmer you need to think whether the sorting is stable or not. Similarly iterating over a map - you can't be sure about keys order since there's no such concept as "order" in maps. Depending on a language the solutions will differ. Languages following regularized programming paradigm are supposed to work in very clear and predictable way.&lt;/p&gt;

&lt;p&gt;Another example refers to the mentioned invariants. Let's say you create a game of chess. These are your invariants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a piece (or a pawn) can take only one square&lt;/li&gt;
&lt;li&gt;a piece (or a pawn) disappears from the board when it gets taken&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the rules that cannot be broken. So we need to create a data structure representing a chessboard. When one of the players make a move we need to do two things: take a piece from one square (1) and put it on another (2). For a very short moment one of our invariants is broken. If we take the piece from a square first and then put it on another one, we break the second invariant because for a brief moment a piece that hasn't been taken is not on the board. If we put the piece on another square first and then take it from its current square, we break the first invariant because for a moment a piece stands on two different squares. A "regulation" here is a mechanism that allows you to update class fields in atomic way so that there's no moment in time when your invariants are broken.&lt;/p&gt;

&lt;p&gt;This is what regularized programming has been initiated for. It's a paradigm that gives us tools that take a ton of problems from us. There are more examples really and it's hard to show them all. Using Bosque built-in tools you can create quite complex programs although the language is still under quite a heavy development.&lt;/p&gt;

&lt;p&gt;Let's see a few examples from my book to better understand advantages of this language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Typed Strings
&lt;/h2&gt;

&lt;p&gt;It's a feature that allows you to create text values of a specific type. Have a look at the fragment from &lt;em&gt;Chapter 3&lt;/em&gt;, &lt;em&gt;Bosque Key Features&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[...] On the other hand, there are lots of less complex data types that are still schematic, and some checks are required to confirm their validity. Some examples include phone number, ZIP code, email address, credit card number, or even simple types such as mass or distance. All these data types can be described with a finite number of rules that define their structure. The problem is that we usually do not create a class for things such as a mass unit and even if we do, we feel like it's overkill because the implementation hides the true intent of a programmer, which is to simply know what type of data we are dealing with. A solution we usually go for is strings.&lt;/p&gt;

&lt;p&gt;If we want to store a phone number or a distance unit, we simply store it as a string. This is convenient, but at the same time, it brings terrible consequences. If we operate on strings, then how sure can we be that a given string is actually a phone number? What stops us from – accidentally – passing a string representing a mass unit to a function that expects a ZIP code string? The answer is that we can't be sure at all unless we perform a manual check; nothing stops us unless we perform a manual check. It would be much easier for us if there was a way to tell the compiler to treat some strings as if they were representing some data type – &lt;strong&gt;typed strings&lt;/strong&gt;. This is where Bosque shines.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we can define a type of value that is stored in a string. Thanks to that it's impossible to accidentaly pass the wrong value to your variable/parameter. For example if you want to store a phone number you can create a typed string and use it in your code so that there's no doubt what you are looking at:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let number = PhoneNumber'123-456-789';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are more interesting ideas from Bosque creators.&lt;/p&gt;

&lt;h2&gt;
  
  
  There are no loops in Bosque
&lt;/h2&gt;

&lt;p&gt;No, really, not a single loop construct. But don't worry, you can still perform iteration - just use built-in methods for that. No need to create a "manual" &lt;code&gt;for&lt;/code&gt; or &lt;code&gt;while&lt;/code&gt;. Have a quick read:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The truth is that Bosque did not remove the iteration completely. What the creators did is they removed structures like the ones we mentioned previously: &lt;code&gt;while&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;do...while&lt;/code&gt;, and so on. Instead, every iterable data structure (such that we can perform an iteration on it) that is available in Bosque is equipped with a ton of methods that are performing iterative processing internally. The only thing we have to do is to pick the ones that will satisfy our needs and make use of them. This may seem like an impediment because we simply got used to structured loops. But in reality, for the price of some level of flexibility, we are better able to understand the code we write.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using the built-in methods we can create programs that utilize iteration without having to write any loops. I prove that in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Artificial Intelligence in Bosque? You can do that
&lt;/h2&gt;

&lt;p&gt;Although Bosque is on the early stage of development you can create quite interesting projects. One of such projects is an AI classifier. Here are a few citations:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[...] Our end result should consist of two programs. The first one should be used to train and test our intelligent model and return trained values (we'll talk about these in the next section). The second one should accept those learned values as parameters plus two coordinates – X and Y – and return a string that will indicate which quadrant the point with these coordinates belongs to.&lt;br&gt;
[...]&lt;br&gt;
Neural networks are mathematical structures that are designed to perform calculations by using a collection of nodes called neurons. An artificial neuron is an elementary unit of a neural network. Basically, it's a mathematical function that receives input and produces output. The artificial neuron usually has multiple input values (input vectors) that are weighted (weight vectors).&lt;br&gt;
[...]&lt;br&gt;
Alright, after this very quick introduction to neural networks, we are ready to put the theory into practice. Our task is a simple classification problem. We want to assign every&lt;br&gt;
point to one of four classes (quadrants). As mentioned earlier, in order to recognize four classes, we need two perceptrons. Each of these two perceptrons will produce only one of&lt;br&gt;
the two possible outputs – 1 or 0, which maps to a certain class. If the output is 1, we can say that the given perceptron has activated.&lt;br&gt;
[...]&lt;br&gt;
In order to train the perceptron, we need to perform certain operations iteratively until our training set is fully processed. At each step of iteration, we have to take the training&lt;br&gt;
sample, create an input vector from it, and multiply this and the current weights vector. Then, we need to pass the result to the activation function and use the output to update&lt;br&gt;
the weights. These steps must be repeated for all of the training samples.&lt;br&gt;
[...]&lt;br&gt;
The best practice is to always wrap the constantly repeating&lt;br&gt;
code in a function so that it will be easier to maintain. So, let's go ahead and wrap the&lt;br&gt;
repeating piece of code in a classify function. This is what it can look like:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function classify(x: Float64, y: Float64, perceptron: Perceptron): Float64 {
    let sum = multiplyVectors(List&amp;lt;Float64&amp;gt;@{ x, y }, perceptron.weights);
    return activation(sum);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;It's hard to show all examples here. The whole idea standing behind Bosque, its syntax and approach is a topic for a whole series of articles or... a book :) Anyway, I encourage you to have a look at the language. People at Microsoft have proven that they know how to create good programming lanugages - C# and TypeScript are the best examples. Who knows, maybe one day it will turn out to be a great success?&lt;/p&gt;

</description>
      <category>books</category>
      <category>functional</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Can I use MySQL with Node.js?</title>
      <dc:creator>Pan Seba</dc:creator>
      <pubDate>Mon, 10 May 2021 20:34:59 +0000</pubDate>
      <link>https://forem.com/catchmareck/can-i-use-mysql-with-node-js-57m</link>
      <guid>https://forem.com/catchmareck/can-i-use-mysql-with-node-js-57m</guid>
      <description>&lt;p&gt;For far too many times have I heard that question and far too many times have I heard someone saying that it’s not a popular combination or questioning this choice. It’s time to finally release my frustration and dispel any doubts on this topic.&lt;/p&gt;

&lt;p&gt;Here’s what we are going to cover in this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why MySQL may look unnatural for Node?&lt;/li&gt;
&lt;li&gt;What database should you choose when working with Node?&lt;/li&gt;
&lt;li&gt;Can I use MySQL with Node?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The aim of this article is to explain the common misconception about using MySQL with Node.js and to assure you that there is completely nothing wrong with using this database together with Node.js. Let’s get started.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why MySQL may look unnatural for Node?
&lt;/h1&gt;

&lt;p&gt;I think the whole uncertainty of choosing MySQL for Node.js projects comes from the conviction that when you are using Node and want to store your data in a database then you should pick MongoDB. Why is that? Well, I think we need to get back in time to 2013 when the acronym &lt;strong&gt;MEAN&lt;/strong&gt; was coined.&lt;/p&gt;

&lt;p&gt;What is MEAN and what does it stand for? It is a well-known JavaScript software stack for building full-stack web applications. It stands for:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;M&lt;/strong&gt;ongoDB&lt;br&gt;
&lt;strong&gt;E&lt;/strong&gt;xpress.js&lt;br&gt;
&lt;strong&gt;A&lt;/strong&gt;ngular&lt;br&gt;
&lt;strong&gt;N&lt;/strong&gt;odejs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All of these components are technologies supporting programs written in JavaScript. &lt;em&gt;MongoDB&lt;/em&gt; is a non-relational database for storing document-oriented data. &lt;em&gt;Express.js&lt;/em&gt; is a web framework designed for &lt;em&gt;Node.js&lt;/em&gt;. And &lt;em&gt;Angular&lt;/em&gt; is a frontend framework created for building dynamic web applications. All of them support JS and therefore when choosing this stack you can build the whole system using a single programming language. It’s the essence of the &lt;em&gt;„JavaScript everywhere”&lt;/em&gt; paradigm.&lt;/p&gt;

&lt;p&gt;Because knowing just a single programming language enabled developers to build the whole web apps the MEAN stack became very popular. As new JavaScript frameworks was created, new versions of this stack evolved. Today we have the three most popular JS frameworks and because of that we have three variations of the MEAN stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MEAN – the original stack with Angular as the main frontend technology&lt;/li&gt;
&lt;li&gt;MERN – a variation using React as the frontend technology&lt;/li&gt;
&lt;li&gt;MEVN – a variation using Vue as the frontend technology&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Today, the MERN stack became even more popular than the original variation because React's popularity is skyrocketing.&lt;/p&gt;

&lt;p&gt;There are dozens of free tutorials, courses and videos showing how to get started with these stacks. By googling any of these we can learn that using them you can build any web application with only one language – JavaScript.&lt;/p&gt;

&lt;p&gt;All of this caused that today these stacks seem to be a natural combination for Node. This belief becomes stronger as new developers learn that it’s the only right way of building JS apps and share it further. MongoDB became a default choice for Node apps – especially for junior developers or people who are completely new to programming. &lt;strong&gt;Of course, this is wrong.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So what is the best option for Node? What should be your default choice when choosing a database for Node? Let’s find out.&lt;/p&gt;

&lt;h1&gt;
  
  
  What database should you choose when working with Node?
&lt;/h1&gt;

&lt;p&gt;Let’s start with the fact that &lt;strong&gt;there is no such thing as a default database for any technology&lt;/strong&gt;. That’s not how it works. You never should pick a database based on the technology that you use in your application. The reason is that similarly to the choice of backend and frontend technology it all depends on requirements. The project requirements should be your main deciding factor for database technology. On top of that, when your architecture is well-designed the kind of your database becomes a technical detail that does not have anything in common with programming languages used to create a backend.&lt;/p&gt;

&lt;p&gt;Every database available on the market has its own advantages and will be the best choice for projects that it has been created for. Taking only the two databases that we are discussing here – MySQL and MongoDB – each of them should (and should not) be used in certain scenarios. If in your project you must deal with a large amount of data (like really large) or the data is unstructured or it has a potential for rapid growth then probably the better choice is to use MongoDB. On the other hand, if you have structured data or your priority is data security or you just need a relational database then MySQL is a lot better choice.&lt;/p&gt;

&lt;p&gt;So let me repeat that again: there is no default database for any backend technology. It all depends on project requirements and what do you expect from the database. You should never be guided by the technology that you chose to create your backend.&lt;/p&gt;

&lt;p&gt;Having said that, let’s finally answer the main question of this article.&lt;/p&gt;

&lt;h1&gt;
  
  
  Can I use MySQL with Node?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Yes!&lt;/strong&gt; There is completely nothing wrong with that. I hope you realize this by now. The common misconception comes from the huge popularity of the &lt;code&gt;ME[ARV]N&lt;/code&gt; stacks and the fact that MongoDB feels very compatible with JavaScript because of its JSON-like document-oriented structure and the query language that looks like JavaScript. However this is only the illusion – yes, in many cases MongoDB is the best option for Node.js but not always. In equally many scenarios the best option will be MySQL or any other database available in the market.&lt;/p&gt;

&lt;p&gt;At the end of the day, we do have ORMs like TypeORM or Sequelize (or a dozen of others) that support MySQL drivers and they are very popular – it also means something.&lt;/p&gt;

&lt;p&gt;To sum up, the most important thing to realize is that there is no such thing as a default database for Node.js (or any technology) and that you never should pick MongoDB just because you use Node. In fact, Node.js does not care about data storage really – just like Java or Python, Node.js is a backend technology used to implement server-side business logic. You should always choose the database that meets your particular project needs regardless of the chosen programming language.&lt;/p&gt;

&lt;p&gt;With that in mind, go ahead and create a new Node.js project without worrying if MySQL will be a good choice.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>mysql</category>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
