<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.6.2">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2017-11-03T10:41:12+00:00</updated><id>https://afshinm.name/</id><title type="html">Afshin</title><subtitle>Notes about programming, math and adventures
</subtitle><author><name>Afshin Mehrabani</name></author><entry><title type="html">Are you alive?</title><link href="/2017/09/03/are-you-alive/" rel="alternate" type="text/html" title="Are you alive?" /><published>2017-09-03T20:27:28+00:00</published><updated>2017-09-03T20:27:28+00:00</updated><id>https://afshinm.name/2017/09/03/are-you-alive</id><content type="html" xml:base="https://afshinm.name/2017/09/03/are-you-alive/">&lt;p&gt;If you are actually living (not just surviving), you have probably experienced many disappointments in your journey. However, if your growth in life is following a linear function, then probably you are not alive or it’s better to say, you don’t want to. It is likely that you don’t do anything exciting in your life or you prefer to take a safe path and not taking any risks in your &lt;a href=&quot;https://afshinm.name/tag/life/&quot;&gt;lifetime&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Imagine this chart:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;size-full wp-image-173 aligncenter&quot; src=&quot;https://afshinm.name/wp-content/uploads/2017/09/Screen-Shot-2017-09-03-at-21.08.27.png&quot; alt=&quot;Growth in life - linear &quot; width=&quot;696&quot; height=&quot;441&quot; /&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Above chart illustrates a boring life. Bullshit. It means you don’t enjoy your life and one day you will get bored of and you will regret the decisions you could have made but you didn’t. It doesn’t really matter if you are following a &lt;strong&gt;y = x&lt;/strong&gt;, &lt;strong&gt;y = x/2&lt;/strong&gt; or &lt;strong&gt;y = 1&lt;/strong&gt;. This is a boring life.&lt;/p&gt;

&lt;p&gt;Whereas, consider this one:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;size-full wp-image-175 aligncenter&quot; src=&quot;https://afshinm.name/wp-content/uploads/2017/09/Screen-Shot-2017-09-03-at-21.13.49.png&quot; alt=&quot;Growth in life - better version&quot; width=&quot;735&quot; height=&quot;488&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This one shows the growth of someone that wants to take a risk. Wants to experience new things and explore the world. If you ask yourself that you really like to take a risk and make a change in your life, the answer of your heart is “yes”. We, as humans, have something that no animal has. We are in charge of our lives and we have to accept dark moments to gain more.&lt;/p&gt;

&lt;p&gt;I decided to write this post and share my opinions when I experienced a terrible situation a few days ago. This is what happened.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2 id=&quot;failure&quot;&gt;Failure&lt;/h2&gt;

&lt;p&gt;About 4 years ago, I decided to start my own software company and spend my time on what I wanted to. I did that and I spent a year to establish the company with my friends.&lt;/p&gt;

&lt;p&gt;I was young and I didn’t know how important contract and legal work is. We started the company, I built the software but I didn’t have anything to prove that I own a part of the company (slow clap).&lt;/p&gt;

&lt;p&gt;One of the guys designed a fake argument and as a result, they took my share of the company. And yes, they had access to the entire source code. I didn’t have any other choices but to start from scratch, again. I started interviewing with a few companies. It was unbelievable. I can’t explain how terrible that feeling was.&lt;/p&gt;

&lt;p&gt;I spent a week at home to think and relax a bit. Then I said to myself, I have to start my own open-source project. I don’t remember how I came up with this decision but the result of that decision was &lt;a href=&quot;http://introjs.com&quot;&gt;Introjs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2 id=&quot;failure-again&quot;&gt;Failure, again&lt;/h2&gt;

&lt;p&gt;A few days ago, I experienced one of my darkest moments in my life. I heard a disappointing news, and I had to accept that. There were no alternatives.&lt;/p&gt;

&lt;p&gt;I signed a game changer contract but after a few weeks, the company forced me to terminate the contract, due to a ridiculous legal problem. The reason is not important though.&lt;/p&gt;

&lt;p&gt;I spent a week, analysing my life, my goals and what I really wanted to do. I had two options. To keep thinking about the negative and dark parts of my life, and that event. Or, take advantage of what happened. I took the latter path.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2 id=&quot;what-im-going-to-do&quot;&gt;What I’m going to do?&lt;/h2&gt;

&lt;p&gt;I decided to work harder. Prioritising tasks in my life and spending more time on the important ones. Reading more books and using my spare time better.&lt;/p&gt;

&lt;p&gt;Just because that thing happened, it doesn’t mean I will use it as an excuse.&lt;/p&gt;

&lt;p&gt;I made the decision to work on &lt;a href=&quot;http://juggernaut.rs&quot;&gt;Juggernaut&lt;/a&gt; and start my dream project, Streamer.js. I will publish the updates soon.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2 id=&quot;to-be-continued&quot;&gt;To be continued…&lt;/h2&gt;

&lt;p&gt;I wrote this blog post as a reminder for myself. Those things have happened to me and it is very likely to experience the same situation again in the future.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Everything that happens once can never happen twice. But everything that happens twice will surely happen a third time.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I wrote this post to have something to refer to.  Stay strong, keep going.&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="life" /><category term="lifeahack" /><category term="motivation" /><summary type="html">If you are actually living (not just surviving), you have probably experienced many disappointments in your journey. However, if your growth in life is following a linear function, then probably you are not alive or it’s better to say, you don’t want to. It is likely that you don’t do anything exciting in your life or you prefer to take a safe path and not taking any risks in your lifetime.</summary></entry><entry><title type="html">Don’t abuse language features</title><link href="/2017/08/23/dont-abuse-language-features/" rel="alternate" type="text/html" title="Don't abuse language features" /><published>2017-08-23T16:29:03+00:00</published><updated>2017-08-23T16:29:03+00:00</updated><id>https://afshinm.name/2017/08/23/dont-abuse-language-features</id><content type="html" xml:base="https://afshinm.name/2017/08/23/dont-abuse-language-features/">&lt;p&gt;Recently we had a situation in a ES6 project. We were using some of new features ES6 in a wrong way. I’m writing this blog post to explain what was the issue and what is the solution to avoid these kind of problems it the future.&lt;/p&gt;

&lt;h2 id=&quot;es6-promise&quot;&gt;ES6 Promise&lt;/h2&gt;

&lt;p&gt;We designed a Message box class which accepts a title, text and buttons. Then, you can call &lt;code class=&quot;highlighter-rouge&quot;&gt;open&lt;/code&gt; to display the message box. User can decide to click on &lt;code class=&quot;highlighter-rouge&quot;&gt;Yes&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;No&lt;/code&gt; button (assume there is a Accept / Reject button on the message box).&lt;/p&gt;

&lt;p&gt;That &lt;code class=&quot;highlighter-rouge&quot;&gt;open&lt;/code&gt; function returns a &lt;code class=&quot;highlighter-rouge&quot;&gt;Promise&lt;/code&gt; which fulfills the Promise if user clicked on &lt;code class=&quot;highlighter-rouge&quot;&gt;Yes&lt;/code&gt; and rejects it if you click on &lt;code class=&quot;highlighter-rouge&quot;&gt;No&lt;/code&gt; button. Therefore you can decide what to do with the Promise.&lt;/p&gt;

&lt;p&gt;This is sample of that API:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let msg = Message({ 
	text: &quot;Hello world&quot;, 
	title: &quot;Are you sure?&quot;,
}).open(); //&amp;lt;code&amp;gt;msg is a Promise now &amp;lt;/code&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Now, to test and see if user clicked on Accept or Reject button, you can use &lt;code class=&quot;highlighter-rouge&quot;&gt;then()&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;catch()&lt;/code&gt; of Promise object. But we didn’t use these functions. Instead, we use &lt;code class=&quot;highlighter-rouge&quot;&gt;try...catch&lt;/code&gt; with &lt;code class=&quot;highlighter-rouge&quot;&gt;async / await&lt;/code&gt; keywords.&lt;/p&gt;

&lt;h2 id=&quot;incorrect-usage-of-promise&quot;&gt;Incorrect usage of Promise&lt;/h2&gt;

&lt;p&gt;Another way to handle Promise is to use async and await keywords, like so:&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(async function () {
	try {
  	let res = await ReturnPromise();
    console.log(&quot;gotcha&quot;, res);
  } catch (e) {
	console.warn('opssss');
  }
}());
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;It would reach the catch block if user clicked on Reject and the try block if Accept (actually, it would execute anything after &lt;code class=&quot;highlighter-rouge&quot;&gt;await&lt;/code&gt; keyword, otherwise, it throws an exception and hits the &lt;code class=&quot;highlighter-rouge&quot;&gt;catch&lt;/code&gt; block).&lt;/p&gt;

&lt;p&gt;Well, above usage is correct, too, but there are many problem with that approach:&lt;/p&gt;

&lt;h3 id=&quot;1-suppressing-errors&quot;&gt;1) Suppressing errors&lt;/h3&gt;

&lt;p&gt;If you add anything to that &lt;code class=&quot;highlighter-rouge&quot;&gt;try&lt;/code&gt; block, you would need to handle the errors on the &lt;code class=&quot;highlighter-rouge&quot;&gt;catch&lt;/code&gt; block. If you don’t handle them in try &lt;code class=&quot;highlighter-rouge&quot;&gt;catch&lt;/code&gt; block, it would suppress the errors and wouldn’t be able to understand what is going on in your application in case of any errors.&lt;/p&gt;

&lt;h3 id=&quot;2-catch-block&quot;&gt;2) Catch block&lt;/h3&gt;

&lt;p&gt;You cannot remove the &lt;code class=&quot;highlighter-rouge&quot;&gt;catch&lt;/code&gt; block. You should either leave it empty or add something between the &lt;code class=&quot;highlighter-rouge&quot;&gt;catch&lt;/code&gt; block. Imagine your application with a milion of empty &lt;code class=&quot;highlighter-rouge&quot;&gt;catch&lt;/code&gt; blocks:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(async function () {
	try {
  	let res = await ReturnPromise();
    console.log(&quot;gotcha&quot;, res);
  } catch (e) { } // &amp;lt;-- this is the empty block
}());
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Also, sometimes you just need to handle the Accept case and there is no need to handle the &lt;code class=&quot;highlighter-rouge&quot;&gt;catch&lt;/code&gt; or Reject case. Now, you can’t do anything but adding an empty &lt;code class=&quot;highlighter-rouge&quot;&gt;catch&lt;/code&gt; block.&lt;/p&gt;

&lt;h3 id=&quot;3-you-are-not-handling-an-error&quot;&gt;3) You are not handling an error!&lt;/h3&gt;

&lt;p&gt;You should use try catch when you want to handle an error and do something instead of letting the application to crash and exit the main. Is that case, we were not handling an error. Clicking on a Reject button is not an error.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2 id=&quot;write-maintainable-code&quot;&gt;Write maintainable code&lt;/h2&gt;

&lt;p&gt;Remember, you are writing codes and it should be readable and understandable for other contributors and if you use some sort of weird syntax in your program, it doesn’t prove anything about the level of your coding skills or how talented you are. A &lt;a href=&quot;http://programmingisterrible.com/post/102153932958/how-i-write-software&quot;&gt;good code&lt;/a&gt; is boring to read.&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="es6" /><category term="good code" /><category term="language" /><category term="promise" /><summary type="html">Recently we had a situation in a ES6 project. We were using some of new features ES6 in a wrong way. I’m writing this blog post to explain what was the issue and what is the solution to avoid these kind of problems it the future.</summary></entry><entry><title type="html">OpenAI bot vs. Dendi - The International 2017 Dota2</title><link href="/2017/08/12/openai-bot-vs-dendi-international-2017-dota2/" rel="alternate" type="text/html" title="OpenAI bot vs. Dendi - The International 2017 Dota2" /><published>2017-08-12T09:53:24+00:00</published><updated>2017-08-12T09:53:24+00:00</updated><id>https://afshinm.name/2017/08/12/openai-bot-vs-dendi-international-2017-dota2</id><content type="html" xml:base="https://afshinm.name/2017/08/12/openai-bot-vs-dendi-international-2017-dota2/">&lt;p&gt;&lt;a href=&quot;http://wiki.teamliquid.net/dota2/Dendi&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Dendi&lt;/a&gt; is one of the most famous Dota2 players, because of his abilities in &lt;a href=&quot;https://www.youtube.com/watch?v=yeGQe0dgkZw&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;playing with Pudge&lt;/a&gt;. Last night, as a part of &lt;a href=&quot;https://afshinm.name/tag/ti17/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;The International 2017&lt;/a&gt;, Dendi played with an OpenAI bot.&lt;/p&gt;

&lt;p&gt;The funny part is, he lost the 1 vs. 1 two times and then he said, “I give up!”. Apparently, the bot was so accurate.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2 id=&quot;blocking-creeps&quot;&gt;Blocking creeps&lt;/h2&gt;

&lt;p&gt;When it comes to blocking the creeps. the bot played much better than Dendi. This is the first game &lt;a href=&quot;https://youtu.be/9_cS7oWtp1o?t=4m1s&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;when the bot blocks creeps&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the second game, there were two lines of creeps but the bot managed to &lt;a href=&quot;https://youtu.be/9_cS7oWtp1o?t=10m12s&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;block both lines&lt;/a&gt; very nicely.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2 id=&quot;first-blood&quot;&gt;First blood&lt;/h2&gt;

&lt;p&gt;In the second game, the bot drew first blood in a &lt;a href=&quot;https://youtu.be/9_cS7oWtp1o?t=11m14s&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;very complex situation&lt;/a&gt;. Under the tower with half hp.&lt;/p&gt;

&lt;p&gt;And the nice part of this fight is, the bot used his 3rd skill (not first or second skill) to accurately hit the target.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This is so amazing that Dota2 introduces AI this year. Probably, something more important is going on behind the scene. We have to wait and see how AI and &lt;a href=&quot;https://openai.com/the-international/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;this relationship with OpenAI&lt;/a&gt; would affect the experience of Dota2 in the future.&lt;/p&gt;

&lt;p&gt;This is the official blog post from OpenAI team that explain the process of training the bot and techniques that the bot can utilize to defeat the opponent: &lt;a href=&quot;https://blog.openai.com/dota-2/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://blog.openai.com/dota-2/&lt;/a&gt;&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="dendi" /><category term="dota2" /><category term="openai" /><category term="ti17" /><summary type="html">Dendi is one of the most famous Dota2 players, because of his abilities in playing with Pudge. Last night, as a part of The International 2017, Dendi played with an OpenAI bot.</summary></entry><entry><title type="html">Character density using Haskell</title><link href="/2016/12/31/character-density-using-haskell/" rel="alternate" type="text/html" title="Character density using Haskell" /><published>2016-12-31T13:57:08+00:00</published><updated>2016-12-31T13:57:08+00:00</updated><id>https://afshinm.name/2016/12/31/character-density-using-haskell</id><content type="html" xml:base="https://afshinm.name/2016/12/31/character-density-using-haskell/">&lt;p&gt;In this short tutorial, I’m going to write a simple Haskell program to calculate the density of characters in a given text. For instance, in “hello” we have &lt;code class=&quot;highlighter-rouge&quot;&gt;[('e',0.2),('h',0.2),('l',0.4),('o',0.2)]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The return format of our application would be a list of tuples&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;String -&amp;gt; [(Char, Float)]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To make the problem easier to solve, I’m going to break it into two programs. One that returns the number of repeats for each character and another program that converts that list to a percentage of repeats.&lt;/p&gt;

&lt;h2 id=&quot;count-of-repeats&quot;&gt;Count of repeats&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;freq_letter :: String -&amp;gt; [(Char, Int)]
freq_letter str = [ (x,c) | x &amp;lt;- ['a'..'z'], let c = (length.filter (==x)) str, c&amp;gt;0 ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This program returns a list of tuples with the Char and Int. First item of tuple is the character in the given text and the second parameter, Int, is the count of repeats in the text. Executing this program will return:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;*Main&amp;gt; freq_letter &quot;hello&quot;
[('e',1),('h',1),('l',2),('o',1)]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;percentage-of-repeats&quot;&gt;Percentage of repeats&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;freq_letter_pc :: String -&amp;gt; [(Char, Float)]
freq_letter_pc str = [ (char, fromIntegral(count) / fromIntegral(length str)) | x &amp;lt;- freq_letter (map toLower str), let (char,count) = x]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Second part of the application uses the previous program to get the list of repeats and then divide them by the length of the given text. This way we can calculate the density of characters in the text. Executing this program returns:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;*Main&amp;gt; freq_letter_pc &quot;hello&quot;
[('e',0.2),('h',0.2),('l',0.4),('o',0.2)]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That’s it.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In this tutorial, we have learnt how to calculate the density of characters using Haskell. Achieving this goal is much easier in Haskell because of list comprehension and higher order functions.&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="density" /><category term="haskell" /><category term="text processing" /><summary type="html">In this short tutorial, I’m going to write a simple Haskell program to calculate the density of characters in a given text. For instance, in “hello” we have [('e',0.2),('h',0.2),('l',0.4),('o',0.2)].</summary></entry><entry><title type="html">How to fix Library not loaded: libmysqlclient.18.dylib in Mac OS X</title><link href="/2016/02/05/how-to-fix-library-not-loaded-libmysqlclient-18-dylib-in-mac-os-x/" rel="alternate" type="text/html" title="How to fix Library not loaded: libmysqlclient.18.dylib in Mac OS X" /><published>2016-02-05T11:50:33+00:00</published><updated>2016-02-05T11:50:33+00:00</updated><id>https://afshinm.name/2016/02/05/how-to-fix-library-not-loaded-libmysqlclient-18-dylib-in-mac-os-x</id><content type="html" xml:base="https://afshinm.name/2016/02/05/how-to-fix-library-not-loaded-libmysqlclient-18-dylib-in-mac-os-x/">&lt;p&gt;Well, sometimes a blog simple tip can save a day. That’s why I created this blog.&lt;/p&gt;

&lt;p&gt;In this post, I’m going to show a simple way to fix &lt;code class=&quot;prettyprint&quot;&gt;Library not loaded: libmysqlclient.18.dylib&lt;/code&gt; error. It could happen when you upgrade your MySQL to the latest version (here it’s &lt;code class=&quot;prettyprint&quot;&gt;5.7.10&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Just run following command:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;sudo ln -s /usr/local/Cellar/mysql/5.7.10/lib/libmysqlclient.dylib /usr/local/lib/libmysqlclient.18.dylib
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;OR&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The reason for that error is that your client (let’s say Python Django) is unable to find your &lt;code class=&quot;prettyprint&quot;&gt;libmysqlclient&lt;/code&gt; library. That way you can link that library again. everything else should work fine.&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="libmysqlclient" /><category term="macos" /><summary type="html">Well, sometimes a blog simple tip can save a day. That’s why I created this blog.</summary></entry><entry><title type="html">Optimized solution to calculate first powers of 2</title><link href="/2015/06/21/optimized-solution-to-calculate-first-powers-of-2/" rel="alternate" type="text/html" title="Optimized solution to calculate first powers of 2" /><published>2015-06-21T12:08:57+00:00</published><updated>2015-06-21T12:08:57+00:00</updated><id>https://afshinm.name/2015/06/21/optimized-solution-to-calculate-first-powers-of-2</id><content type="html" xml:base="https://afshinm.name/2015/06/21/optimized-solution-to-calculate-first-powers-of-2/">&lt;p&gt;I experienced many programming contests and olympiads but this one was really different. In a programming contest on Hacker Rank, I faced with a challenging problem and it was the main reason I failed the test. That question made me nervous and stressful so I gave up. When I found the solution after the test, I laughed so loud.&lt;/p&gt;

&lt;p&gt;The question is:&lt;/p&gt;

&lt;blockquote class=&quot;large&quot;&gt;
  &lt;p&gt;
    Calculate sum of first powers of 2 without using any built-in language API to calculate the power. Implement optimized solution.
  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, there are two factors. First, not using built-in functions like &lt;code class=&quot;prettyprint&quot;&gt;Math.pow&lt;/code&gt; and second implementing the optimized solution. So, of course a recursive function or for-loop is not the answer.&lt;/p&gt;

&lt;h1 id=&quot;answer_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#answer_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Answer&lt;/h1&gt;

&lt;p&gt;Use bitwise operations. How? Ok, let me show you a trick.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;parseInt(10, '2')    == Math.pow(2, 1); //2^1 = 2
parseInt(100, '2')   == Math.pow(2, 2); //2^2 = 4
parseInt(1000, '2')  == Math.pow(2, 3); //2^3 = 8
parseInt(10000, '2') == Math.pow(2, 4); //2^4 = 16
//and so on
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, how to make &lt;code class=&quot;prettyprint&quot;&gt;10&lt;/code&gt;, &lt;code class=&quot;prettyprint&quot;&gt;100&lt;/code&gt; or &lt;code class=&quot;prettyprint&quot;&gt;1000&lt;/code&gt;? Using &lt;strong&gt;shift operations&lt;/strong&gt; you can make it:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;1 &amp;lt;&amp;lt; 1 //2
1 &amp;lt;&amp;lt; 2 //4
1 &amp;lt;&amp;lt; 3 //8
1 &amp;lt;&amp;lt; 4 //16
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A short explanation of above source code is that the binary of &lt;code class=&quot;prettyprint&quot;&gt;1&lt;/code&gt; is &lt;code class=&quot;prettyprint&quot;&gt;00000001&lt;/code&gt; and when you use left shift operator, the result is &lt;code class=&quot;prettyprint&quot;&gt;00000010&lt;/code&gt;, &lt;code class=&quot;prettyprint&quot;&gt;00000100&lt;/code&gt; and so on.&lt;/p&gt;

&lt;p&gt;So the answer is to use a for-loop for &lt;code class=&quot;prettyprint&quot;&gt;N&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;var sum = 0;
for (var i = 1;i &amp;lt;= n;i++) {
  sum += 1 &amp;lt;&amp;lt; i;
}
console.log(sum);
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&quot;can-we-make-it-better_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#can-we-make-it-better_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Can we make it better?&lt;/h1&gt;

&lt;p&gt;Yes. Here is the final hack.&lt;/p&gt;

&lt;p&gt;The sum of first N powers of 2 is &lt;code class=&quot;prettyprint&quot;&gt;2^(N + 1) - 2&lt;/code&gt;. For instance, &lt;code class=&quot;prettyprint&quot;&gt;n = 3&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;(2 ^ (3 + 1)) - 2 = 16 - 2 = 2 + 4 + 8 = 14
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And then, using bitwise operations we have:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;console.log((2 &amp;lt;&amp;lt; n) - 2);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Please note that the binary of &lt;code class=&quot;prettyprint&quot;&gt;2&lt;/code&gt; is &lt;code class=&quot;prettyprint&quot;&gt;00000010&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Actually, this is the right answer.&lt;/p&gt;

&lt;p&gt;Edit: You can find more interesting examples here: &lt;a href=&quot;http://www.cs.utsa.edu/%7Ewagner/knuth/fasc1a.pdf&quot; rel=&quot;nofollow&quot;&gt;http://www.cs.utsa.edu/~wagner/knuth/fasc1a.pdf&lt;/a&gt;&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="math" /><category term="power" /><category term="power of 2" /><summary type="html">I experienced many programming contests and olympiads but this one was really different. In a programming contest on Hacker Rank, I faced with a challenging problem and it was the main reason I failed the test. That question made me nervous and stressful so I gave up. When I found the solution after the test, I laughed so loud.</summary></entry><entry><title type="html">MongoDB Aggregation</title><link href="/2015/06/14/mongodb-aggregation/" rel="alternate" type="text/html" title="MongoDB Aggregation" /><published>2015-06-14T14:04:09+00:00</published><updated>2015-06-14T14:04:09+00:00</updated><id>https://afshinm.name/2015/06/14/mongodb-aggregation</id><content type="html" xml:base="https://afshinm.name/2015/06/14/mongodb-aggregation/">&lt;p&gt;MongoDB has a &lt;code class=&quot;prettyprint&quot;&gt;aggregate&lt;/code&gt; operator that help programmers to define complex type of queries and combine operators such as &lt;code class=&quot;prettyprint&quot;&gt;group&lt;/code&gt;, &lt;code class=&quot;prettyprint&quot;&gt;match&lt;/code&gt; and &lt;code class=&quot;prettyprint&quot;&gt;find&lt;/code&gt; to build a complete result set.&lt;/p&gt;

&lt;p&gt;Unfortunately, this operator doesn’t have enough tutorials and examples so I decided to explain it in better words.&lt;/p&gt;

&lt;h1 id=&quot;aggregate-is-a-pipeline_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#aggregate-is-a-pipeline_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Aggregate is a pipeline&lt;/h1&gt;

&lt;p&gt;First of all, you should know that &lt;code class=&quot;prettyprint&quot;&gt;aggregate&lt;/code&gt; is a pipeline of different operators. So, what does it mean?&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;prettyprint&quot;&gt;aggregate&lt;/code&gt; operator accepts an array of stages:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;db.collection.aggregate( [ { &amp;lt;stage&amp;gt; }, ... ] )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Each stage contains an operator such as &lt;code class=&quot;prettyprint&quot;&gt;$match&lt;/code&gt;, $limit&lt;code class=&quot;prettyprint&quot;&gt;or&lt;/code&gt;$project&lt;code class=&quot;prettyprint&quot;&gt;. After executing,&lt;/code&gt;aggregate` operator starts to perform each stage one by one. It’s important to know that each stage get the result set from the &lt;strong&gt;previous&lt;/strong&gt; stage and the result set for the first stage is all records of the collection.&lt;/p&gt;

&lt;p&gt;Following diagram illustrates a sample &lt;code class=&quot;prettyprint&quot;&gt;aggregate&lt;/code&gt; procedure (source: &lt;a href=&quot;http://www.mongodb.org):&quot; rel=&quot;nofollow&quot;&gt;www.mongodb.org):&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://svbtleusercontent.com/vpexilzjirmwyw.png&quot; title=&quot;Pipeline of an aggregate operator - source: Mongodb.org&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;https://svbtleusercontent.com/vpexilzjirmwyw_small.png&quot; alt=&quot;aggregation-pipeline.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;example_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#example_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Example&lt;/h1&gt;

&lt;p&gt;Let’s assume we have a &lt;code class=&quot;prettyprint&quot;&gt;users&lt;/code&gt; collection with following records:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;[{
  _id: '55329ec72d3c018764000001',
  name: 'Steve',
  role: 'admin',
  age: 35
}, {
  _id: '55329ec72d3c018764000002',
  name: 'Guillermo',
  role: 'admin',
  age: 28
}, {
  _id: '55329ec72d3c018764000003',
  name: 'Roshan',
  role: 'user',
  age: 45
}]
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;example-with-two-stages_2&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#example-with-two-stages_2&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Example with two stages&lt;/h2&gt;

&lt;p&gt;Following &lt;code class=&quot;prettyprint&quot;&gt;aggregate&lt;/code&gt; operator has two stages, &lt;code class=&quot;prettyprint&quot;&gt;$sort&lt;/code&gt; and &lt;code class=&quot;prettyprint&quot;&gt;$limit&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;User.aggregate([{
  $sort: { age: -1 }
}, {
  $limit: 2
}], function (err, records) { 
  //...
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First of all, the &lt;code class=&quot;prettyprint&quot;&gt;aggregate&lt;/code&gt; operator executes the first stage. The result set for the first stage is the whole collection records. &lt;code class=&quot;prettyprint&quot;&gt;$sort&lt;/code&gt; operator sorts the &lt;code class=&quot;prettyprint&quot;&gt;users&lt;/code&gt; collection by &lt;code class=&quot;prettyprint&quot;&gt;age&lt;/code&gt; field. The result of the first stage is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;[{
  _id: '55329ec72d3c018764000003',
  name: 'Roshan',
  role: 'user',
  age: 45
}, {
  _id: '55329ec72d3c018764000001',
  name: 'Steve',
  role: 'admin',
  age: 35
}, {
  _id: '55329ec72d3c018764000002',
  name: 'Guillermo',
  role: 'admin',
  age: 28
}]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, the second stage gets the sorted array of &lt;code class=&quot;prettyprint&quot;&gt;users&lt;/code&gt; from previous stage and eliminates a record. Finally, the result of &lt;code class=&quot;prettyprint&quot;&gt;aggregate&lt;/code&gt; operator will be:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;[{
  _id: '55329ec72d3c018764000003',
  name: 'Roshan',
  role: 'user',
  age: 45
}, {
  _id: '55329ec72d3c018764000001',
  name: 'Steve',
  role: 'admin',
  age: 35
}]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can find more operators for aggregation stages here: &lt;a href=&quot;http://docs.mongodb.org/manual/reference/operator/aggregation/&quot; rel=&quot;nofollow&quot;&gt;http://docs.mongodb.org/manual/reference/operator/aggregation/&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;conclusion_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#conclusion_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;Aggregate operator is a pipeline of stages. It accepts an array of stages and each stage has a operator. The first stage gets all records of the collection and after performing the operator, passes the result to the next stage.&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="aggregation" /><category term="database" /><category term="mongodb" /><summary type="html">MongoDB has a aggregate operator that help programmers to define complex type of queries and combine operators such as group, match and find to build a complete result set.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://afshinm.name/wp-content/uploads/2015/06/vpexilzjirmwyw_small.png" /></entry><entry><title type="html">Scheduled backup for MongoDB</title><link href="/2015/05/29/scheduled-backup-for-mongodb/" rel="alternate" type="text/html" title="Scheduled backup for MongoDB" /><published>2015-05-29T15:09:07+00:00</published><updated>2015-05-29T15:09:07+00:00</updated><id>https://afshinm.name/2015/05/29/scheduled-backup-for-mongodb</id><content type="html" xml:base="https://afshinm.name/2015/05/29/scheduled-backup-for-mongodb/">&lt;p&gt;MongoDB is one of NoSQL pioneers and it has a great community. Nowadays, most of startups prefer to use MongoDB as the main database because of its simplicity. Configuring scheduled backup for a database is really important to keep the last updated data somewhere and restore it in case of database crash.&lt;/p&gt;

&lt;p&gt;In this post I want to introduce a simple open-source tool that I’ve recently published to setup a minimal scheduled backup for MongoDB.&lt;/p&gt;

&lt;h1 id=&quot;dependencies_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#dependencies_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Dependencies&lt;/h1&gt;

&lt;p&gt;I built this tool using Node.js so first of all you need to install &lt;a href=&quot;http://nodejs.org/&quot; rel=&quot;nofollow&quot;&gt;http://nodejs.org/&lt;/a&gt;. I will publish a binary version for all platforms soon.&lt;/p&gt;

&lt;p&gt;You need no more dependencies but official Node.js module. In the next step we will install them using &lt;code class=&quot;prettyprint&quot;&gt;npm&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;install_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#install_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Install&lt;/h1&gt;

&lt;p&gt;I named this tool &lt;strong&gt;mongodb-backup&lt;/strong&gt; and you can clone the repository from Github:&lt;/p&gt;

&lt;blockquote class=&quot;large&quot;&gt;
  &lt;p&gt;
    &lt;a href=&quot;https://github.com/afshinm/mongodb-backup&quot; rel=&quot;nofollow&quot;&gt;https://github.com/afshinm/mongodb-backup&lt;/a&gt;
  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then go to the folder of project and run following command:&lt;/p&gt;

&lt;blockquote class=&quot;large&quot;&gt;
  &lt;p&gt;
    npm install
  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This command installs all required dependencies including AWS sdk.&lt;/p&gt;

&lt;h1 id=&quot;configuration_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#configuration_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Configuration&lt;/h1&gt;

&lt;p&gt;This tool has a simple &lt;code class=&quot;prettyprint&quot;&gt;config.js&lt;/code&gt; file that defines all dependencies to run the &lt;code class=&quot;prettyprint&quot;&gt;mongodump&lt;/code&gt; command and upload it to the Amazon S3 storages. Here you can find a list of all dependencies: &lt;a href=&quot;https://github.com/afshinm/mongodb-backup#configjs&quot; rel=&quot;nofollow&quot;&gt;https://github.com/afshinm/mongodb-backup#configjs&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;go-go-go_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#go-go-go_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Go go go&lt;/h1&gt;

&lt;p&gt;Final step is to run the script. You can run the cronjob using following command:&lt;/p&gt;

&lt;blockquote class=&quot;large&quot;&gt;
  &lt;p&gt;
    node index.js start
  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And if you want to run the script as a deamon, install &lt;code class=&quot;prettyprint&quot;&gt;forever&lt;/code&gt; using &lt;code class=&quot;prettyprint&quot;&gt;npm install forever&lt;/code&gt; and then run this command:&lt;/p&gt;

&lt;blockquote class=&quot;large&quot;&gt;
  &lt;p&gt;
    forever start index.js start
  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1 id=&quot;conclusion_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#conclusion_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;This version of &lt;code class=&quot;prettyprint&quot;&gt;mongodb-backup&lt;/code&gt; works with Amazon S3 but I will publish another version soon that accepts FTP and SFTP to transfer backup files.&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="backup" /><category term="mongodb" /><summary type="html">MongoDB is one of NoSQL pioneers and it has a great community. Nowadays, most of startups prefer to use MongoDB as the main database because of its simplicity. Configuring scheduled backup for a database is really important to keep the last updated data somewhere and restore it in case of database crash.</summary></entry><entry><title type="html">Using `object` as the keys of another `object` in JavaScript</title><link href="/2015/05/12/using-object-as-the-keys-of-another-object-in-javascript/" rel="alternate" type="text/html" title="Using `object` as the keys of another `object` in JavaScript" /><published>2015-05-12T12:59:06+00:00</published><updated>2015-05-12T12:59:06+00:00</updated><id>https://afshinm.name/2015/05/12/using-object-as-the-keys-of-another-object-in-javascript</id><content type="html" xml:base="https://afshinm.name/2015/05/12/using-object-as-the-keys-of-another-object-in-javascript/">&lt;p&gt;Sometimes you need to use &lt;code class=&quot;prettyprint&quot;&gt;object&lt;/code&gt;s for the keys of a dictionary (let’s say, another &lt;code class=&quot;prettyprint&quot;&gt;object&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Assume this code:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;var keys = [{a: 1}, {a: 2}, {a: 3}];
var dic = {};

//add a new key to the dictionary
dic[keys[0]] = 'boo';
dic[keys[1]] = 'foo';
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Above code yields this result:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;console.log(dic); //Object {[object Object]: &quot;foo&quot;}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So the dictionary doesn’t have two items because &lt;code class=&quot;prettyprint&quot;&gt;object&lt;/code&gt; keys cannot be &lt;code class=&quot;prettyprint&quot;&gt;object&lt;/code&gt;. You can get the item with &lt;code class=&quot;prettyprint&quot;&gt;[object Object]&lt;/code&gt; string:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;console.log(dic['[object Object]']); //returns `foo`
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, wait. Then how we can set objects as the keys of a dictionary?&lt;/p&gt;

&lt;h1 id=&quot;ecmascript-6-map_1&quot;&gt;&lt;a class=&quot;head_anchor&quot; href=&quot;#ecmascript-6-map_1&quot; rel=&quot;nofollow&quot;&gt; &lt;/a&gt;ECMAScript 6 – Map&lt;/h1&gt;

&lt;p&gt;&lt;code class=&quot;prettyprint&quot;&gt;Map&lt;/code&gt; feature in ES6 enables you to assign &lt;code class=&quot;prettyprint&quot;&gt;object&lt;/code&gt;s as the keys of a dictionary (or another &lt;code class=&quot;prettyprint&quot;&gt;object&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Here is the ES6 compatible of the previous example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;var keys = [{a: 1}, {a: 2}, {a: 3}];
var map = new Map();

//add a new key to the dictionary
map.set(keys[0], 'boo');
map.set(keys[1], 'foo');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And the &lt;code class=&quot;prettyprint&quot;&gt;map&lt;/code&gt; variable should be:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;Map {Object {a: 1} =&amp;gt; &quot;boo&quot;, Object {a: 2} =&amp;gt; &quot;foo&quot;}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Besides, you can get the item with &lt;code class=&quot;prettyprint&quot;&gt;get&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;map.get(keys[0]); //returns `boo`
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or set a new value for one of items:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;map.set([keys[0], 'new value');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here you can read more about Map feature in ECMAScript 6: &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map&quot; rel=&quot;nofollow&quot;&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map&lt;/a&gt;&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="javascript" /><category term="objects" /><summary type="html">Sometimes you need to use objects for the keys of a dictionary (let’s say, another object).</summary></entry><entry><title type="html">Block scope variable – ECMAScript 6</title><link href="/2015/05/03/block-scope-variable-ecmascript-6-2/" rel="alternate" type="text/html" title="Block scope variable – ECMAScript 6" /><published>2015-05-03T13:25:37+00:00</published><updated>2015-05-03T13:25:37+00:00</updated><id>https://afshinm.name/2015/05/03/block-scope-variable-ecmascript-6-2</id><content type="html" xml:base="https://afshinm.name/2015/05/03/block-scope-variable-ecmascript-6-2/">&lt;p&gt;Since some of ECMAScript 6 features looks weird to old-fashioned JavaScripters, I’m going to write a series of blog posts to describe the main idea of some common ECMAScript 6 features.&lt;/p&gt;

&lt;p&gt;In this posts I’d like to explain the usage of &lt;strong&gt;block scope variables&lt;/strong&gt;. The keyword &lt;code class=&quot;prettyprint&quot;&gt;let&lt;/code&gt; enables you to create a block scope variable in ECMAScript 6:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;let boo = 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, what’s the difference between &lt;code class=&quot;prettyprint&quot;&gt;var boo = 1;&lt;/code&gt; and &lt;code class=&quot;prettyprint&quot;&gt;let boo = 1;&lt;/code&gt;? Assume following example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;function sample() {
  if (true) {
    var boo = 1;
  }
  return boo;
}

console.log(sample()); //returns 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the above old-fashioned code, &lt;code class=&quot;prettyprint&quot;&gt;boo&lt;/code&gt; variable is defined within the &lt;code class=&quot;prettyprint&quot;&gt;sample&lt;/code&gt; function scope so we have the value even outside of the &lt;code class=&quot;prettyprint&quot;&gt;if&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;With &lt;code class=&quot;prettyprint&quot;&gt;let&lt;/code&gt; variable you can create a variable that is available within the &lt;code class=&quot;prettyprint&quot;&gt;if&lt;/code&gt; statement block:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;function sample() {
  if (true) {
    let boo = 1;
  }
  return boo; //ReferenceError: boo is not defined
}

console.log(sample());
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Awesome, isn’t it?&lt;/p&gt;

&lt;p&gt;I will write more blog posts about ECMAScript 6 features soon.&lt;/p&gt;</content><author><name>Afshin Mehrabani</name></author><category term="es6" /><category term="javascript" /><summary type="html">Since some of ECMAScript 6 features looks weird to old-fashioned JavaScripters, I’m going to write a series of blog posts to describe the main idea of some common ECMAScript 6 features.</summary></entry></feed>