Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hypothetical C# Metaprogramming
int MAX_ARGS = 10;
var arg_list = new string[MAX_ARGS]();
for (int i=0; i < MAX_ARGS; i++)
{
arg_list[i] = "arg_" + i.ToString
define_method(@"
static Func<A, R> Memoize<A, R>(this Func<A, R> function)
{
var cache = new Dictionary<A, R>();
return " + string.join(",", arg_list) + @" =>
{
R result;
if (!cache.TryGetValue(argument, out result))
{
result = function(argument);
cache[argument] = result;
}
return result;
};
}
"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment