Memoize<>

Language:
C#
Keywords:
Memoize
Code Snippet

public static Func<T, K> Memoize<T, K>(this Func<T, K> function)

{

    var table = new Dictionary<T, K>();

    return (args) =>

    {

        // Test the dictionary

        var result;

        if (table.TryGetValue(args, out result))

            return result;

 

        // Add new result

        result = function(args);

        table[args] = result;

        return result;

    };

}


Created 2013-03-27
comments powered by Disqus
Login