## Sunday, March 6, 2016

### Difference between exports and module.exports in Node.js

When you have large javascript file, you want to break that file into independent modules so that you can reuse each module in different files.  Node.js follows CommonJS module system in which modules are imported using require method available in global scope. For example, lets say you have two files, person.js and main.js. To import person.js functionality into main.js, you would do this

var Person = require('person');

Now you can use methods defined in person.js in main.js using Person variable. To be able to get the Person variable from person.js file, we should have return statement inside the person.js file so that require method assigns return value to the Person variable.  person.js would look like this

## Saturday, March 5, 2016

### Sorting Java objects based on property

In Java, sorting of group of  primitive type is simple. That is, sorting of group of integers, floats, doubles or strings is one step process (if you don't use your own algorithm for sorting).  java.util provides various methods for sorting these kind of group. Two of them are :
1. Arrays.sort()
2. Collections.sort()
Why sorting of group of primitive type is simple?

The answer is Java knows what exactly are those types and how to compare those types to each other. For example, comparing two integers is simply using the logical <, = and > operator. Similar for float, double and strings.

But, if you have your custom type (i.e. class) of data, then Java doesn't know what to do with them.  Lets say you have group of people with different properties like height, weight, age, name, religion and so on,  and you want Java to sort these people in increasing order. When Java tries to compare two people, it gets confused. It doesn't know which parameter to consider for sorting. Sorting can be done based on height, weight, age etc but which one to take as a parameter to decide?

To solve this problem, you need to explicitly specify the property. Based on the property, comparison should be carried out i.e. if you want to sort people based on their height, you need to tell Java explicitly that I want to sort people based on their height.

Programatically, this can be accomplished using Comparator interface as follows.

Collections.sort(persons, new Comparator () {
@Override
public int compare(Person p1, Person p2) {
return Integer.compare(p1.height, p2.height);
}
});


Passing implementation of Comparator interface as a second argument to Collection.sort method helps Java to make decision while comparing between two objects.

In above code snippet, you see that you need to implement one method i.e. compare , to carry out this operation.  If you closely observe the code you can see we  have explicitly specified that sorting needs to be carried out based on height. The compare method returns 3 possible values

1. A negative integer -> means first argument is less than second argument
2. Zero -> means both arguments are equal
3. A positive integer -> means first argument is greater than second argument
A runnable example for sorting is given bellow.

import java.util.*;
class Person {
// height in cm
public int height;
public String name;
public Person(int height, String name) {
this.height = height;
this.name = name;
}
}
class CustomSort {
static void sort(List persons) {
Collections.sort(persons, new Comparator () {
@Override
public int compare(Person p1, Person p2) {
return Integer.compare(p1.height, p2.height);
}
});
}
public static void main(String [] args) {
List persons = new ArrayList<>();
// add some persons to arraylist
CustomSort.sort(persons);
System.out.println("Sorted persons based on height");
System.out.println();
for (Person person: persons) {
System.out.println(person.name);
}
}
}


## Wednesday, September 30, 2015

### Retrofit 2.0 tutorial with sample application

Retrofit is a HTTP client library in Android and Java. It has great performance compares to AsyncTask and other android networking libraries. Retrofit 2.0 has been released few months back with major update. In this tutorial I am going to talk about how to create a simple REST client  in Android that communicates with REST endpoint to perform the READ and WRITE operations. This tutorial is intended for retrofit 2.0 beginner who are using retrofit for first time and getting their hand dirty with retrofit 2.0.

The overview of application that we are creating is as follows. The application has two main components 1) REST client in Android 2) REST server in Node.js. We will focus on REST client as this writing is for retrofit which is client library. We will create REST endpoints that creates and list User. User has two properties., username and name. The program  (Client) has two methods; one for creating user and other for listing all the users. Final android application has two fragments in pager view, one has interface for creating new user and another for listing the user. In backend, the program uses Mongodb as database.

## Friday, February 7, 2014

### Discrete Cosine Transform and JPEG compression : Image Processing

JPEG is well known standard for image compression and Discrete Cosine Transform (DCT) is the mathematical tool used by JPEG for achieving the compression. JPEG is lossy compression meaning some information is lost during the compression. Lets dig deeper into the JPEG standard starting from the block diagram.

Above diagram shows the encoder part of JPEG. The decoder is the reverse of encoder except it doesn't contains the reverse part of Quantizer block and this is the main reason why JPEG is lossy compression. We cannot recover data lost during the quantization step. The operations performed by each block is presented below

## Thursday, January 9, 2014

### Google Cloud Messaging (GCM) in Android using PHP Server

GCM for android is a service which is basically used to send the data from server to the android devices. One use of this GCM is a push notification service. In this tutorial, I am going through all the steps needed to setup the GCM and build a simple but complete android  application in Eclipse.

## Thursday, December 26, 2013

### Logistic Regression in Machine Learning

Before studying logistic regression, I would recommend you to go through these tutorials.
The first and most important thing about logistic regression is that it is not a "Regression" but a "Classification" algorithm. The name itself is somewhat misleading. Regression gives a continuous numeric output but most of the time we need the output in classes (i.e. categorical, discrete). For example, we want to classify emails into "spam" or "not spam",  classify treatment into "success" or "failure", classify statement into "right" or "wrong" , classify transactions into "fraudulent" or "non-fraudulent" and so on. These are the examples of  logistic regression having binary output (also called dichotomous). Note that the output may not always be binary but in this article I merely talk about  binary output.

## Saturday, December 21, 2013

### Nepal over last Four Decades with R

The World Bank DataBank provides the data of all countries for more than 1000 indicators since 1960. It can be used for various statistical analysis purpose. This weekend, I downloaded the Data for Nepal, and tried few simple things in R, mainly for the purpose of learning R. R is very powerful statistical analysis tool.

The data was downloaded as csv file, which can be read with read.csv() function in R. We can run the R commands from R command line, and its really interactive.

### Gradient descent versus normal equation

Gradient descent and normal equation (also called batch processing) both are methods for finding out the local minimum of a function. I have given some intuition about gradient descent in previous article. Gradient descent is actually an iterative method to find out the parameters. We start with the initial guess of the parameters (usually zeros but not necessarily), and gradually adjust those parameters so that we get the function that best fit the given data points. Here is the mathematical formulation of gradient descent in the case of linear regression.