Skip to content

Instantly share code, notes, and snippets.

@jciechowski
Created Mar 12, 2018
Embed
What would you like to do?
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