Created
July 17, 2015 14:55
-
-
Save albertoventurini/fbf35fa7e99e5df1815f to your computer and use it in GitHub Desktop.
C# caching function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace CachingFunction | |
{ | |
public class CachingFunction<T1, TResult> | |
{ | |
private readonly Func<T1, TResult> _func; | |
private T1 _cachedParam; | |
private TResult _cachedResult; | |
public CachingFunction(Func<T1, TResult> func) | |
{ | |
_func = func; | |
} | |
public TResult Execute(T1 param) | |
{ | |
if (!EqualityComparer<T1>.Default.Equals(_cachedParam, param)) | |
{ | |
_cachedParam = param; | |
_cachedResult = _func(param); | |
} | |
return _cachedResult; | |
} | |
} | |
public class CachingFunction<T1, T2, TResult> | |
{ | |
private readonly Func<T1, T2, TResult> _func; | |
private readonly CachingFunction<T1, CachingFunction<T2, TResult>> _parent; | |
public CachingFunction(Func<T1, T2, TResult> func) | |
{ | |
_func = func; | |
_parent = new CachingFunction<T1, CachingFunction<T2, TResult>>(BuildParentFunction); | |
} | |
public TResult Execute(T1 p1, T2 p2) | |
{ | |
return _parent.Execute(p1).Execute(p2); | |
} | |
private CachingFunction<T2, TResult> BuildParentFunction(T1 p1) | |
{ | |
return new CachingFunction<T2, TResult>(p2 => _func(p1, p2)); | |
} | |
} | |
public class CachingFunction<T1, T2, T3, TResult> | |
{ | |
private readonly Func<T1, T2, T3, TResult> _func; | |
private readonly CachingFunction<T1, CachingFunction<T2, T3, TResult>> _parent; | |
public CachingFunction(Func<T1, T2, T3, TResult> func) | |
{ | |
_func = func; | |
_parent = new CachingFunction<T1, CachingFunction<T2, T3, TResult>>(BuildParentFunction); | |
} | |
private CachingFunction<T2, T3, TResult> BuildParentFunction(T1 p1) | |
{ | |
return new CachingFunction<T2, T3, TResult>((p2, p3) => _func(p1, p2, p3)); | |
} | |
public TResult Execute(T1 p1, T2 p2, T3 p3) | |
{ | |
return _parent.Execute(p1).Execute(p2, p3); | |
} | |
} | |
public class CachingFunction<T1, T2, T3, T4, TResult> | |
{ | |
private readonly Func<T1, T2, T3, T4, TResult> _func; | |
private readonly CachingFunction<T1, CachingFunction<T2, T3, T4, TResult>> _parent; | |
public CachingFunction(Func<T1, T2, T3, T4, TResult> func) | |
{ | |
_func = func; | |
_parent = new CachingFunction<T1, CachingFunction<T2, T3, T4, TResult>>(BuildParentFunction); | |
} | |
private CachingFunction<T2, T3, T4, TResult> BuildParentFunction(T1 p1) | |
{ | |
return new CachingFunction<T2, T3, T4, TResult>((p2, p3, p4) => _func(p1, p2, p3, p4)); | |
} | |
public TResult Execute(T1 p1, T2 p2, T3 p3, T4 p4) | |
{ | |
return _parent.Execute(p1).Execute(p2, p3, p4); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment