Partial Function Application
Posted
by AngelEyes
on Geeks with Blogs
See other posts from Geeks with Blogs
or by AngelEyes
Published on Thu, 27 Sep 2012 08:20:31 GMT
Indexed on
2012/09/27
21:38 UTC
Read the original article
Hit count: 215
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);
}
© Geeks with Blogs or respective owner