using System;
using System.Collections.Generic;

namespace MusicMetaTagger.Core.Utility
{
	public static class Memoization
	{
		public static Func<T, K> MemoizeFunction<T, K>(this Func<T, K> function)
			where K : class where T : class
		{
			var table = new Dictionary<T, K>();
			return arg =>
			       	{
						if (arg == null) 
							return null;
			       		if (table.ContainsKey(arg)) 
							return table[arg];
			       		var result = function(arg);
			       		table[arg] = result;
			       		return result;
			       	};
		}
	}
}