<?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: Sergei</title>
    <description>The latest articles on DEV Community by Sergei (@sergeis).</description>
    <link>https://dev.to/sergeis</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%2F229405%2Ffe3bc8ff-ccab-47bf-9cfc-ee1513ddd5d6.png</url>
      <title>DEV Community: Sergei</title>
      <link>https://dev.to/sergeis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sergeis"/>
    <language>en</language>
    <item>
      <title>JavaScript ES6 one-liners: merge two sorted lists</title>
      <dc:creator>Sergei</dc:creator>
      <pubDate>Thu, 17 Oct 2019 07:37:17 +0000</pubDate>
      <link>https://dev.to/sergeis/javascript-es6-one-liners-merge-two-sorted-lists-2018</link>
      <guid>https://dev.to/sergeis/javascript-es6-one-liners-merge-two-sorted-lists-2018</guid>
      <description>&lt;p&gt;One of the steps in the merge-sort algorithm is merging two sorted lists together. In the spirit of doing everything on a single line (see my other posts), here is an admittedly hokey one-liner for merging two lists together (it creates a new merged list and clobbers the second of the two original sorted lists):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;orderedList1&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;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;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;orderedList2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;orderedList1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
  &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;orderedList2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
    &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderedList2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()):&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[]),&lt;/span&gt; 
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;orderedList2&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="c1"&gt;// [ 0, 1, 2, 3, 5, 5, 7, 8, 9, 11 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A bit of an explanation for this monstrosity:&lt;/p&gt;

&lt;p&gt;The outer "reduce" starts with an empty list, goes through each element of the first list, extracts all the elements of the second list that are ahead of the current element of the first list and inserts them into the merged list, and then inserts the current element. Finally, the remainder of the second list, if any, is appended at the end of the merged list. The fake ternary operator smuggles in the "some" call inside the spread operator collecting the merged list in progress. Not sure if this can be done more neatly.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>merge</category>
    </item>
    <item>
      <title>JavaScript ES6 one-liners: rotate an array</title>
      <dc:creator>Sergei</dc:creator>
      <pubDate>Wed, 16 Oct 2019 06:50:38 +0000</pubDate>
      <link>https://dev.to/sergeis/javascript-es6-one-liners-rotate-an-array-33bn</link>
      <guid>https://dev.to/sergeis/javascript-es6-one-liners-rotate-an-array-33bn</guid>
      <description>&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shift&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;// rotate left by n &amp;lt; array length:&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`rotated: &lt;/span&gt;&lt;span class="p"&gt;${[...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;shift&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="c1"&gt;// rotated: 3,4,5,1,2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>array</category>
      <category>rotate</category>
      <category>spread</category>
    </item>
    <item>
      <title>Removing duplicates from a string in one planet-size JavaScript statement</title>
      <dc:creator>Sergei</dc:creator>
      <pubDate>Sun, 22 Sep 2019 23:32:04 +0000</pubDate>
      <link>https://dev.to/sergeis/removing-duplicates-from-a-string-in-one-planet-size-javascript-statement-3gp2</link>
      <guid>https://dev.to/sergeis/removing-duplicates-from-a-string-in-one-planet-size-javascript-statement-3gp2</guid>
      <description>&lt;p&gt;JavaScript array functions are one feature borrowed from functional programming that are relatively easy to wrap your head around. I wrote before about doing &lt;a href="https://dev.to/sergeis/fizzbuzz-in-one-planet-size-javascript-statement-4oef"&gt;FizzBuzz in one planet-size JavaScript statement&lt;/a&gt;, and this post is about solving another basic coding interview question in one statement: removing duplicates from a string.&lt;/p&gt;

&lt;p&gt;A standard approach to removing duplicates is to &lt;a href="http://www.ardendertat.com/2012/01/06/programming-interview-questions-25-remove-duplicate-characters-in-string/"&gt;create a set of seen characters as we go through the string and only retain those that have not been seen&lt;/a&gt;. But that's not the only way to do it. Note that we can easily sort an array, so all the duplicates are bunched together, and then reduce repeated sequences to the first element only. Note the word &lt;strong&gt;reduce&lt;/strong&gt;, because that's the array function we will use to do it!&lt;/p&gt;

&lt;p&gt;It may seem expensive and wasteful to use sort() to accomplish what we need, but remember that comparison sorting is O(n log(n)), not far off O(n) that is the lowest order possible for string operations.&lt;/p&gt;

&lt;p&gt;So, here is what we intend to do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;sort the given string so that the duplicates are all together&lt;/li&gt;
&lt;li&gt;replace all substrings of repeated characters with the character that is repeated&lt;/li&gt;
&lt;li&gt;restore the original order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We can do all of those in a single JavaScript statement, though it gets a bit long. Here is an annotated example that you can copy and paste into the debug console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'4366447654434567876'.split('')             // string to array
    .map((e,i)=&amp;gt;({val:e,pos:i}))            // remember the original position of each character
    .sort((a,b)=&amp;gt;a.val.localeCompare(b.val))// sort by value
    .reduce((acc,e)=&amp;gt;acc.length == 0 
        || acc[acc.length-1].val!=e.val?    // keep if not a duplicate 
        acc.concat([e]):acc, 
        [])                                 // empty array as a seed        
    .sort((a,b)=&amp;gt;a.pos-b.pos)               // restore the original order
    .map(e=&amp;gt;e.val)                          // select original values
    .join('')                               // back to string!
;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And the answer is, as expected&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"436758"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Most of the above should be self-explanatory, but it is worth explaining what we did in the reduce() function:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;start with an empty accumulator array []&lt;/li&gt;
&lt;li&gt;insert the first element unconditionally (hence the check for acc.length == 0)&lt;/li&gt;
&lt;li&gt;for each subsequent element, if it is not a duplicate, add it to the accumulator array&lt;/li&gt;
&lt;li&gt;otherwise leave the accumulator unchanged.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, this is definitely more wasteful than using an intermediate set of seen characters, so don't offer it during a coding interview as your first choice! But it is neat to know that we can do it all in one (veeeery long) line. Or, if you are an interviewer who is bored out of their mind with asking the same questions over and over, you might want to mix it up by challenging the poor interviewee to answer a question like that in one line.&lt;/p&gt;

&lt;p&gt;Another note. It is not very hard to do the "set of seen" algorithm as a one-liner as well, can you do it?&lt;/p&gt;

</description>
      <category>duplicates</category>
      <category>javascript</category>
      <category>interview</category>
      <category>array</category>
    </item>
    <item>
      <title>Fun with Array.reduce(): get a nested object by name</title>
      <dc:creator>Sergei</dc:creator>
      <pubDate>Fri, 20 Sep 2019 07:06:46 +0000</pubDate>
      <link>https://dev.to/sergeis/fun-with-array-reduce-get-a-nested-object-by-name-377j</link>
      <guid>https://dev.to/sergeis/fun-with-array-reduce-get-a-nested-object-by-name-377j</guid>
      <description>&lt;p&gt;Say you have an object in the global scope such as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var level0 = {level1:{level2:{level3:'holy grail'}}};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;that you want to access the last level of, and you are given its name as a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var nestedName = 'level0.level1.level2.level3';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are, of course, many ways to get the value of the level3, and one way is to use Array.reduce():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
var getNestedValueByName = name=&amp;gt;name.split('.').reduce((acc, next)=&amp;gt;acc[next], window);

console.log(getNestedValueByName(nestedName));
// holy grail
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also works for arrays, only have to use indices instead of names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = [['first', 'second'],['third', 'fourth']];

console.log(getNestedValueByName('a.1.0'));
// third
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>array</category>
      <category>reduce</category>
    </item>
    <item>
      <title>FizzBuzz in one planet-size JavaScript statement</title>
      <dc:creator>Sergei</dc:creator>
      <pubDate>Wed, 18 Sep 2019 02:28:25 +0000</pubDate>
      <link>https://dev.to/sergeis/fizzbuzz-in-one-planet-size-javascript-statement-4oef</link>
      <guid>https://dev.to/sergeis/fizzbuzz-in-one-planet-size-javascript-statement-4oef</guid>
      <description>&lt;p&gt;JavaScript has many tempting features and it is easy to get carried away. And so I have. Here is a FizzBuzz one-liner that uses a whole whack of ES5 and ES6 features: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array.from()&lt;/li&gt;
&lt;li&gt;Array-like objects&lt;/li&gt;
&lt;li&gt;Array.assign()&lt;/li&gt;
&lt;li&gt;map&lt;/li&gt;
&lt;li&gt;forEach&lt;/li&gt;
&lt;li&gt;ternary operator&lt;/li&gt;
&lt;li&gt;truthy and falsy values&lt;/li&gt;
&lt;li&gt;IIFE&lt;/li&gt;
&lt;li&gt;and, of course, fat arrow functions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(n=&amp;gt;Array.from({length:n}, (_,i)=&amp;gt;i+1)
.map(e=&amp;gt;({text:e+' ', value: e}))
.map(e=&amp;gt; e.value%3? e: Object.assign(e, {text:e.text+'Fizz'}))
.map(e=&amp;gt; e.value%5? e: {value:e.value, text:e.text+'Buzz'})
.forEach(e=&amp;gt;console.log(e.text)))(100)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Compare this to a purely procedural C-like version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (var i = 1; i &amp;lt;= 100; i++) 
  console.log(i+' '+(i%15==0?"FizzBuzz":i%3==0?"Fizz":i%5==0?"Buzz":""));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It takes way less resources and probably runs ten times faster.&lt;/p&gt;

&lt;p&gt;The lesson? Sometimes it's best to avoid the frills and stick to the basics.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>fizzbuzz</category>
    </item>
  </channel>
</rss>
