<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Categories on Y-AX</title>
    <link>https://y-ax.com/categories/</link>
    <description>Recent content in Categories on Y-AX</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    
	<atom:link href="https://y-ax.com/categories/index.xml" rel="self" type="application/rss+xml" />
    
    
    
    <item>
      <title>ES6 Array.prototype.fill For Object Fix</title>
      <link>https://y-ax.com/es6-array.prototype.fill-for-object-fix/</link>
      <pubDate>Sun, 23 Oct 2016 00:00:00 +0000</pubDate>
      
      <guid>https://y-ax.com/es6-array.prototype.fill-for-object-fix/</guid>
      <description>&lt;p&gt;In ES6, we have Array.prototype.fill method, which will help us to initialize arrays with same values. for example: &lt;code&gt;new Array(5).fill(0)&lt;/code&gt; will get &lt;code&gt;[0,0,0,0,0]&lt;/code&gt;. it&amp;rsquo;s a very handy method.&lt;/p&gt;
&lt;p&gt;But we when you use it with object, like  &lt;code&gt;new Array(5).fill([])&lt;/code&gt;, or &lt;code&gt;new Array(5).fill({})&lt;/code&gt;, you will probaly got hurt by this method:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;let arr = new Array(5).fill({});
arr[0].name = &#39;test&#39;;
console.log(arr);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;you will got:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;[{name:&#39;test&#39;}, {name:&#39;test&#39;}, {name:&#39;test&#39;}, {name:&#39;test&#39;}, {name:&#39;test&#39;}]

&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;the same are for arrays, as array is object in JS too.&lt;/p&gt;
&lt;p&gt;As in JS, objects are passed by reference, and Array.prototype.fill do not consider this diffirence, so it&amp;rsquo;s impossible to just use fill to create an array of empty objects or arrays.&lt;/p&gt;
&lt;p&gt;We can use map to do the same thing actually:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;new Array(5).fill(null).map(()=&amp;gt;{})
&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    
    
    
    <item>
      <title>Solve NumberOfDiscIntersections</title>
      <link>https://y-ax.com/solve-numberofdiscintersections/</link>
      <pubDate>Fri, 14 Oct 2016 00:00:00 +0000</pubDate>
      
      <guid>https://y-ax.com/solve-numberofdiscintersections/</guid>
      <description>&lt;p&gt;For the NumberOfDiscIntersections problem of Codility site. it&amp;rsquo;s a little hard to figure it out.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://codility.com/programmers/lessons/6-sorting/number_of_disc_intersections/&#34;&gt;The Question&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;At first, I use bruce force method to solve it, the complexity is O(n^^2)&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;function solution(A) {
    // write your code in JavaScript (Node.js 6.4.0)
    A = A.map((x, i) =&amp;gt; [i-x, i+x])
    let c = 0;
    for(i = 0; i&amp;lt; A.length; i++){
        for(j = i+1; j &amp;lt; A.length; j++){
            if(A[i][1] &amp;gt;= A[j][0]){
                c++
                if(c &amp;gt; 10000000) return -1;
            }
        }
    }
    return c;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;On the web page, they require the worst complexity is O(nlogn), so we need find a better solution.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I searched on web, many people have a solution which sort the boundary of disc, and its complexity is O(nlogn), it suggest to use binary search whose complexity is O(logn), so I try and got this:&lt;/em&gt;&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;function solution(A) {
    // write your code in JavaScript (Node.js 6.4.0)
    A = A.map((x, i) =&amp;gt; [i-x, i+x])
    A = A.sort((a,b) =&amp;gt; a[0] - b[0])
    let lefts = A.map((x)=&amp;gt;x[0]);
    
    let search = function(list, x){
        let beg = 0;
        let end = list.length -1;
        let result = -1;
        while(beg &amp;lt;= end){
            let mid = Math.floor((beg+end)/2)
            if(list[mid] &amp;lt;= x){
                beg = mid + 1;
                result = mid;
            }else{
                end = mid -1;
            }
        }
        return result;
    }
    
    let c = 0;
    for(i = 0; i&amp;lt; A.length; i++){
        let rank = search(lefts, A[i][1])
        c += (rank+1) - i - 1;
        if(c &amp;gt; 10000000) return -1;
    }
    return c;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We first sort the left boundarys, and and then in a loop to find each right boundary&amp;rsquo;s position in it(using binary search), as the loop is O(n), and binary search is O(logn), so the total complexity is O(nlogn)&lt;/p&gt;
&lt;p&gt;But do we still have a better solution?&lt;/p&gt;
&lt;p&gt;We can consider the discs as events. for each disc, so each disc has 2 events: open event, and close event. And then we can use stack to trace the events. when a disc open, we push a record, when another disc open, we check how many records are there in the stack, that means the intersection, and when a disc close, we pop the stack.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;function solution(A) {
    // write your code in JavaScript (Node.js 6.4.0)
    var events = []
    A.forEach(function(x, i){
        events.push({type: &#39;left&#39;, value: i-x})
        events.push({type: &#39;right&#39;, value: i+x})
    })
    events = events.sort(function(a,b){
        if(a.value != b.value) return a.value - b.value;
        else return (a.type == &#39;right&#39; ? 1 : -1);
    })

    var c= 0;
    var stack = [];
    for(i = 0; i &amp;lt; events.length; i++){
        let event = events[i];
        
        if(event.type == &#39;left&#39;){
            c += stack.length;
            if(c &amp;gt; 10000000) return -1;
            stack.push(1);
        }else{
            stack.pop()
        }
    }

    return c;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now the complexity is O(n) now.&lt;/p&gt;
</description>
    </item>
    
    
    
    <item>
      <title>Sample Markdown</title>
      <link>https://y-ax.com/sample-markdown/</link>
      <pubDate>Sat, 01 Oct 2016 00:00:00 +0000</pubDate>
      
      <guid>https://y-ax.com/sample-markdown/</guid>
      <description>&lt;h1 id=&#34;an-h1-header&#34;&gt;An h1 header&lt;/h1&gt;
&lt;p&gt;Paragraphs are separated by a blank line.&lt;/p&gt;
&lt;p&gt;2nd paragraph. &lt;em&gt;Italic&lt;/em&gt;, &lt;strong&gt;bold&lt;/strong&gt;, &lt;code&gt;monospace&lt;/code&gt;. Itemized lists
look like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;this one&lt;/li&gt;
&lt;li&gt;that one&lt;/li&gt;
&lt;li&gt;the other one&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that &amp;mdash; not considering the asterisk &amp;mdash; the actual text
content starts at 4-columns in.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Block quotes are
written like so.&lt;/p&gt;
&lt;p&gt;They can span multiple paragraphs,
if you like.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Use 3 dashes for an em-dash. Use 2 dashes for ranges (ex. &amp;ldquo;it&amp;rsquo;s all in
chapters 12&amp;ndash;14&amp;rdquo;). Three dots &amp;hellip; will be converted to an ellipsis.&lt;/p&gt;
&lt;h2 id=&#34;an-h2-header&#34;&gt;An h2 header&lt;/h2&gt;
&lt;p&gt;Here&amp;rsquo;s a numbered list:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;first item&lt;/li&gt;
&lt;li&gt;second item&lt;/li&gt;
&lt;li&gt;third item&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Note again how the actual text starts at 4 columns in (4 characters
from the left side). Here&amp;rsquo;s a code sample:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;# Let me re-iterate ...
for i in 1 .. 10 { do-something(i) }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you probably guessed, indented 4 spaces. By the way, instead of
indenting the block, you can use delimited blocks, if you like:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;define foobar() {
    print &amp;quot;Welcome to flavor country!&amp;quot;;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(which makes copying &amp;amp; pasting easier). You can optionally mark the
delimited block for Pandoc to syntax highlight it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; time
&lt;span style=&#34;color:#75715e&#34;&gt;# Quick, count to ten!&lt;/span&gt;
&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; i &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;):
    &lt;span style=&#34;color:#75715e&#34;&gt;# (but not *too* quick)&lt;/span&gt;
    time&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sleep(&lt;span style=&#34;color:#ae81ff&#34;&gt;0.5&lt;/span&gt;)
    print i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;an-h3-header&#34;&gt;An h3 header&lt;/h3&gt;
&lt;p&gt;Now a nested list:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;First, get these ingredients:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;carrots&lt;/li&gt;
&lt;li&gt;celery&lt;/li&gt;
&lt;li&gt;lentils&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Boil some water.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Dump everything in the pot and follow
this algorithm:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;find wooden spoon
uncover pot
stir
cover pot
balance wooden spoon precariously on pot handle
wait 10 minutes
goto first step (or shut off burner when done)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Do not bump wooden spoon or it will fall.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Notice again how text always lines up on 4-space indents (including
that last line which continues item 3 above). Here&amp;rsquo;s a link to &lt;a href=&#34;http://foo.bar&#34;&gt;a
website&lt;/a&gt;. Here&amp;rsquo;s a link to a &lt;a href=&#34;local-doc.html&#34;&gt;local
doc&lt;/a&gt;. Here&amp;rsquo;s a footnote &lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;Done.&lt;/p&gt;
&lt;section class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;Footnote text goes here.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</description>
    </item>
    
    
    
    <item>
      <title>A Javascript Fibonacci (Generator) Function</title>
      <link>https://y-ax.com/a-javascript-fibonacci-generator-function/</link>
      <pubDate>Thu, 30 May 2013 00:00:00 +0000</pubDate>
      
      <guid>https://y-ax.com/a-javascript-fibonacci-generator-function/</guid>
      <description>&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fibonacci&lt;/span&gt; () {
	&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;;
	&lt;span style=&#34;color:#a6e22e&#34;&gt;fibonacci&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt; () {
		&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f2&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;;
		&lt;span style=&#34;color:#a6e22e&#34;&gt;fibonacci&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt; () {
			&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f3&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f2&lt;/span&gt;;
			&lt;span style=&#34;color:#a6e22e&#34;&gt;f1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f2&lt;/span&gt;;
			&lt;span style=&#34;color:#a6e22e&#34;&gt;f2&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f3&lt;/span&gt;;
			&lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f3&lt;/span&gt;;
		}
		&lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f2&lt;/span&gt;;
	}
	&lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;f1&lt;/span&gt;;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;in &lt;a href=&#34;http://wiki.ecmascript.org/doku.php?id=harmony:generators&#34;&gt;ES6(Harmony)&lt;/a&gt;, the generator keyword: yield will be introduced, a generator function will be like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fibonacci&lt;/span&gt;(){
	&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fn1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;;
	&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fn2&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;;
	&lt;span style=&#34;color:#66d9ef&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;){
		&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;current&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fn2&lt;/span&gt;;
		&lt;span style=&#34;color:#a6e22e&#34;&gt;fn2&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fn1&lt;/span&gt;;
		&lt;span style=&#34;color:#a6e22e&#34;&gt;fn1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fn1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;current&lt;/span&gt;;
		&lt;span style=&#34;color:#66d9ef&#34;&gt;yield&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;current&lt;/span&gt;;
	}
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
    
    
    
    <item>
      <title>Asynchronous closure call in loop in Javascript</title>
      <link>https://y-ax.com/asynchronous-closure-call-in-loop-in-javascript/</link>
      <pubDate>Wed, 15 May 2013 00:00:00 +0000</pubDate>
      
      <guid>https://y-ax.com/asynchronous-closure-call-in-loop-in-javascript/</guid>
      <description>&lt;p&gt;Problem: Async closure call in loop may cause mistake, Consider following code&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;; &lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;; &lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;++&lt;/span&gt;){
	&lt;span style=&#34;color:#a6e22e&#34;&gt;setTimeout&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(){
		&lt;span style=&#34;color:#a6e22e&#34;&gt;console&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;);
	},&lt;span style=&#34;color:#ae81ff&#34;&gt;1000&lt;/span&gt;)
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;what will the output be? &lt;code&gt;0 1 2 3&lt;/code&gt;? no, actually it will be &lt;code&gt;4 4 4 4&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The reason is &lt;code&gt;Async&lt;/code&gt;, when the closure: &lt;code&gt;function(){console.log(i)}&lt;/code&gt; been called, the loop already finished, and the &lt;code&gt;i&lt;/code&gt; is been changed to &lt;code&gt;4&lt;/code&gt;, so when the closure been called, all the &lt;code&gt;i&lt;/code&gt; is 4&lt;/p&gt;
&lt;h2 id=&#34;how-to-solve-this-problem&#34;&gt;How to solve this problem&lt;/h2&gt;
&lt;p&gt;we need to avoid the change of the &lt;code&gt;i&lt;/code&gt; in the closure, this time, we need use another closure:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;; &lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;; &lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;++&lt;/span&gt;){
	(&lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;){
		&lt;span style=&#34;color:#a6e22e&#34;&gt;setTimeout&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(){
			&lt;span style=&#34;color:#a6e22e&#34;&gt;console&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;);
		},&lt;span style=&#34;color:#ae81ff&#34;&gt;1000&lt;/span&gt;)
	})(&lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;)
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;we make a closure and call it instantly in the loop, so when the closure is been called each time, the &lt;code&gt;i&lt;/code&gt; is different, so in the closure inside this closure, the i is different.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Async&lt;/code&gt; and &lt;code&gt;Closure&lt;/code&gt; are both great features of Javascript, when using them in a correct way, they are very powerful and flexiable. And when you are using them together, you must be careful.&lt;/p&gt;
&lt;p&gt;Reference:
&lt;a href=&#34;https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Closures&#34;&gt;Closures&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    
    
    <item>
      <title>Auto start node.js application on linux server</title>
      <link>https://y-ax.com/auto-start-node.js-application-on-linux-server/</link>
      <pubDate>Thu, 09 May 2013 00:00:00 +0000</pubDate>
      
      <guid>https://y-ax.com/auto-start-node.js-application-on-linux-server/</guid>
      <description>&lt;p&gt;So we are developing node.js application and we want to keep it runing on server&lt;/p&gt;
&lt;h2 id=&#34;what-we-need&#34;&gt;What we need?&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;node.js application auto start on linux server boot&lt;/li&gt;
&lt;li&gt;node.js application auto restart when it crashed&lt;/li&gt;
&lt;li&gt;node.js application auto restart when files change&lt;/li&gt;
&lt;li&gt;developer can check the output of node.js application anytime&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;easy-problem-first&#34;&gt;Easy problem first&lt;/h2&gt;
&lt;p&gt;The problem 2,3 are easily solved by the &lt;a href=&#34;https://github.com/remy/nodemon&#34;&gt;nodemon&lt;/a&gt; module. This module can auto restart node.js app when it crashed and also when files of the app changed. OK, let&amp;rsquo;s install it and get it running first:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;npm install -g nodemon
cd /path/to/app
nodemon app.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And we see the outputs of the app, and also we have auto restart feature now.&lt;/p&gt;
&lt;p&gt;OK, if we close our terminal (for me, it&amp;rsquo;s putty), the nodemon process will be termiated as its parent process termiated. so let&amp;rsquo;s introduce &lt;a href=&#34;http://linux.die.net/man/1/screen&#34;&gt;screen&lt;/a&gt; to keep the session. to use it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;screen -S nodeapp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And now we are in the new &lt;code&gt;screen&lt;/code&gt; session (whose name is &lt;code&gt;nodeapp&lt;/code&gt;). run the &lt;code&gt;nodemon&lt;/code&gt; command:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;nodemon app.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we can press Ctrl+A,D to detach the &lt;code&gt;screen&lt;/code&gt; session, and even we close putty, the node is still runing. and later, when we login to the server again,and use:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;screen -r nodeapp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;to attach into the &lt;code&gt;screen session&lt;/code&gt; again.&lt;/p&gt;
&lt;p&gt;so we solved problem 2,3,4 now&lt;/p&gt;
&lt;p&gt;we make this into a shell script so we can run it anytime&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;touch ~/nodeapp.sh
chmod u+x ~/nodeapp.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;add add following shell command into it&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#!/bin/bash
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;cd /home/william
nodemon /path/to/app.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;auto-start-on-server-boot&#34;&gt;Auto start on server boot&lt;/h2&gt;
&lt;p&gt;Next, let&amp;rsquo;s focus on problem 1. to auto start the node app when server boot, in our situation, we want the server to automaticlly create a &lt;code&gt;screen&lt;/code&gt; session, and start &lt;code&gt;nodemon&lt;/code&gt; in it.&lt;/p&gt;
&lt;p&gt;When search on internet, i found many different solution for this, some one suggest &lt;a href=&#34;http://en.wikipedia.org/wiki/Systemd&#34;&gt;systemd&lt;/a&gt;, &lt;a href=&#34;http://en.wikipedia.org/wiki/Upstart&#34;&gt;upstart&lt;/a&gt;, and &lt;a href=&#34;http://mmonit.com/monit/&#34;&gt;Monit&lt;/a&gt; etc.&lt;/p&gt;
&lt;p&gt;But consider diffirent linux distro have diffirent boot machanism, it&amp;rsquo;s a little bit hard to do that.&lt;/p&gt;
&lt;p&gt;My solution is a lot easier. and much flexiable. We want:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;it auto start a screen session so that the developer can attach to that screen session again to see the outputs.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In most linux distro, we can find the /etc/init.d/rc.local file. this file is designed to be a custom shell script which will run on everytime the server boot. And in most case, that file will source the /etc/rc.local file too, so let&amp;rsquo;s add our command there.&lt;/p&gt;
&lt;p&gt;the command to add is:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;su - william -c &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;screen -d -m -S nodeapp /home/william/nodeapp.sh&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Let me explain this command, we can split it into 3 parts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;su - william&lt;/code&gt;: this command switch into the &lt;code&gt;william&lt;/code&gt; user who is the developer of the node.js app&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-c &#39;....&#39;&lt;/code&gt;: this part run the xxxx command once it logged into the developer user&lt;/li&gt;
&lt;li&gt;&lt;code&gt;screen -d -m -S nodeapp /home/william/nodeapp.sh&lt;/code&gt;: this part start a deattached &lt;code&gt;screen session&lt;/code&gt; whose name is &lt;code&gt;nodeapp&lt;/code&gt; and run the &lt;code&gt;nodeapp.sh&lt;/code&gt; shell script&lt;/li&gt;
&lt;/ol&gt;
</description>
    </item>
    
    
    
    <item>
      <title>Difference of module.exports and exports in node.js</title>
      <link>https://y-ax.com/difference-of-module.exports-and-exports-in-node.js/</link>
      <pubDate>Mon, 06 May 2013 00:00:00 +0000</pubDate>
      
      <guid>https://y-ax.com/difference-of-module.exports-and-exports-in-node.js/</guid>
      <description>&lt;p&gt;If you are new in node.js, you may get confused by the difference of &lt;code&gt;module.exports&lt;/code&gt; and &lt;code&gt;exports&lt;/code&gt; in node.js&amp;rsquo;s module system.&lt;/p&gt;
&lt;h2 id=&#34;docs&#34;&gt;Docs&lt;/h2&gt;
&lt;p&gt;Following 2 pages in official documents didn&amp;rsquo;t explain them very well:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://nodejs.org/api/globals.html#globals_exports&#34;&gt;Globals&lt;/a&gt;
&lt;a href=&#34;http://nodejs.org/api/modules.html&#34;&gt;Modules&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Let me give you the conclusion first:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;module.exports&lt;/code&gt; is the real object that been used&lt;/p&gt;
&lt;p&gt;&lt;code&gt;exports&lt;/code&gt; is just a reference&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&#34;details&#34;&gt;Details&lt;/h2&gt;
&lt;p&gt;Then let&amp;rsquo;s explain them in details:&lt;/p&gt;
&lt;p&gt;in Javascript, when you use &lt;code&gt;=&lt;/code&gt; to assign an object to a variable, it&amp;rsquo;s passed it&amp;rsquo;s reference, not value so:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;a&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {&lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;a&amp;#39;&lt;/span&gt;};
 &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;b&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;a&lt;/span&gt;;
 &lt;span style=&#34;color:#a6e22e&#34;&gt;b&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;b&amp;#39;&lt;/span&gt;;
 &lt;span style=&#34;color:#a6e22e&#34;&gt;console&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;a&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt;); &lt;span style=&#34;color:#75715e&#34;&gt;// this will output &amp;#39;b&amp;#39;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;in node.js, for each module, there is a &lt;code&gt;module&lt;/code&gt; object been automaticlly created. and in this &lt;code&gt;module&lt;/code&gt; object, an &lt;code&gt;exports&lt;/code&gt; attribute been automatically created too, and assigned a initial value: &lt;code&gt;{}&lt;/code&gt;, so in code, it look like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;module&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and then, Nodejs added a convinient way to use this &lt;code&gt;module.exports&lt;/code&gt; object&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;module&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So that when you want to add a attribute into &lt;code&gt;module.exports&lt;/code&gt; object, you can do it like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;test&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(){
	&lt;span style=&#34;color:#75715e&#34;&gt;//do something;
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;What if i want to assign the whole &lt;code&gt;exports&lt;/code&gt; object as a function? so you may try it like this way:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(){
	&lt;span style=&#34;color:#75715e&#34;&gt;//do something
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will not work. because when you do this, the &lt;code&gt;exports&lt;/code&gt; variable is been assigned to another object (a function), and it&amp;rsquo;s now not the same object as &lt;code&gt;module.exports&lt;/code&gt;, you can add following code to check it&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;module&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;===&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;// will return false
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and as I mentioned before: &lt;strong&gt;module.exports is the real object that been used&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;so when you want to do make the whole module just been a function. do it like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;module&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(){
	&lt;span style=&#34;color:#75715e&#34;&gt;//do something	
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
    
    
    
    <item>
      <title>Hello World!</title>
      <link>https://y-ax.com/hello-world/</link>
      <pubDate>Sun, 05 May 2013 00:00:00 +0000</pubDate>
      
      <guid>https://y-ax.com/hello-world/</guid>
      <description>&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-php&#34; data-lang=&#34;php&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;echo&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Hello World!&amp;#39;&lt;/span&gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;module&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(){
	&lt;span style=&#34;color:#a6e22e&#34;&gt;console&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Hello World!&amp;#39;&lt;/span&gt;);
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
    
    
    
    
  </channel>
</rss>
