<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Guy Ariely</title>
    <description>The latest articles on DEV Community by Guy Ariely (@guyariely).</description>
    <link>https://dev.to/guyariely</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%2F619502%2F1f0ff352-b6d7-409a-8854-69a2435fce5a.jpeg</url>
      <title>DEV Community: Guy Ariely</title>
      <link>https://dev.to/guyariely</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/guyariely"/>
    <language>en</language>
    <item>
      <title>Using Python range() in JavaScript</title>
      <dc:creator>Guy Ariely</dc:creator>
      <pubDate>Wed, 04 Aug 2021 20:37:47 +0000</pubDate>
      <link>https://dev.to/guyariely/using-python-range-in-javascript-337p</link>
      <guid>https://dev.to/guyariely/using-python-range-in-javascript-337p</guid>
      <description>&lt;h2&gt;
  
  
  What is the Python range() type?
&lt;/h2&gt;

&lt;p&gt;If you’re not familiar with Python, &lt;code&gt;range()&lt;/code&gt; refers to the use of the range type to create an immutable sequence of numbers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.” — &lt;a href="https://docs.python.org/3/library/stdtypes.html#range"&gt;docs.python.org&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;range()&lt;/code&gt; constructor has two forms of definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;range(stop)
range(start, stop[, step])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;A concise explanation of the parameters, return value, etc. can be found on &lt;a href="https://www.programiz.com/python-programming/methods/built-in/range"&gt;programiz&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A few examples:&lt;/p&gt;


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



&lt;h2&gt;
  
  
  Building the range() function in JavaScript
&lt;/h2&gt;

&lt;p&gt;For simplicity sake, we will ignore the optional &lt;code&gt;step&lt;/code&gt; argument.&lt;/p&gt;

&lt;p&gt;By using the &lt;code&gt;Array&lt;/code&gt; constructor, &lt;code&gt;fill&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt;, you could work out a simple solution in a quick one-liner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;new Array(stop - start).fill(start).map((el, i) =&amp;gt; el + i)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And maybe then offer a more complete solution, covering the case of calling range with only one argument:&lt;/p&gt;


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



&lt;p&gt;But it’s &lt;em&gt;not&lt;/em&gt; quite it yet. Can you see why this solution is wrong?&lt;/p&gt;

&lt;p&gt;Remember, calling Python &lt;code&gt;range&lt;/code&gt; returns an &lt;strong&gt;immutable sequence of numbers&lt;/strong&gt;. Notice how in order to get the familiar list data structure, the Python examples above wrap the return value of range with &lt;code&gt;list()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An equal example in JavaScript should probably look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Array.from(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So how can we make a function in JavaScript return an “immutable sequence of numbers”? Is there any way to achieve the same structure of the &lt;code&gt;range()&lt;/code&gt; return value in JavaScript?&lt;/p&gt;
&lt;h2&gt;
  
  
  Iterators to the rescue!
&lt;/h2&gt;

&lt;p&gt;Iterators are wildly used in different programming languages to allow us to iterate over different data structures.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“In JavaScript an iterator is an object which defines a sequence and potentially a return value upon its termination.” —&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators"&gt;developer.mozilla.org&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Specifically in JavaScript, an iterator is any object which implements the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol"&gt;Iterator protocol&lt;/a&gt; by having a &lt;code&gt;next()&lt;/code&gt; method that returns an object with two properties:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;next() {
  ...
  return {
    value: // current value to be passed
    done: // did we finish iterating over the data structure?
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Using an iterator, you can provide your own logic on how to iterate. For example, here is a simple iterator that will skip every second item:&lt;/p&gt;


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



&lt;p&gt;More importantly, If we create an object that defines &lt;code&gt;[Symbol.iterator]&lt;/code&gt; which returns that iterator, &lt;strong&gt;we can get exactly the behavior we were looking for&lt;/strong&gt;:&lt;/p&gt;


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


&lt;p&gt;Play around with these examples and see what kind of interesting and useful iterators you can create 💪.&lt;/p&gt;

&lt;p&gt;By now you can probably imagine how we might approach creating Python &lt;code&gt;range()&lt;/code&gt; in JavaScript. This is how I implemented it:&lt;/p&gt;


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


&lt;p&gt;As I mentioned, for simplicity's sake this implementation leaves out the &lt;code&gt;step&lt;/code&gt; argument from the original Python &lt;code&gt;range()&lt;/code&gt;, but it’s just a matter of extra logic that you can implement yourself. Feel free to @ me your solution ✌️.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>python</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
