Partial Function Application
- by AngelEyes
This is long overdo... Just a short and simple example
of Partial Function Application.
For some good explanations, which also include the difference between Currying and Partial Function Application, check out:
http://msmvps.com/blogs/jon_skeet/archive/2012/01/30/currying-vs-partial-function-application.aspx
and also this answer on stackoverflow: http://stackoverflow.com/a/8826698/532517
And here is the example, taken from real code:
internal void Addfile(string parentDirName, string fileName, long size)
{
AddElement(parentDirName, fileName, (name, parent) => new File(name, size, parent));
}
public void AddDir(string parentDirName, string dirName)
{
AddElement(parentDirName, dirName, (name, parent) => new Directory(dirName, parent));
}
private void AddElement(string parentDirName, string name,
Func<string, FileSystemElement, FileSystemElement> elementCreator)
{
ValidateFileNames(parentDirName, name);
var parent = (Directory)_index[parentDirName];
FileSystemElement element = elementCreator(name, parent);
parent._elements.Add(element);
_index.Add(name, element);
}