Syntax Highlighting Post

Syntax highlighting is a feature that displays source code, in different colors and fonts according to the category of terms. This feature facilitates writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct. Highlighting does not affect the meaning of the text itself; it is intended only for human readers.1

GFM Code Blocks

GitHub Flavored Markdown fenced code blocks are supported. To modify styling and highlight colors edit /_sass/syntax.scss.

#container {
  float: left;
  margin: 0 -240px 0 0;
  width: 100%;
}
.highlight {
  margin: 0;
  padding: 1em;
  font-family: $monospace;
  font-size: $type-size-7;
  line-height: 1.8;
}
<nav class="pagination" role="navigation">
  {% if page.previous %}
    <a href="{{ site.url }}{{ page.previous.url }}" class="btn" title="{{ page.previous.title }}">Previous article</a>
  {% endif %}
  {% if page.next %}
    <a href="{{ site.url }}{{ page.next.url }}" class="btn" title="{{ page.next.title }}">Next article</a>
  {% endif %}
</nav><!-- /.pagination -->
1
2
3
4
5
6
7
8
<nav class="pagination" role="navigation">
  {% if page.previous %}
    <a href="{{ site.url }}{{ page.previous.url }}" class="btn" title="{{ page.previous.title }}">Previous article</a>
  {% endif %}
  {% if page.next %}
    <a href="{{ site.url }}{{ page.next.url }}" class="btn" title="{{ page.next.title }}">Next article</a>
  {% endif %}
</nav><!-- /.pagination -->
module Jekyll
  class TagIndex < Page
    def initialize(site, base, dir, tag)
      @site = site
      @base = base
      @dir = dir
      @name = 'index.html'
      self.process(@name)
      self.read_yaml(File.join(base, '_layouts'), 'tag_index.html')
      self.data['tag'] = tag
      tag_title_prefix = site.config['tag_title_prefix'] || 'Tagged: '
      tag_title_suffix = site.config['tag_title_suffix'] || '&#8211;'
      self.data['title'] = "#{tag_title_prefix}#{tag}"
      self.data['description'] = "An archive of posts tagged #{tag}."
    end
  end
end

Code Blocks in Lists

Indentation matters. Be sure the indent of the code block aligns with the first non-space character after the list item marker (e.g., 1.). Usually this will mean indenting 3 spaces instead of 4.

  1. Do step 1.
  2. Now do this:

    def print_hi(name)
      puts "Hi, #{name}"
    end
    print_hi('Tom')
    #=> prints 'Hi, Tom' to STDOUT.
    
  3. Now you can do this.

GitHub Gist Embed

An example of a Gist embed below.

Articles Read in July 2013

  1. Jitbit’s SQL interview questions.

    A basic SQL interview question list and its answer. Basic select, join on, etc. Also there is a debate in the comments about whether a modern developer should learn these basic DB technologies.

  2. Java Basic Input and Output.

    Java I/O is not as simple as you might think it should be. For example, in what scenarios, should you use BufferedInputStream?

Articles read in June 2013

  1. A Visual Explanation of SQL Joins.

    it explains how INNER JOIN and OUTER JOIN work in a visualized way.

  2. Difference between clustered index and non-clustered index.

    A clustered index is like a dictionary, where all words are sorted in alphabetical order in the entire book. A non-clustered index is like an index in the last pages of a book, where keywords are sorted and contain the page number to the material of the book for faster reference.

  3. What is SOA.

    SOA is supposed to solve this(hundreds of separate apps that are insufficiently integrated) by designing every app from the ground up to publish its services in a standardized, cross-platfrom manner so that other apps can access the data and don’t have to duplicate it.

Understanding Require in NodeJs

require in NodeJs is very different from the client-side library RequireJs. The latter will not be covered in this post.

require is an implementation in NodeJs to support moduling following CommonJs specfication.

require works like include (in C++) or import (in Java) things. The basic functionanlity of require is that it reads a javascript file, executes the file and proceeds to return the exports object.

An example of a required file:

console.log('evaluating example.js');
exports.message = 'hello world';

If you run var example=require('./example.js'), we will see evaluating example.js on the console, and example be an object equal to:

{
    message: "hello world"
}

However, you shold not change the reference of exports. An error example:

console.log('Hello Error world');
// exports will NOT be returned.
exports = function Error(){};

Your code can be view in this way:

var module = { exports: {} };
var exports = module.exports;
// Your code
return module.exports;

module is a plain Javascript object. If you change exports by exprots = another_object or exports = a_function, you will lose it (module.exports will not be changed after the reference of exports is changed.)

Some other points you should know about require:

  • required exports is cached.
  • you can omit .js and require will automatically append it if needed.
  • If the file doesn’t start with “./” or “/”, then it is consided as either a core module or located in local node_modules folder.
  • If the file stars with “./”, it is considered a relative file to the file that called require.
  • If the file starts with “/”, it is consided an absolute path.
  • If the file-name is actually a directory, it will first look for package.json and load the file referenced in the main property; otherwise, it will look for an index.js.

References:

  1. http://docs.nodejitsu.com/articles/getting-started/what-is-require
  2. http://stackoverflow.com/questions/16383795/difference-between-module-exports-and-exports-in-the-commonjs-module-system/16383925?noredirect=1#16383925

Comparing Function Value Reference in Scala and JavaScript

The functional programming flavor in Scala is very simimliar to that of JavaScript. Let’s compare the following code:

// Javascript code, can be found here: http://codepen.io/MrCoder/pen/bcKLF
function filter_odd(i){
  return i % 2 === 0 ? 0 : i;
}

function filter_even(i){
  return i % 2 === 0 ? i : 0;
}

function total(max, filter){
  var result = 0;
  for (var i = 0; i < max; i++){
    result += filter(i);
  }
  return result;
}

console.log(total(100, filter_odd));
console.log(total(100, filter_even));

Then let’s have a look at Scala implemenation:

def filter_odd(i: Int) = {if (i%2 == 0) 0 else i}
def filter_even(i: Int) = {if (i%2 == 0) i else 0}

def total(max: Int, filter: Int => Int) = {
    var result = 0
    for(i <- 1 to max){
      result += filter(i)
    }
    result
}

println(total(100, filter_odd))
println(total(100, filter_even))

What makes it even more interesting is that both of them have a second style for the filter_xxx

// JavaScript. Can be found here: http://codepen.io/MrCoder/pen/zjGiu
var filter_odd = function(i){
  return i % 2 === 0 ? 0 : i;
}

var filter_even = function(i){
  return i % 2 === 0 ? i : 0;
}

And Scala version:

val filter_even = {(i: Int) => if (i%2 == 0) i else 0}
var filter_odd = {(i: Int) => if (i%2 == 0) 0 else i}