Caramel – Alpha source code is public


I have added the Caramel code generator to my public repository at google code.
http://code.google.com/p/alsing/

Please note that this is very very alpha and not actually useful yet.

Before it can be used I need to finish the template support and fully implement the DB meta data importer.

But if you are interested in this kind of stuff and want to help out building it, please let me know.

Book – Compiler principles… (The red dragon book)


Parsing and languages have always been one of my main interests when it comes to programming.
So a few weeks ago I decided to buy the “red dragon book” from Amazon.com.
http://www.amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/0201100886
(note: For some reason I managed to buy the edition from 1986 instead of 2006)

It is definetly one of the most interesting books I’ve ever read.
Maybe the most interesting part is that It is very humbeling to see how bloody brilliant those old computer science language guys are.

My biggest problem at the moment is that I fall asleep every 3-4 pages.
It is not because of boredom or anything, it’s just that I have to pause and reflect on what I have read for each page and that’s when I fall asleep :-)

So if you are into geeky stuff like domain specific languages and such, read the book, it’s awesome!

.NET Stateful delegates and memoization


I’ve been planning to write about stateful anonymous delegates for quite some time now so here I go :-)

The common perception of anonymous delegates is that they are stateless functions used for filtering and transformations and similar things.

What is less known is that anonymous delegates can hold state by binding to local variables.
If you create an anonymous delegate inside a factory method, you can infact give the delegate it’s own state.

Example:

static Func<int> CreateCounter() 
{
     int i = 0; //bind delegate to a local mutable variable 
     return () => i++; 

     //"i" doesn't really go out of scope here 
     // because it is bound to our delegate 
}

By using this method we can now create a stateful delegate:

var myCounter = CreateCounter(); 
Console.WriteLine( myCounter() ); //prints : 0 
Console.WriteLine( myCounter() ); //prints : 1 
Console.WriteLine( myCounter() ); //prints : 2

The above code will print 1, 2, 3 in the console window, simply because the delegate is increasing the value of the bound variable inside it’s body.

So where does anonymous delegates store it’s state?
The state is stored as fields inside a class that is auto generated by the C# compiler.
This class will contain the state for all bound variables / arguments that are used inside anonymous delegates defined inside the same method.

And when is this useful?
To be honest, almost never ;-)
But there are cases where it is actually quite nice.

Here is a sample that I came up with after reading Ayendes blog ( http://www.ayende.com/Blog/archive/2008/06/04/Review-Umbrella-project.aspx )
He wrote about “memoization” and I thought that would make a great example of stateful delegates.

We can create memoizations in C# by exploiting the fact that anonymous delegates bind to local variables.

Example:

static Func<T1, T2, TResult> CreateMemo
    <T1, T2, TResult>(Func<T1, T2, TResult> source)
{
    var cache = new Dictionary<string, TResult>();
    return (arg1, arg2) =>
           {
               string key = string.Format("{0}|{1}", arg1, arg2);
               if (!cache.ContainsKey(key))
               {
                   var value = source(arg1, arg2);
                   Console.WriteLine
                       ("Returning {0} and adding it to cache", value);
                   cache.Add(key, value);
                   return value;
               }
               else
               {
                   var value = cache[key];
                   Console.WriteLine
                       ("Returning {0} from cache", value);
                   return value;
               }
           };            
}

This code will create a memoization for any delegate of the type Func<T1, T2, TResult>.
You simply pass a delegate to it, and you will get a memoized delegate back.

The code creates a dictionary that will act as our cache.
And then returns a new delegate that are bound to this dictionary.
The returned delegate will transform it’s arguments into a key and then check if the key is present in the cache.
If the key is present, it will return the cached value.
And if not, it will call the wrapped delegate and then store the result in the cache.

You can even pass normal methods as argument to the memoizer and get a delegate that acts as a cache around your method.

Well that’s it :-)
Now go and abuse the mutable variable bindings in your delegates.

Enjoy.

//Roger