Skip to content

Instantly share code, notes, and snippets.

@pilisera
Created April 23, 2015 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pilisera/071305e20aa831a79d71 to your computer and use it in GitHub Desktop.
Save pilisera/071305e20aa831a79d71 to your computer and use it in GitHub Desktop.
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