Skip to content

Instantly share code, notes, and snippets.

@jciechowski
Created March 12, 2018 11:01
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 jciechowski/5779103d437926f13c6e261de9adfdf1 to your computer and use it in GitHub Desktop.
Save jciechowski/5779103d437926f13c6e261de9adfdf1 to your computer and use it in GitHub Desktop.
public class CourseTests
{
[Fact]
public void PlayIncreaseViewers()
{
Func<int, int> doubleX = x => x*2;
var memoized = doubleX.Memoize();
var result = memoized(4);
var obj = memoized(1);
Assert.Equal(result, obj);
}
}
public static class MemoizeExtension
{
public static Func<A, R> Memoize<A, R>(this Func<A, R> f)
{
var cache = new ConcurrentDictionary<A, R>();
return a => cache.GetOrAdd(a, f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment