Created
April 23, 2015 20:05
-
-
Save pilisera/071305e20aa831a79d71 to your computer and use it in GitHub Desktop.
Hypothetical C# Metaprogramming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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