Skip to content

Instantly share code, notes, and snippets.

@angelobelchior
Created March 28, 2014 19:04
Show Gist options
  • Save angelobelchior/9840524 to your computer and use it in GitHub Desktop.
Save angelobelchior/9840524 to your computer and use it in GitHub Desktop.
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