« Functional C# - Part 1 | Main | Dictionary AddRange Extension Method »

February 23, 2009

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e5536ed7938833011278fc5f9428a4

Listed below are links to weblogs that reference Functional C# - Part 2:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Great post, thanks for sharing. I think I've seen something similar in Lua that I tried to port to C#, but this is a way better implementation.

Hi Patrick,

This was a great post, but I like to note that your definition of memoizing the CPS-ed Fibonacci function could be written A LOT more concisely as below (but may be a teeny bit less efficient):

public static Action> Memoize(this Action> function)
{
var cache = new Dictionary();
return (x, f) =>
{
if (!cache.ContainsKey(x))
{
function(x, fx => cache.Add(x, fx));
}
f(cache[x]);
};
}

Regards,
Tahir

Oh dear,

The code in my comment is not rendered properly but I am sure that you can understand very quickly what it is trying to do.

BTW the type signature of memoize has two type parameters because the type of the first parameter may be different from the first parameter of the continuation. For example, int.Parse, if written in CPS would be Action[string, Action[int]].

I am still not sure what your memoize function aims to do with all that code (CPS version only). Can you explain what it does please?

The comments to this entry are closed.