Created
March 28, 2014 19:04
-
-
Save angelobelchior/9840524 to your computer and use it in GitHub Desktop.
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
using System.Collections.Generic; | |
namespace System | |
{ | |
public static class FuncExtentions | |
{ | |
public static Func<T, TResult> Memoize<T, TResult>(this Func<T, TResult> func) | |
{ | |
var cache = new Dictionary<T, TResult>(); | |
Func<T, TResult> memoizeFunc = n => | |
{ | |
if (cache.ContainsKey(n)) return cache[n]; | |
var result = func(n); | |
cache.Add(n, result); | |
return result; | |
}; | |
return memoizeFunc; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment