Created
October 27, 2011 13:59
-
-
Save jasonporritt/1319611 to your computer and use it in GitHub Desktop.
Memoization in C#: Complex Input
This file contains hidden or 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
public static Func<TSource1, TSource2, TReturn> Memoize<TSource1, TSource2, TReturn>(Func<TSource1, TSource2, TReturn> func) | |
{ | |
var cache = new Dictionary<string, TReturn>(); | |
return (s1, s2) => | |
{ | |
var key = s1.GetHashCode().ToString() + s2.GetHashCode().ToString(); | |
if (!cache.ContainsKey(key)) | |
{ | |
cache[key] = func(s1, s2); | |
} | |
return cache[key]; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment