How to curry a Func and pass it to another Func with fewer arguments inside the constructor?
I have a class that looks like this
public class CacheManger<T> where T : class, new()
{
public CacheManger(Func<int, T> retriveFunc, Func<List<T>> loadFunc)
{
_retriveFunc = retriveFunc;
_loadFunc = loadFunc;
}
public T GetValueByid(int id)
{
return _retriveFunc(id);
}
}
I have another class which is as follows
public class AccountCache
{
public AccountCache ()
{
CacheManager = new CacheManger<Account>(GetAccount, LoadAcc);
// LoadAcc is another method that returns a List<Account>
}
private Account GetAccount(int accID)
{
return CacheManager.CacheStore.FirstOrDefault(o => o.ID == accID);
//CacheStore is the List<T> in the CacheManager.(internal datastore)
}
public Account GetProdServer(int accID)
{
return CacheManager.GetValueByid(accID);
}
}
Now as you could see I can pass GetAccount
to the constructor of CacheManager
. Now I have another class where i have a method like this
public User GetUser(string accountID, int engineID)
{
}
How could I pass this function to the CacheManager
's constructor.
I could carry the function, but then how could I pass it as a constructor argument?
What I am doing doing right now:
private User GetUserInternal(string accountID, int engineID)
{
return
/* actual code to get user */
}
private Func<string, Func<int, User>> Curry(Func<string, int, User> function)
{
return x => y => function(x, y);
}
public UserGetAccount(string accountID, int engineID)
{
_retriveFunc = Curry(GetUserInternal)(accountID);
CacheManager.RetrivalFunc = _retriveFunc; //I really dont want to do this. I had to add a public property to CacheManager class for this
return CacheManager.GetValueByid(engineID);// This will call GetUserInternal
}
You probably want partial application rather than currying. The best way I've found is to just create N functions that take from 1-N generic parameters, then let the compiler pick the one you want. If you take a look at my language-ext project I have two functions, one called curry
and one called par
for currying and partial application:
Currying and Partial Application source
To partially apply, do this:
// Example function
int AddFour(int a,int b,int c, int d)
{
return a + b + c + d;
}
// This returns a Func<int,int,int> with the first two arguments 10 & 5 auto-provided
var tenfive = par(AddFour, 10, 5);
// res = 10 + 5 + 1 + 2
var res = tenfive(1,2);
To curry, do this:
// Example function
int AddFour(int a,int b,int c, int d)
{
return a + b + c + d;
}
// Returns Func<int,Func<int,Func<int,Func<int,int>>>>
var f = curry(AddFour);
// res = 10 + 5 + 1 + 2
var res = f(10)(5)(1)(2);