Archive

Archive for July, 2009

Local variables are no longer local

July 20, 2009 Leave a comment

Let us consider this code –

public class MathUtil {

	public static Func<int> MakeIncrementer() {
		int n = 0;

		Func<int> incr = () => ++n;
		return incr;
	}
}

Given the above code, what do you think should be the output of the below program –

public class Program {

	static void Main(string[] args) {

		Func incr = MathUtil.MakeIncrementer();
		Func incr1 = MathUtil.MakeIncrementer();

		Console.WriteLine(incr());
		Console.WriteLine(incr());
		Console.WriteLine(incr());

		Console.WriteLine(incr1());
		Console.WriteLine(incr1());
		Console.WriteLine(incr1());
	}
}

Well, if your answer is –

1
2
3
1
2
3

then great, you already know what closures are and don’t need to read any further.

For those of you who don’t – Closures are a block of code with bindings to the environment they come from. Please read this post from Martin Fowler for more details. Also read an excellent article on Functional Programming by Slava. What does bindings and environment mean? In our case, the incr() function, inside the MakeIncrementer function has access (is bound) to the variable n, which is outside the scope (i.e. the environemnt) of the incr() function body.

The most important observations are –

1) That local variables are no longer limited to simple scope rules and have an undefined lifetime.
2) The obvious conclusion is that they’re no longer stored on the stack – they must be stored on the heap instead.

Slava writes this in his article – “Closures bring functional and OO worlds closer together. Every time you create a class that holds some state and pass it to somewhere else, think of closures. A closure is just an object that creates “member variables” on the fly by grabbing them from the scope, so you don’t have to!

And as my friend, Raj mentioned that Java had the notion of inner classes since long back, and they have similar characteristics, I believe closures (implemented with functions) are much less overhead, at least the syntactical part.

Do you still think that local variables are local and stored on the stackΒ  πŸ˜‰

Categories: Programming

First baby steps towards DSL nirvana

July 7, 2009 Leave a comment

I am sure, you must have come across this term, DSL – Domain Specific Language, if you read blogs from some well known industry experts. Well, I read this term long back, and got interested in it, and even bought the MEAP version of Ayende’s excellent book (and the only available, at the time, in the market on the subject) on Writing DSLs in Boo.

Obviously, I tried to write a DSL after reading the book. Maybe, I didn’t read it thoroughly, or for some other reason, but I couldn’t write one. And then I read this quote by Richard Feynman – β€œWhat I cannot create, I do not understand.” Obviously I was thoroughly disappointed, and felt bad and what not, but somehow I couldn’t perform the DSL magic.

Then I came across this excellent article on Lisp and it’s melleability for doing magic (read bending the language at will) and I was convinced about the power of the language. Then somehow, I came across Clojure and started learning it only because it’s a Lisp variant for JVM. And, then suddenly, one day, after spending not a very substantial time with it, the light bulb did glow for me πŸ™‚

So, a few of the basic characteristics which make a language suitable for DSL development are –

1) The language should have a very minimal syntax; yes, no semi-colons, no curly braces πŸ˜‰ [ I mean, these are noise ]
2) There should be some way by which the language supports lazy evaluation

Enough rambling and time for some code. Look at the following C# code –

public string [] GetPathsOfAllFiles(string folderName, string extension)
{
    return Directory.GetFiles(folderName, String.Format("*.{0}", extension));
}

And now look at this code (please try to imagine the context behind this code; I’ll post more on this later) –


(defmacro source-files-of-type [file-filter from folder]
Β Β Β  (list 'GetPathsOfAllFiles folder file-filter))

And yes, the above is valid code, which will run on .Net πŸ™‚ by way of Clojure’s port on CLR. So instead of –

SomeClassOrObject.GetPathsOfAllFiles("C:\\abc", "*.txt);

one can write –

(source-files-of-type "*.txt" from "C:\\abc")

Do you notice a difference in the readability? And as I mentioned before, I’ve started learning Clojure a mere three months back. Sweet πŸ™‚

I’ll be posting more on this once I am done with a first full implementation of a DSL, so until then, take care.

Categories: Programming

The yield (return) usage

July 7, 2009 Leave a comment

The yield keyword is a C# 2.0 feature. It’s been a long time that C# 3.0 is in the market, so much so C# 4.0 is looming over the horizon. Yet so many C# developers are not aware of its usage. Even I was not 100% clear, until I read (long back) this post from Joshua. Obviously, it’s a must read for anyone who can’t explain the usage of yield return.

Categories: Programming