Thursday, December 20, 2012

Simple sshfs usage example

Follow this link: http://osxfuse.github.com/ to install sshfs for mac first.

Wednesday, December 12, 2012

Grep and Sed combine example

The command stores all the words in a file that has ' -' at the end to a new file using grep and sed.

Friday, November 16, 2012

Chrome V8 features

Reading upon this article from http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript and it gave some inside of the V8 javascript engine inside Chrome.

Citing from: http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/
  • A base compiler, which parses your JavaScript and generates native machine code before it is executed, rather than executing bytecode or simply interpreting it. This code is initially not highly optimized.
  • V8 represents your objects in an object model. Objects are represented as associative arrays in JavaScript, but in V8 they are represented with hidden classes 6, which are an internal type system for optimized lookups.
  • The runtime profiler monitors the system being run and identifies “hot” functions (i.e. code that ends up spending a long time running).
  • An optimizing compiler recompiles and optimizes the “hot” code identified by the runtime profiler, and performs optimizations such as inlining (i.e. replacing a function call site with the body of the callee).
  • V8 supports deoptimization, meaning the optimizing compiler can bail out of code generated if it discovers that some of the assumptions it made about the optimized code were too optimistic.
  • It has a garbage collector. Understanding how it works can be just as important as the optimized JavaScript.

Javascript inheritance example

Wednesday, November 07, 2012

Yet, it is javascript closure

Here are some basic examples of closure:


Wednesday, October 10, 2012

How to construct a Eulerian Tour graph


1. Randomly pick 2 nodes and connect them
2. Randomly pick a disconnected node and connect it with one of the connected node
3. Repeat step 2 util all the nodes are connected
4. Highlight the nodes that has odd degree
5. Find any node that has odd degree and connect it with another node with odd degree
6. Repeat step 5 until all nodes are having add degree

Trick:
If there odd degree node that are already connected another odd degree node. We can connect it with a even degree node, and from that we can connect the newly odd degree node with other odd degree node.

See demonstration from this video:

Thursday, August 23, 2012

Application of Logorithm

In computing, log2 is often used. One reason is that the number of bits needed to represent an integer n is given by rounding down log2(n) and then adding 1. For example log2(100) is about 6.643856. Rounding this down and then adding 1, we see that we need 7 bits to represent 100. Similarly, in order to have 100 leaves, a binary tree needs log2(100) levels. In the game where you have to guess a number between 1 and 100 based on whether it's higher or lower than your current guess, the average number of guesses required is log2(100) if you use a halving strategy to bracket the answer.
Reference: http://logbase2.blogspot.com.br/2007/12/log-base-2.html

Wednesday, July 11, 2012

Add role to anonymous user programmatically

Although some people may oppose to add additional role to anonymous user in drupal, if you want to do it anyway because you want anonymous user has rights to access some area of your site. Here is some code snippet you can use:

Tuesday, May 15, 2012

Highlight your code

I believe there are many programmers just like me who has a tech blog which use as channel to share ideas and knowledges with other people. Sometimes share some code snippets in inevitable. Alex Gorbatchev, the creator of syntaxhighlighter, has been great for sharing his scripts syntaxhighlighter to the rest of world. It has been worked great on many platforms and framework and very easy to implement.

Personally, I have used it in drupal, wordpress and even now in blogger. I believed there are many people are using it as well; however, if you installed the recent version (v3.0.x), you may notices that the toolbar does not work well properly. Some how the toolbar icons are missing. The only icon you can see is the "?".  It appeared to me that many people are experiencing the same issue: http://stackoverflow.com/questions/4607609/syntaxhighlighter-not-showing-toolbar. I spent an hour to try to solve it without luck. And even the demo page on it official web site also has the problem that can be seem here: http://alexgorbatchev.com/SyntaxHighlighter/manual/demo/toolbar.html.

Regardless of the problem with current version, if you still want to use syntaxhighligher, as I know of, v2.1.382 still work great and it is the version that I am using it on this post.

As a side note, to use syntaxhiglighter in blogger.com, the only thing need to modify is the template. Add the following code right before the end of head tag. However, if you are running on Dynamic view, you CANN'T modify the template in HTML form. For that, there's a great solution on this post http://www.alexconrad.org/2011/12/highlight-code-with-bloggers-dynamic.html


If you don't like the scripts from Alex Gorbatchev for some reasons. There are other solutions you may find interested and may be easier to implement. 

THE GIST SOLUTION

If you have a github account, then you can use gist to share you code with other.Basically when you create a gist, you can embed into your page simply include oneline of javascript: Result:

THE GOOGLE CODE PRETIFY SOLUTION

This also a straight forward way to enable syntax highlight, and this also works well with dynamic view on blogger.com. The idea is for every page that you want syntax highlight, you put this the following line of javascript at the button of your page/post and also add some small amount of css in the template. You can find out more info on how this work at: http://www.alexconrad.org/2011/12/highlight-code-with-bloggers-dynamic.html
Result:
    
function IncludeJavaScript(sURL) {
    var oRequest;
    
    // if using a normal browser
    if (window.XMLHttpRequest) {
        oRequest = new XMLHttpRequest();
    } else {
        if (window.ActiveXObject) {
            // if using IE 6 for some terrible reason
            oRequest  = new ActiveXObject('MSXML2.XMLHTTP.3.0');
        }
    }
    
    oRequest.open("GET",sURL,false);
    oRequest.setRequestHeader("User-Agent",navigator.userAgent);
    oRequest.send(null)
    
    if (oRequest.status==200) {
        eval(oRequest.responseText);
    } else {
        // alert("Error executing XMLHttpRequest.");
    }
}

// Now call the function, passing the script, here I'm including jQuery
IncludeJavaScript('https://gist.github.com/2710367.js');