Wednesday, March 30, 2011

Learn Algorithms like you learn Maths

I am not gifted. I wish I was but the fact is to learn Maths and solve unknown problems I need to go and solve lots of problems. I use the knowledge gained from these extra problems to tackle unknown ones. Least to say, I at least get an idea on how to think ahead, what steps I could try. The solution will come if your steps are heading the right way (please note that there can be many different combinations of steps - not necessarily the one given in book).

I like programming and algorithms. Again I wish I was gifted but I am not. I can solve some amount of basic problems (stack, queues, linked lists, trees, basic sorts and so on) with probably not so optimal solution. But I mostly end up finding a better solution than mine (in the book or Google) which kind of pisses me off. I used to leave it there and not proceed further. I've now realized that I need to stop getting bugged and start admiring the solution. So, just like in Maths when I solved an new kind of problem and applied the technique learnt from it to another problem I need to do the same thing in Algorithms. When I find a unsolvable (for me) problem, I need to look at the solution and learn from it the approach that can be applied to another similar kind of problem. Hopefully, slowly I'll start moving to a stage where the number of problems I can solve increases.

Labels: ,

Thursday, April 12, 2007

JavaScript closures

As Doughlas Crockford rightly puts it The world's most misunderstood language, the language has certain very powerful features like closure which is rarely nicely utilized.

I've recently started working in JavaScript and learnt few concepts and techniques on closure in the language. Lemme first start explaining what it is.

Closure formally seems to be a complicated definition. I would like to put it "Closure is a technique which lets you declare an inner function and access outer function's variable in it by storing the state of outer function".


function outer() {
var i = Math.random() * 10 + 1; // random num from 1 to 10
return function (a) {
return i + a;
}
}

var f = outer(); // returns a function and stores it in f
alert(f(2)); // calls f which magically has access to 'i' even though the outer has returned

this would output 3 (1 the random number + 2). Now the first thing comes in mind after seeing this is how can inner function access it when it doesn't exist! This is because our way of thinking in traditional language environments like C where a variable is defined when the function is called (typically on stack) and released after the function returns. Here, the function after returning remains in memory (say like an object remains in memory in an object oriented system).

In these kind of cases where an inner function is defined, the outer function along with its current variables are stored and it is linked to inner function. One can think of outer function to be like an object privately storing its local variables including variables passed as arguments. The inner functions are linked to this particular object.



As can be seen in the diagram each invocation of "outer" creates a separate object and stores its inner variable. The inner function has a link to this object. Rather ONLY inner function has link to this object (function). Hence, when no variable is referencing the inner function, the inner function along with "outer" is garbage collected. If a function defines two inner functions both the inner functions have link to that copy of outer function. Guess thats why this technique is called "closure" as the inner function is closed on an environment which is the instance of outer function.

Now, I would like to share an incident which made me visualize closure better than earlier. I was writing code where I had to create functions based on data from an array.


function createFuncs(arr) {
for (var i = 0; i < arr.length; i++) {
var data = arr[i];
window[data.name] = function() {
// do something with 'data'
var s = data.some;
}
}
}


As you can see, I am trying to create functions on window object (window is the top level JavaScript object in browser environment) hence, writing something like window["func"] = function() {...} will create a function which can be directly invoked (like func(a)). This is how alert and other functions are. The function name and its working is based on "data". I would expect that each function works on its separate "data" when called, i.e. say func0 (from say data.name) would work on arr[0], func1 on arr[1] and so on. But I was wrong.. Turns out that all the funcs when invoked end up working on arr[arr.length - 1] (i.e. last element of the arr). This is because "data", a variable declared inside loop doesn't have block scope, it has function scope. When the loop finishes data refers to last element of array. All the functions created have link to the same function "createFuncs". Hence, when called they refer to one "data" variable which at that time refers to last element arr.

Well, this is not what I wanted. What to do? Then it just struck me that instead of closing created functions on createFuncs, I can close it on another function which has its required "data". So, I ended up doing something like this :)


function createFuncs(arr) {
for (var i = 0; i < arr.length; i++) {
var data = arr[i];
var createFunc = function(data) {
window[data.name] = function() {
// do something with 'data'
var s = data.some;
}
}
createFunc(data);
}
}


Now, every call to "createFunc" creates a instance of "createFunc" (the word instance may not be right but its the best I could fit in this situation) which has that "data" at that time and newly created function is bound to this "createFunc" and hence its "data".

Thats it folks.. Please do leave your comments on anything about the article :)

Thursday, October 05, 2006

no idea!!

first of all thanks to the person who is reading this blog... pls mail me if u read this.. i really wanna know!!

second of all, there is no second of all.. cause i dont have anything in mind to write.. just wanted to post something on the blog like lot of other people do.. maybe i'll write about some movie or song i heard..

if the person reading this knows me.. then he/she will be knowing how difficult it is for me to write this..

Saturday, March 25, 2006

Directory Analyzer

Hey!!

I am currently using this blog as my application's home page. Application is called Directory Analyzer. It can be downloaded from download.com. It gives contents of a directory is a pie chart. You can get more information on it at http://manish.tomar.googlepages.com/directoryanalyzer

Or download it from Get it from CNET Download.com!